help with functions.

V

vinhkan

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=hotdog\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;
}
 
A

Andrew Heath

vinhkan said:
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=hotdog\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 << "";
|
You've got an empty string here ---------------+
which is not going to output anything. Change to ' '. (with a space)
 
J

Jonathan Mcdougall

just learning c++.

Get a good book (and throw away the one you are using right now).
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.
/* This is my main menu program */

Good to know.

Too many comments is like not enough.
#include <iostream.h>

Non standard :

# include <iostream>

using std::cout;
using std::cin;
using std::endl;
//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 ;

Do you think it makes sense to have an integer variable called 'name' ?
Make up _good_ names for your variables.

cout
<<"1=burger\n"<<"2=fries\n"<<"3=frappe\n"<<"4=hotdog\n"<<"5=icecream\n"<<"6=
coke\n"<<"7=pepsi\n"<<
endl;

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;
cin >> ws;

What is 'ws' ?
cin >> name;

What if the user enters letters or garbage?
if (name==1)

Here would be a good time to make a switch :

switch ( name )
{
case 1 :
{
cent(32);
cout << burger();
break;
}

case 2 :
{
fries();
break;
}

// ...

};

int cent(int len)
{
int j;

j = 39 - (len/2);

Not portable .. :)
for(int i = 1; i < (j+1); i++)

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.
cout << "";

And to get to your problem, this outputs nothing on the screen. What you
want is

cout << " ";
return 0;

What is that return value for ?


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top