Hi,
Somewhere in your Visual Studio IDE there might be an option to auto format the code with proper indenting and some sane placement of the braces.
Also, you can hopefully get it to do auto complete the braces, so when you type the left one, it automagically does the right hand one. This also works for (), [] and <>. That way you don't have to worry about it as you write code.
When using do loops, place the while condition on the same line as the closing brace.
Also get rid of line 10, seen as you already have lines 7 to 9. Read up about what the using statement and name spaces mean.
You have a lot of function definitions with a semi-colon, get rid of them:
152 153 154 155 156
|
} // indent to line up with the switch statement
} while // no condition for the do loop
int void hw1(); // start or function definition - remove semicolon
{
|
I have a personal preference where I don't have
do
loops. I find them error prone. All 3 forms of looping constructs can be translated into one of the other forms, albeit at the expense of another variable. So I use a
while
loop instead of a
do
loop. Just because a
do
loop always executes at least once shouldn't be the only motivation for using one. Use a
for
loop if the number of iterations is known.
Your
hw
functions are all declared as returning
int
, but they don't return anything - so they should be
void
? There should have been a compiler warning about that.
The normal way to layout your file is to put your function declarations, then
main()
, then function definitions.
Try really hard not to use global variables - you will loose lots of marks for doing that. At least declare them inside
main()
, then send whatever variables a function needs as arguments. Also try to identify which variables can be local to (inside) a function.
Another
Golden Rule is to always initialise your variables to something - preferably at the same time as declaration.
This part isn't doing what you think it does:
114 115 116 117 118 119
|
switch (choice)
{
case 1:
cout << "you have selected " << /* something here */ << "\n";
int hw1 ; // did you mean hw1();
break;
|
Look in the reference section at the top left of this page to see how to use
std::cout
to output the value of a variable.
The
case
's don't call a function. The hw1() and similar functions are declared to take a
string
as an argument, but the definitions have no arguments, so change the declarations to match the definitions.
The normal idiom for a for loop is this:
1 2
|
for ( i = 0; i <= MAX - 1; i++ )
for ( i = 0; i < MAX ; i++ )
|
The use of the
<=
operator can be error prone, mainly causing it to go 1 more than necessary - this is a problem with arrays. Using
<
is better than having to remember to subtract 1.
Try to come up with good variable names.
a
is not a good name for an array, and I would prefer
CurrentValue
over
value
as examples.
Good variable and function names serve as self documenting code, aid understanding - you are not the only one reading your code :+)
I hope you find all this useful - hopefully leads to better marks for your assignment :+D