I have a project due for my computer programming class on Monday, and though I've completed the majority of the program, I now have to loop it and have no idea how. Here is the program:

#include<iostream.h>


int add ();
int subtract ();
int multiply ();
int division ();
void menu ();


int main ()


{

menu ();


return 0;
}

int add ()
{
int x, y;
cout<<"So! You want to add. Cool.\n";
cout<<"enter two numbers.\n";
cin>>x>>y;
return (x+y);
}

int subtract ()
{

int x, y;
cout<<"You want to subtract? Stellar!\n";
cout<<"Please enter two numbers.\n";
cin>>x>>y;
return (x - y);
}

int multiply ()
{
int x, y;
cout<<"Multiplication is my fave!\n";
cout<<"Please enter two numbers.\n";
cin>>x>>y;
return (x * y);
}

int division ()
{
int x, y;
cout<<"Bleh, division. So tough, right?! Lucky the computer will be doing it for us!\n\n";
cout<<"Please enter two numbers!\n";
cin>>x>>y;
return (x/y);

Help would be greatly appreciated. I have to loop it so that it never really ends. I know that I should probably use a while loop, but I have no idea what the conditions would be. Thankyou!

Use a while loop around the text of the main()

while(1)
{
menu();
}

You haven't included the menu function, so I can't test it for sure. I changed the menu function in the main to multiply, and i created an infinite loop where the computer kept asking for two numbers to multiply.
I assume you want to do this with the menu function. I hope that helps and is actually what you were looking for ;)

Also, when posting your code for us to look at, please use code tags:

[code]

// Your code goes here

[/code]

It makes the code MUCH easier to read. (There are several people that won't even look at your problem unless you use code tags.)

ahhh, i'm so sorry, i forgot to include the end of the program, haha.

#include<iostream.h>


int add ();
int subtract ();
int multiply ();
int division ();
void menu ();


int main ()


{
while(1)
{

menu ();

}

return 0;
}


int add ()
{
int x, y;
cout<<"So! You want to add. Cool.\n";
cout<<"enter two numbers.\n";
cin>>x>>y;
return (x+y);
}

int subtract ()
{

int x, y;
cout<<"You want to subtract? Stellar!\n";
cout<<"Please enter two numbers.\n";
cin>>x>>y;
return (x - y);
}

int multiply ()
{
int x, y;
cout<<"Multiplication is my fave!\n";
cout<<"Please enter two numbers.\n";
cin>>x>>y;
return (x * y);
}

int division ()
{
int x, y;
cout<<"Bleh, division. So tough, right?! Lucky the computer will be doing it for us!\n\n";
cout<<"Please enter two numbers!\n";
cin>>x>>y;
return (x/y);
}

void menu ()
{int x;
cout<<"Hello! Let's play a math game. Please choose one of the 5 below.\n\n";
cout<<" 1) Add\n 2) Subtract\n 3) Multiply\n 4) Divide\n 5) Quit\n\n";
cout<<"Press 1, 2, 3, 4. Have fun!\n\n";
cout<<"If you'd rather not play my math game, press 5. Jerk.\n\n";
cin>>x;


if (x == 1)
cout<<add();
cout<<endl;

if (x == 2)
cout<<subtract();
cout<<endl;

if (x == 3)
cout<<multiply();
cout<<endl;

if (x == 4)
cout<<division();
cout<<endl;

if (x == 5)
cout<<"You're quitting? Lamesauce. Toodles.\n";
cout<<endl;

}

Let me see if understand your problem correctly. You want to keep running the program until the user enters 5 at the menu to quit. Is that right? If that is indeed what you want to do, just include a while loop around your menu function.

while( x!=5)
{
cout<<"Hello! Let's play a math game. Please choose one of the 5 below.\n\n";
cout<<" 1) Add\n 2) Subtract\n 3) Multiply\n 4) Divide\n 5) Quit\n\n";
cout<<"Press 1, 2, 3, 4. Have fun!\n\n";
cout<<"If you'd rather not play my math game, press 5. Jerk.\n\n";
cin>>x;


if (x == 1)
cout<<add();
cout<<endl;

if (x == 2)
cout<<subtract();
cout<<endl;

if (x == 3)
cout<<multiply();
cout<<endl;

if (x == 4)
cout<<division();
cout<<endl;

if (x == 5)
cout<<"You're quitting? Lamesauce. Toodles.\n";
cout<<endl;

}

Let me know if that's what you are looking for.

I suggest you to change the return type of the menu() to int. If the user wants to continue return 1 and he doesn't wants to continue(that is, when he pressed 5), return 0

Then inside your main(), you can simply do like this:

int main(){
while( menu() );
}

Note that <iostream.h>is depracated header file, use <iostream> instead.

Also, post your code next time in code tags

Edit: the above post by Grn Xtrm is also another way to tackle this.

Edited 3 Years Ago by happygeek: fixed formatting

and you forgot the [code]

[/code] around your code so that it is highlighted and has line numbers.

(oops didn't see the previous post -- sorry)

Edited 6 Years Ago by Murtan: oops

This article has been dead for over six months. Start a new discussion instead.