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.
Printable View
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
THANX 4 your help i'll do that now!
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
I am just doing it as a hobby (in my spare time).
Please would you have a look at this code for me when i try to compile it i keep getting the message "[Linker error]undefined reference to 'mult (int,int),[Linked error]undefined reference to 'divi (int,int)' etc...."
Code:#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
int selection;
int mult (int a,int b);
int divi (int c,int d);
int add (int e,int f);
int subt (int g,int h);
int main()
{
cout<<"1. Multiplication";
cout<<"2. Division";
cout<<"3. Addition";
cout<<"4. Subtraction";
cout<<"5. Exit";
cin>> selection;
cin.ignore();
switch (selection)
{
case 1:
int mult (int a,int b);
int a;
int b;
{
cout<<"Please input two numbers to be multiplied: ";
cin>> a >> b;
cin.ignore();
cout<<"The product of your two numbers is "<< mult (a,b) <<".\n";
cin.get();
}
int mult (int a,int b);
{
return a*b;
}
break;
case 2:
int divi (int c,int d);
int c;
int d;
{
cout<<"Please input two numbers to be divided: ";
cin>> c >> d;
cin.ignore();
cout<<"The product of your two numbers is "<< divi (c,d) <<".\n";
cin.get();
}
int divi (int c,int d);
{
return c/d;
}
break;
case 3:
int add (int e,int f);
int e;
int f;
{
cout<<"Please input two numbers to be added: ";
cin>> e >> f;
cin.ignore();
cout<<"The product of your two numbers is "<< add (e,f) <<".\n";
cin.get();
}
int add (int e,int f);
{
return e+f;
}
break;
case 4:
int subt (int g,int h);
int g;
int h;
{
cout<<"Please input two numbers to be subtracted: ";
cin>> g >> h;
cin.ignore();
cout<<"The product of your two numbers is "<< subt (g,h) <<".\n";
cin.get();
}
int subt (int g,int h);
{
return g-h;
}
break;
case 5:
return 0;
}
}
Code:
#include <iostream>
using namespace std;
int mult(int a, int b);
int main(){
int selection;
int num1;
int num2;
//prompt user for selection
//read user input
//prompt user for numbers
//read user input
switch(selection){
case 0:
cout << "The answer is: " << mult(num1,num2) << "\n";
break;
}
return 0;
}
int mult(int a, int b){
return (a*b);
}
I recommend you declare just 2 ints, a and b, and do so just after the opening braces in main(). Then in each case option obtain user input into a and b and pass a and b to the desired funtion. Function calls are made by typing the name of the function and the parameters to be sent, such as add(a, b); or subt(a, b). A function call should not have any type information in it like the declaration and first line of the definition need.
I tried it with just the two intergers before and i just kept getting an error whenever o tried to compile the source code.
Here's your latest post of code with a few comments outlining
what you did wrong:
Now here is your corrected code - this compiles now:Code:#include <iostream>
//Dont need these 3 because you dont use any functions from them
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
int selection; //You should declare this inside main
int mult (int a,int b);
int divi (int c,int d);
int add (int e,int f);
int subt (int g,int h);
int main()
{
cout<<"1. Multiplication"; //These are probably printing all on the same line - use cout << endl
cout<<"2. Division"; //to move to next line
cout<<"3. Addition";
cout<<"4. Subtraction";
cout<<"5. Exit";
cin>> selection;
cin.ignore();
switch (selection)
{ //Watch your braces - its a style issue - not critical
case 1:
int mult (int a,int b); //Here is your problem - the stuff that
int a; //the function is supposed to do
int b; //shouldn't be here - it should
{ //be defined outside main
cout<<"Please input two numbers to be multiplied: ";
cin>> a >> b;
cin.ignore();
cout<<"The product of your two numbers is "<< mult (a,b) <<".\n";
cin.get();
}
int mult (int a,int b);
{
return a*b;
}
break;
case 2:
int divi (int c,int d); //As above
int c;
int d;
{
cout<<"Please input two numbers to be divided: ";
cin>> c >> d;
cin.ignore();
cout<<"The product of your two numbers is "<< divi (c,d) <<".\n";
cin.get();
}
int divi (int c,int d);
{
return c/d;
}
break;
case 3:
int add (int e,int f); //As above
int e;
int f;
{
cout<<"Please input two numbers to be added: ";
cin>> e >> f;
cin.ignore();
cout<<"The product of your two numbers is "<< add (e,f) <<".\n";
cin.get();
}
int add (int e,int f);
{
return e+f;
}
break;
case 4:
int subt (int g,int h); //Once again, as above
int g;
int h;
{
cout<<"Please input two numbers to be subtracted: ";
cin>> g >> h;
cin.ignore();
cout<<"The product of your two numbers is "<< subt (g,h) <<".\n";
cin.get();
}
int subt (int g,int h);
{
return g-h;
}
break;
case 5:
return 0;
}
}
Hope that this clarifies it for you, sorry about the long postCode:#include <iostream>
using namespace std;
int mult (int a,int b);
int divi (int a,int b);
int add (int a,int b);
int subt (int a,int b);
int main()
{
int selection;
int a; //You won't need different variables for each function
int b; //These to are used for all operations
cout<<"1. Multiplication" << endl;
cout<<"2. Division" << endl;
cout<<"3. Addition" << endl;
cout<<"4. Subtraction" << endl;
cout<<"5. Exit" << endl;
cin>> selection;
cin.ignore();
switch (selection)
{
case 1:
cout<<"Please input two numbers to be multiplied: ";
cin>> a >> b;
cin.ignore();
cout<<"The product of your two numbers is "<< mult (a,b) <<".\n";
cin.get();
break;
case 2:
cout<<"Please input two numbers to be divided: ";
cin>> a >> b;
cin.ignore();
cout<<"The product of your two numbers is "<< divi (a,b) <<".\n";
cin.get();
break;
case 3:
cout<<"Please input two numbers to be added: ";
cin>> a >> b;
cin.ignore();
cout<<"The product of your two numbers is "<< add (a,b) <<".\n";
cin.get();
break;
case 4:
cout<<"Please input two numbers to be subtracted: ";
cin>> a >> b;
cin.ignore();
cout<<"The product of your two numbers is "<< subt (a,b) <<".\n";
cin.get();
break;
case 5:
return 0;
}
}
//now define what the functions do
int mult (int a,int b)
{
return a*b;
}
int divi (int a,int b)
{
return a/b;
}
int add (int a,int b)
{
return a+b;
}
int subt (int a,int b)
{
return a-b;
}
Thanks sooooo much! That prog. has caused me nothing but problems but thanks to you the things that i did wrong have been clarified thanx to you!
Is there any way i can loop it so that it goes back to the menu after each calculation?
Yes, very easily you'll be glad to know. the most convenient
way is a while loop - as shown below. It is very important
to initialise the variable "selection" in circumstances such as these
because as you may or may not know, variables are not
automatically set to zero when you declare them, they have an
undefined value, which is usually the value last stored in the
particular piece of memory - which if that happened to be the
same as the value that the whle loop checks for, then your
program would just exit immediately.
Now i'll briefly explain how this loop works - the while keyword
is a lot like the if keyword - you enter a logical expression into
the parentheses, and if the expression evaluates to true, the
program runs the code inside the braces. if it is false, it skips that
block of code. the condition is checked at the start of each loop,
so that the loop does not exit the moment that the expression
becomes false.
in the code below, the while loop ckecks to see whether the
value stored in selection is 5. if it is not 5, the code inside
the loop is run - allowing the user to make a selection. if they
choose 5, then the switch statement jumps to the label "case 5:"
but the oply thing contained there is a break statement which
exits the switch statement. then the loop itself comes to an
end (there is no code between the end of the switch statement
and the end of the looping block), so the program performs
the while loop check. selection is still 5, so the expression
evaluates to false (it is true for any value except 5, remember?)
and the program skips the code block for the loop. the next line
of code encountered is return 0, so the program exits.
Code:int main()
{
int selection = 0; //Important to initialise this
int a; //for the loop to work properly
int b;
while (selection != 5)
{
cout<<"1. Multiplication" << endl;
cout<<"2. Division" << endl;
cout<<"3. Addition" << endl;
cout<<"4. Subtraction" << endl;
cout<<"5. Exit" << endl;
cin>> selection;
cin.ignore();
switch (selection)
{
.
.
.
.
case 5:
break;
}
}
return 0;
}
Thanx for all of your help my project is now completely done!
By the way how do i clear the screen of any previous text?
Thanx all sorted.