Connecting Tech Pros Worldwide Forums | Help | Site Map

help with functions.

vinhkan
Guest
 
Posts: n/a
#1: Jul 19 '05
just learning c++. 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.

thanks
Mark


/* This is my main menu program */


#include <iostream.h>

//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 ;




cout
<<"1=burger\n"<<"2=fries\n"<<"3=frappe\n"<<"4=hotd og\n"<<"5=icecream\n"<<"6=coke\n"<<"7=pepsi\n"<<
endl;
cin >> ws;
cin >> name;

if (name==1)
{

cent(32);cout<<burger();
}
else
if(name==2){

fries();}
else
if(name==3)
frappe();
else
if(name==4)
hotdog();
else
if(name==5)
icecream();
else
if(name==6)

coke();
else

if(name==7)

pepsi();
else

cout<<"Not on the menu!\n";




return 0;

}

void burger()
{


cout<<"Thank you for ordering a burger\n";

}

void fries()
{

cout<<"Thank you for ordering fries\n"<<endl;
}
void frappe()
{
cout<<"Thank you for ordering a
frappe\n";
}
void hotdog()
{
cout<<"Thank you for ordering a
hotdog\n";
}
void icecream()
{
cout<<"Thank you for ordering an
icecream\n";
}
void coke()
{
cout<<"Thank you for ordering a
coke\n";
}
void pepsi()
{
cout<<"Thank you for ordering a
pepsi\n";
}

int cent(int len)
{
int j;

j = 39 - (len/2);

for(int i = 1; i < (j+1); i++)

cout << "";

return 0;
}



Andrew Heath
Guest
 
Posts: n/a
#2: Jul 19 '05

re: help with functions.


vinhkan wrote:[color=blue]
> just learning c++. 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.
>
> thanks
> Mark
>
>
> /* This is my main menu program */
>
>
> #include <iostream.h>
>
> //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 ;
>
>
>
>
> cout
> <<"1=burger\n"<<"2=fries\n"<<"3=frappe\n"<<"4=hotd og\n"<<"5=icecream\n"<<"6=coke\n"<<"7=pepsi\n"<<
> endl;
> cin >> ws;
> cin >> name;
>
> if (name==1)
> {
>
> cent(32);cout<<burger();
> }
> else
> if(name==2){
>
> fries();}
> else
> if(name==3)
> frappe();
> else
> if(name==4)
> hotdog();
> else
> if(name==5)
> icecream();
> else
> if(name==6)
>
> coke();
> else
>
> if(name==7)
>
> pepsi();
> else
>
> cout<<"Not on the menu!\n";
>
>
>
>
> return 0;
>
> }
>
> void burger()
> {
>
>
> cout<<"Thank you for ordering a burger\n";
>
> }
>
> void fries()
> {
>
> cout<<"Thank you for ordering fries\n"<<endl;
> }
> void frappe()
> {
> cout<<"Thank you for ordering a
> frappe\n";
> }
> void hotdog()
> {
> cout<<"Thank you for ordering a
> hotdog\n";
> }
> void icecream()
> {
> cout<<"Thank you for ordering an
> icecream\n";
> }
> void coke()
> {
> cout<<"Thank you for ordering a
> coke\n";
> }
> void pepsi()
> {
> cout<<"Thank you for ordering a
> pepsi\n";
> }
>
> int cent(int len)
> {
> int j;
>
> j = 39 - (len/2);
>
> for(int i = 1; i < (j+1); i++)
>
> cout << "";[/color]
|
You've got an empty string here ---------------+
which is not going to output anything. Change to ' '. (with a space)
[color=blue]
>
> return 0;
> }
>
>[/color]


Jonathan Mcdougall
Guest
 
Posts: n/a
#3: Jul 19 '05

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


Closed Thread