I am new to C++ and would like to make a menu in which the user will input a number and then depending on the number they type a second program will open.
I am new to C++ and would like to make a menu in which the user will input a number and then depending on the number they type a second program will open.
Post the code you have and state the problems you are having achieving your task.
The best way to achieve this would to use a case select statement, or an if statement, but case select is much neater Assign a variable of int data type (Ex. int selection) and in the case select put your code that assigns to what number. Hope this helps!
DONT LAUGH AT ME I SAID I WAS NEW TO THIS (thats if its drastically wrong)!Code:#include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> int v; int w; int x; int y; int z; int main() { cout<<"1. Addition"; cout<<"2. Subtraction"; cout<<"3. Multiplication"; cout<<"4. Division"; cout<<"5. Exit"; cin>> v >> w >> x >> y >> z; cin.ignore(); switch (menu) case 1: char a[]; char b[]; if (v ==1) char a[] = "Add.exe"; char b [BFRSIZ]; system ("Add.exe"); system (a); strcpy (b, "Add.exe"); strcat (b, " -aparm -b"); printf ("Executing %s\n", b); system (b); return 0; break; case 2: if (v==2) return 0; break; case 3: if (v==3) return 0; break; case 4: if (v==4) return 0; break; case 5: if (v==5) return 0; break; }
You need to declare a variable of int. Such as, int selection, and for your cin statement, just put cin >> variablename. Variable name should be replaced with the name you chose of the int data type. In the case statements, call your functions. I think you might need to use file i/o to open other programs. Why would you want to do that anyway? A function will do just fine, for whatever you want to achieve, unless opening a program is your goal.
EDIT: Your headers should be changed to string, cstdlib, and cstdio
I need help again (I dont believe this). Could you please have a look at my code and tell me whats the matter with it as i cant compile:
Code:#include <iostream> #include <cstdio> #include <cstdlib> #include <string> using namespace std; int selection; int mult (int x,int y); int divi (int x,int y); int add (int x,int y); int subt (int x,int y); int main() { cout<<"1. Addition"; cout<<"2. Subtraction"; cout<<"3. Multiplication"; cout<<"4. Division"; cout<<"5. Exit"; cin>> selection; cin.ignore(); switch (selection) case 1: int mult (int x,int y); int x; int y; cout<<"Please input two numbers to be multiplied: "; cin>> x >> y; cin.ignore(); cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n"; cin.get(); } int mult ( int x, int y ) { return x * y; } break; case 2: int divi (int x,int y); int x; int y; cout<<"Please input two numbers to be divided:"; cin>> x >> y; cin.ignore(); cout<<"The product of your two numbers is "<< divi (x,y) <<".\n"; cin.get(); } int divi (int x,int y) { return x/y; } break; }
Forgot to mention it gets as far asCode:int mult ( int x, int y ) { return x * y; } it gets to here and i get an error------>BREAK;
That is because you declare two functions of the same name. Also, you forgot a "{" after switch. You also have alot of other errors. Sometimes it's better just to start over, then going through the code rooting out errors.
Thanx for the advise i might try that. But first i'll try solve it!
Why have you used global variables?
could you show me where i have to add the { after switch please?
I think it is liek this:
Edit: Would you not also be best defining your functions outside of main to make your program clearer.Code:switch (selection) { case 1: int mult (int x,int y); int x; int y; cout<<"Please input two numbers to be multiplied: "; cin>> x >> y; cin.ignore(); cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n"; cin.get(); } int mult ( int x, int y ) { return x * y; } break; case 2: int divi (int x,int y); int x; int y; cout<<"Please input two numbers to be divided:"; cin>> x >> y; cin.ignore(); cout<<"The product of your two numbers is "<< divi (x,y) <<".\n"; cin.get(); } int divi (int x,int y) { return x/y; } break; }
Yes, it would be better to declare them from outside of main. Or you could just not do anything in main. But remember it returns 0; even if there is nothing in it. All you would have to do is make a menu() function, and call that in main. Or you could just do int main(void). There are many possibilities in programming, so don't get scared of making a mistake, or not doing something right. If it runs and does what you want it to do then you are fine, although it may not be the best technique, it doesnt really matter.
Well it probably would if he wants to get into big time programming