C++ Programming help

Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum

Help Support Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.
C++ can definitely look confusing. I hate looking at other people's code here at work, especially when they don't comment... I guess thats their version of job security.
 
C++ can definitely look confusing. I hate looking at other people's code here at work, especially when they don't comment... I guess thats their version of job security.
Agreed. I hate when people don't comment their code. The best was when I worked for this company, which had Taiwanese coders. Holy crap!

There was this function called enter trick mode without any comments whatsoever. I got the impression that most of the products were 90% test code.

Personally, I usually rewrite my functions 2-4 times until it is the most readable concise code that I can create.
 
Thats one of my least favorite things to do when I am coding. Our teacher obviously wants us to comment on everything. So I finally get done with a program, and now I have to make sure all the comments are there. Takes forever
 
Thats one of my least favorite things to do when I am coding. Our teacher obviously wants us to comment on everything. So I finally get done with a program, and now I have to make sure all the comments are there. Takes forever

I found that in programming, this is a good guideline for starting ANY project:

1. Outline the program. Just bullet points, which you can usually get from a spec sheet, or from the teacher's assignment.

2. Expand each bullet point. Think about the objects you may need to create, and what the function of them will be.

3. Pseudocode!
a. Take the above document and copy/paste the text into your C++ files, and use them as class/function/method headers.
b. Create your class and method declarations, only with no code inside the brackets.
c. For any complex logic, make comments where you will eventually add code. E.g. "//Loop through the input string, and examine each individual char."

4. CODE! If you've done all of the above steps, you'll be chomping at the bit to get to actual coding! Plus, you'll have a very solid understanding of the problem at hand, and your solution.

I was FORCED to do this in college, and hated it...but now that I'm programming for a living, I finally understand it. It is a great way to document your code, design a program, and make sure you've covered all the bases. It also gives you a good idea of how long the actual coding will take you. I can't tell you how many times I underestimated the length of time coding would take, becuase I didn't fully understand what needed to be done.
 
struct residentRec {
string social;
string lastname;
string phone;
char typephone;
char initial;
};
const int MAX = 500;
residentRec resinfo[MAX];

How can I pass this record to another function? Right under this code is the loop for adding user inputs. I am having trouble displaying the output.
 
struct residentRec {
string social;
string lastname;
string phone;
char typephone;
char initial;
};
const int MAX = 500;
residentRec resinfo[MAX];

How can I pass this record to another function? Right under this code is the loop for adding user inputs. I am having trouble displaying the output.


use a pointer of type residentRec.

ie
void PrintRecord(residentRec *R_ptr)
{
cout<<R_ptr->social<<"/n";
cout<<R_ptr->lastname<<"/n";
etc..
etc..
}

void main (void)
{
const int MAX = 500;
residentRec resinfo[MAX];
//fill information here somewhere
PrintRecord(&resinfo[0]);
}

I believe that is right, its been a while.
 
Let me give you a few pointers:

(Pun intended)

As a former CS major sometimes professors don't read code all that carefully if you comment well. I swear to god one of my professors would have taken this code if commented properly.

// function to return a value for a random dice roll guaranteed to be random.
int getrandomdiceroll()
{
return 4; //chosen by fair dice roll
//guaranteed to be random
}

Additionally you should consider switching over to python, I like it much better, but I work with databases all day so I am a little biased.
 
Well I finally figured our arrays of record, not I have to do some error checking and just random code to get this program finished.
 
Sorry I didn't get back to you sooner...looks like you figured it out.

Donasay - it's pretty tough to switch to Python in the middle of a C++ class...
 
Any idea how I would error check a ###-##-###? I know how to check to make sure their numbers, but how do I check the dashes?
 
Are the dashes always at those positions? If they are then its pretty easy you can just say something like:

if (!(char[3] == '-') || !(char[6] == '-')) //Are dashes in appropriate positions?
{
invalidInput();
}

if the dashes are allowed in various locations just add an or statement onto your isNumeric

if ((isNumeric(char)) || !(char == '-')) //Not numeric or a dash
{
invalidInput();
}


(please note that I program C++ on an IBM mainframe (TPF) and it makes it totally different from any OOP programming.... also our version of C++ is kinda screwy and a lot of weird commands that work differently; hopefully someone else can have better ideas)
 
Well I think they want us to check for everything, so are they numeric, are the dashes there, and did they put too many
 
Back
Top