| re: help with functions.
> just learning c++.
Get a good book (and throw away the one you are using right now).
[color=blue]
>trying out this small routine to demonstrate how to
> center text on the screen. It's a function called "int cent(int)"
> doesn't work for me. could you take a look for me.[/color]
[color=blue]
> /* This is my main menu program */[/color]
Good to know.
Too many comments is like not enough.
[color=blue]
>
> #include <iostream.h>[/color]
Non standard :
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
[color=blue]
> //FUNCTION PROTOTYPES
>
> int cent(int );
> void burger();
> void fries();
> void frappe();
> void hotdog();
> void icecream();
> void coke();
> void pepsi();
>
> //BEGIN MAIN
>
> int main()
> {
>
> //int cent(int );
> int name = 0 ;[/color]
Do you think it makes sense to have an integer variable called 'name' ?
Make up _good_ names for your variables.
[color=blue]
> cout
>[/color]
<<"1=burger\n"<<"2=fries\n"<<"3=frappe\n"<<"4=hotd og\n"<<"5=icecream\n"<<"6=
coke\n"<<"7=pepsi\n"<<[color=blue]
> endl;[/color]
Try to indent your code better next time :
cout << "1 = burger\n"
<< "2 = fries\n"
<< "3 = frappe\n"
<< "4 = hotdog\n"
<< "5 = icecream\n"
<< "6 = coke\n"
<< "7 = pepsi\n"
<< endl;
[color=blue]
> cin >> ws;[/color]
What is 'ws' ?
[color=blue]
> cin >> name;[/color]
What if the user enters letters or garbage?
[color=blue]
> if (name==1)[/color]
Here would be a good time to make a switch :
switch ( name )
{
case 1 :
{
cent(32);
cout << burger();
break;
}
case 2 :
{
fries();
break;
}
// ...
};
<snip>
[color=blue]
> int cent(int len)
> {
> int j;
>
> j = 39 - (len/2);[/color]
Not portable .. :)
[color=blue]
>
> for(int i = 1; i < (j+1); i++)[/color]
two things : 1) increment 'j' before the loop instead of adding one on each
turn and 2) prefer ++i when you are not using the return value.
[color=blue]
> cout << "";[/color]
And to get to your problem, this outputs nothing on the screen. What you
want is
cout << " ";
[color=blue]
> return 0;[/color]
What is that return value for ?
Jonathan |