# program question

This topic is 3940 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

well right now im trying to make a program that encorporates everything ive learned so far its not finished i havented finished everything and done loops. i have a problem when running it though. i click 1 for addition type in my numbers it does the math displays it then i hit enter again and it does the multiplication for the other function. i dont know why its doing this. any help would be greatly appreciated. thanks hey heres the program so far ive only done multiplication and addition
#include <iostream>

using namespace std;

int multiply(int a, int b);

main()
{
int choice;
int x;
int y;
int a;
int b;

cout<<"Would you like me to do multiplication, division, addition, or subtraction for you?\n";
cout<<"Choose 1-4 from the following list.\n";
cout<<"2. Subtraction\n";
cout<<"3. Multiplication\n";
cout<<"4. Division\n";
cin>> choice;

switch (choice) {
case 1:
cin>> x;
cin.ignore();
cout<<"Type in the second number.\n";
cin>> y;
cin.ignore();
break;
case 2:
/*code*/
break;
case 3:
cout<<"Type in your first number to be multiplied.\n";
cin>> a;
cin.ignore();
cout<<"Type in the second number.\n";
cin>> b;
cin.ignore();
int multiply(int a, int b);
break;
case 4:
/*code*/
break;
default:
/*code*/
break;
}

{
cout<<"When you add " << x <<" and " << y << " you get " << x + y << ".\n";
cin.get();
}

int multiply(int a, int b);
{

cout<<"When you multiply " << a <<" and " << b << " you get " << a * b << ".\n";
cin.get();
}

}



##### Share on other sites
1)You can't define functions inside other functions, and that includes main too
2)When you define functions,you don't put a semicolon after the signature
3)When you want to call a function "int addition(int,int)", you do it like: addition(a,b). Not the way you do it.

In other words, this crazy piece of code(it's actually kinda amazing because it compiles due to several mistakes cancelling each other out) is not even remotely close to what you think it should do. I'm too bored to even explain how far it is. Just reread the stuff I mentioned earlier.

##### Share on other sites
Quote:
 Original post by mikeman1)You can't define functions inside other functions, and that includes main too2)When you define functions,you don't put a semicolon after the signature3)When you want to call a function "int addition(int,int)", you do it like: addition(a,b). Not the way you do it.In other words, this crazy piece of code(it's actually kinda amazing because it compiles due to several mistakes cancelling each other out) is not even remotely close to what you think it should do. I'm too bored to even explain how far it is. Just reread the stuff I mentioned earlier.

ok thank you for this information im gonna make the changes that you said and retry it. sorry for the messy code :P.

##### Share on other sites
Here is an attempt to fix your code. You should be able to see what I changed. If not, refer to mikeman's post...I don't have much time.
#include <iostream>using namespace std;int multiply(int a, int b);int addition(int x, int y);int main(){      int choice;      int x;      int y;      int a;      int b;                  cout<< "Would you like me to do multiplication, division, addition, or         subtraction for you?\n";      cout<< "Choose 1-4 from the following list.\n";      cout<< "1. Addition\n";      cout<< "2. Subtraction\n";      cout<< "3. Multiplication\n";      cout<< "4. Division\n";      cin>> choice;            switch (choice) {             case 1:               cout<< "Type in your first number to be added.\n";               cin<< x;               cin.ignore();               cout<< "Type in the second number.\n";               cin >> y;               cin.ignore();               addition(x,y); //No 'int' in the arguments. It knows x and y                               //are ints.  It just needs to know which                               //variables it is using. No return type if you                                   //are calling it either.                 break;             case 2:               /*code*/               break;             case 3:               cout<< "Type in your first number to be multiplied.\n";               cin >> a;               cin.ignore();               cout<< "Type in the second number.\n";               cin>> b;               cin.ignore();               multiply(a, b);               break;             case 4:               /*code*/               break;             default:               /*code*/               break;             }    return 0;}                                  int addition(int x, int y);{    cout<<"When you add "<< x <<" and "<< y << " you get " << x + y<< ".\n";            cin.get();}                                         int multiply(int a, int b);{    cout<< "When you multiply " << a << " and " << b << " you get " << a * b <<            ".\n";    cin.get();}

You should review functions and where to put them, how to call them, etc. Also, there is no reason why you can't just use x and y. You don't need a and b. Since you don't use them at the same time as x and y, you can use x and y.

##### Share on other sites
ok after your input i made changes and finished up the rest of the program hopefully my code is clearer now. anyone feel free to point out if im doing something improper or if i did something wrong. also i have one problem now that i have everything done. i used a do while loop to try and get the program to loop if the user selects a wrong number. when i run my program and click 5 exit it says what it should but then it still loops. for my do while loop i have it loop if choice is != 1,2,3,4, or 5. at first i thought it was because i did || for each then i just changed it to and so it has to not be all of them and it still loops when i select exit. here is the code.

thanks
hey

#include <iostream>using namespace std;int multiply(int a, int b);int addition(int x, int y);float division(float q, float t);int subtraction(int z, int c);                     main(){      int choice;      /* addition ints */      int x;      int y;      /* multiplication ints */      int a;      int b;      /* division ints */      float q;      float t;      /* subtraction ints */      int z;      int c;            do {      cout<<"Would you like me to do multiplication, division, addition, or subtraction for you?\n";      cout<<"Choose 1-5 from the following list.\n";      cout<<"1. Addition\n";      cout<<"2. Subtraction\n";      cout<<"3. Multiplication\n";      cout<<"4. Division\n";      cout<<"5. Exit\n";      cin>> choice;            switch (choice) {             case 1:             cout<<"You have selected Addition.\n";               cout<<"Type in the first number.\n";               cin>> x;               cin.ignore();               cout<<"Type in the second number.\n";               cin>> y;               cin.ignore();               cout<<"When you add " << x <<" and " << y << " you get " << addition(x, y) << ".\n";               cin.get();                                               break;                  case 2:                  cout<<"You have selected Subtraction.\n";                      cout<<"Type in the first number.\n";                      cin>> z;                      cin.ignore();                      cout<<"Type in the second number.\n";                      cin>> c;                      cin.ignore();                      cout<<"When you subtract " << z <<" from " << c << " you get " << subtraction(z, c) << ".\n";                      cin.get();                                                                   break;                       case 3:                            cout<<"You have selected Multiplication.\n";                            cout<<"Type in the first number.\n";                            cin>> a;                            cin.ignore();                            cout<<"Type in the second number.\n";                            cin>> b;                            cin.ignore();                                                        cout<<"When you multiply " << a <<" and " << b << " you get " << multiply(a, b) << ".\n";                                             break;                            case 4:                                 cout<<"You have selected Division.\n";                                 cout<<"Type in the first number.\n";                                 cin>> q;                                 cin.ignore();                                 cout<<"Type in the second number.\n";                                 cin>> t;                                 cin.ignore();                                                                  cout<<"When you divide " << q <<" by "<< t << " you get " << division(q, t) << ".\n";                                                                  break;                                                                  case 5:                                      cout<<"You have chosen to quit. Have a nice day and goodbye!\n";                                      cout<<"Hit enter one more time to exit.\n";                                      cin.get();                                      break;                                                                                                                                                                                       }                                         }                                         /* loop for if the user picks anything besides 1-4 */                                         while (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice  != 5);                                                                                  }                                                                                                                                             int addition(int x, int y)        {            return x + y;            }                                                 int multiply(int a, int b)        {            return a * b;            }                            float division(float q, float t)        {            return q / t;            }                   int subtraction(int z, int c)       {           return z - c;           }

ooh just saw your post bschneid right now im gonna look at that now.

EDIT: i made a dumb mistake i just saw on my while loop i deleted some of the != here is my while loop part now

while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5);

thats after the do part.

so now everything works pretty good but im curious how would i make the program loop after they do a multiplication or subtraction etc? right now it loops if you pick the wrong thing.
now also my exit works except it exits right away and doesnt delay and display the messages and wait for a return... how do i make it do this?

thanks hey

also i added a few random cin.get(); in some of the cases where i forgot them.

[Edited by - HeyHoHey on May 8, 2007 10:29:32 PM]