so... if i am getting it write you need a menu that will ask the user which function to call?
well if thats the case, then this might give you "a road to walk on"
http://codepad.org/VhJQjAj
this is some parts of a similar program i had to write,
ofc i gave you only some parts, you have to write down your if/switch statement, and your own variables, btw the variable "choice" included in the code, is a character type
void userchoice()
{ // after calling the function "userchoice" it will print on the screen the menu of the users options
cout<<endl;
cout<<"Menu Menu Menu"<<endl;
cout<<"=================="<<endl;
cout<<"d: Deposit"<<endl;
cout<<"w: Withdraw"<<endl;
cout<<"s: Show total"<<endl;
cout<<"e: Empty"<<endl;
cout<<"q: Quit"<<endl;
cout<<"Please enter choice: ";
}
the above is for the function
and down below for the main program, btw as you can see the program is reading the choice inside the main program, and not in the function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
{
userchoice(); // calling the function user choice
cin>>choice; // reading/receiving the users choice
//checking if the users choice is valid, if its not we ask him to re-enter until he
get it right
while ((choice!='d') && (choice!='w') && (choice!='s') && (choice!='e') && (choice!='q'))
{
cout<<"Invalid choice, please re-enter: ";
cin>>choice;
}
cout<<endl;
}
while (choice!='q'); //once the user enter the choice 'q'(quit) the program terminates
#include <iostream>
usingnamespace std;
void inputValue(int &a, int &b)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cin>>a;
cin>>b;
}
void inputValue(int *pa, int *pb)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cout<<" A : ";
cin>>*pa;
cout<<endl<<" B : ";
cin>>*pb;
}
void outputValue(int a, int b)
{
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
}
void outputVlaue(int *pa, int *pb)
{
cout<<" Zeiger A ist = "<<pa<<endl;
cout<<" Zeiger B ist = "<<pb<<endl;
}
void swap(int *pa, int *pb)
{
int tmp;
tmp=*pa;
*pa=*pb;
*pb=tmp;
}
int main()
{
int a, b;
int *pa=&a;
int *pb=&b;
inputValue(a,b);
swap(pa,pb);
outputValue(a,b);
void userchoice()
{ // after calling the function "userchoice" it will print on the screen the menu of the users options
cout<<endl;
cout<<"Menu Menu Menu"<<endl;
cout<<"=================="<<endl;
cout<<"d: Deposit"<<endl;
cout<<"w: Withdraw"<<endl;
cout<<"s: Show total"<<endl;
cout<<"e: Empty"<<endl;
cout<<"q: Quit"<<endl;
cout<<"Please enter choice: ";
}
do
{
userchoice(); // calling the function user choice
cin>>choice; // reading/receiving the users choice
//checking if the users choice is valid, if its not we ask him to re-enter until he
get it right
while ((choice!='d') && (choice!='w') && (choice!='s') && (choice!='e') && (choice!='q'))
{
cout<<"Invalid choice, please re-enter: ";
cin>>choice;
}
cout<<endl;
}
while (choice!='q');
//return 0;
system("PAUSE");
//return EXIT_SUCCESS;
}
first of all you are using it wrong, so its just normal that it want work, and i gave you an example on how to use it, i didnt tell you, this is the code, think man, otherwise you will never learn, and i think you want to learn dont you? just believe in your self
#include <iostream>
usingnamespace std;
void inputValue(int &a, int &b)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cin>>a;
cin>>b;
}
void inputValue(int *pa, int *pb)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cout<<" A : ";
cin>>*pa;
cout<<endl<<" B : ";
cin>>*pb;
}
void outputValue(int a, int b)
{
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
}
void outputVlaue(int *pa, int *pb)
{
cout<<" Zeiger A ist = "<<pa<<endl;
cout<<" Zeiger B ist = "<<pb<<endl;
}
void swap(int *pa, int *pb)
{
int tmp;
tmp=*pa;
*pa=*pb;
*pb=tmp;
}
int main()
{
int a, b;
int *pa=&a;
int *pb=&b;
inputValue(a,b);
swap(pa,pb);
outputValue(a,b);
{
cout<<"Choose a operation:"<<endl;
cout<<endl;
cout<<"(1)- Output of A and B :"<<endl;
cout<<"(2) Print A and B:"<<endl;
cout<<"(3) Change A and B:"<<endl;
cout<<"(4) zeiger A and B"<<endl;
cout<<"(5) programm quit:"<<endl;
cin>>opt;
switch(opt)
{
case 1:
cout<<" geben sie A und B ein:"<<endl;
cout<<" A : ";
cin>>a;
cout<<endl<<" B : ";
cin>>b;
break;
case 2:
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
break;
case 3:
swap( pa, pb);
outputValue( a, b);
;
break;
case 4:
cout<<" Zeiger A ist = "<<*pa<<endl;
cout<<" Zeiger B ist = "<<*pb<<endl;
break;
case 5:
exit(0);
break;
default:
cout<<"wrong typed:"<<endl;
}
}
system("pause");}
Is this code a little better ?
Can please someone try to correct my mistakes if there are some.
#include <iostream>
usingnamespace std;
void inputValue(int& a, int& b)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cin>>a;
cin>>b;
}
void inputValue(int *pa, int *pb)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cout<<" A : ";
cin>>*pa;
cout<<endl<<" B : ";
cin>>*pb;
}
void outputValue(int a, int b)
{
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
}
void outputVlaue(int *pa, int *pb)
{
cout<<" Zeiger A ist = "<<pa<<endl;
cout<<" Zeiger B ist = "<<pb<<endl;
}
void swap(int *pa, int *pb)
{
int tmp;
tmp=*pa;
*pa=*pb;
*pb=tmp;
}
int main()
{
int a, b;
int *pa=&a;
int *pb=&b;
inputValue(a,b);
swap(pa,pb);
outputValue(a,b);
cout<<"Choose a operation:"<<endl;
cout<<endl;
cout<<"(1)- Output of A and B :"<<endl;
cout<<"(2) Print A and B:"<<endl;
cout<<"(3) Change A and B:"<<endl;
cout<<"(4) zeiger A and B"<<endl;
cout<<"(5) programm quit:"<<endl;
cin>>opt;
switch(opt)
{
case 1:
cout<<" geben sie A und B ein:"<<endl;
cout<<" A : ";
cin>>a;
cout<<endl<<" B : ";
cin>>b;
break;
case 2:
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
break;
case 3:
swap( pa, pb);
outputValue( a, b);
;
break;
case 4:
cout<<" Zeiger A ist = "<<*pa<<endl;
cout<<" Zeiger B ist = "<<*pb<<endl;
break;
case 5:
cout << "bye">> endl;;
break;
default:
cout<<"wrong typed:"<<endl;
}
system("pause");
}
But my code Blocks is still showing me errors:
C:\C++\|In function 'int main()':|
C:\C++|73|error: 'opt' was not declared in this scope|
C:\C++\|106|error: 'system' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 12 seconds) ===|
#include <iostream>
#include <cstdlib>
usingnamespace std;
void inputValue(int& a, int& b)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cin>>a;
cin>>b;
}
void inputValue(int *pa, int *pb)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cout<<" A : ";
cin>>*pa;
cout<<endl<<" B : ";
cin>>*pb;
}
void outputValue(int a, int b)
{
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
}
void outputVlaue(int *pa, int *pb)
{
cout<<" Zeiger A ist = "<<pa<<endl;
cout<<" Zeiger B ist = "<<pb<<endl;
}
void swap(int *pa, int *pb)
{
int tmp;
tmp=*pa;
*pa=*pb;
*pb=tmp;
}
int main()
{
int a, b;
int *pa=&a;
int *pb=&b;
inputValue(a,b);
swap(pa,pb);
outputValue(a,b);
cout<<"Choose a operation:"<<endl;
cout<<endl;
cout<<"(1)- Output of A and B :"<<endl;
cout<<"(2) Print A and B:"<<endl;
cout<<"(3) Change A and B:"<<endl;
cout<<"(4) zeiger A and B"<<endl;
cout<<"(5) programm quit:"<<endl;
int opt;
switch(opt)
{
case 1:
cout<<" geben sie A und B ein:"<<endl;
cout<<" A : ";
cin>>a;
cout<<endl<<" B : ";
cin>>b;
break;
case 2:
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
break;
case 3:
swap( pa, pb);
outputValue( a, b);
break;
case 4:
cout<<" Zeiger A ist = "<<*pa<<endl;
cout<<" Zeiger B ist = "<<*pb<<endl;
break;
case 5:
cout << "bye">> endl;;
break;
default:
cout<<"wrong typed:"<<endl;
}
system("pause");
}
But it still shows an error
C:main2.cpp In function `int main()':
101 C:main2.cpp no match for 'operator>>' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&std::cout)), ((const char*)"bye")) >> std::endl'