Programming C++

Pages: 12
closed account (jyTM4iN6)
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
The link is not working .

Can you post how I could create a menu in my programm?
closed account (jyTM4iN6)
1
2
3
4
5
6
7
8
9
10
11
12
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 


Last edited on
When I put your code in my programm it doesnt work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace 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;
}
    

How can I change it that it works?
closed account (jyTM4iN6)
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace 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.

Last edited on
closed account (jyTM4iN6)
line 16, instead of void inputValue(int &a, int &b)
its void inputvalue(int& a, int& b)

line 62 whats that for?

line 73 variable not declared

line 91 ??????

line 100 i think there isnt any option exit(0) , just "cout" a goodbye msg

switch line 106 with 107

and work on your indentations
Last edited on
How should I declare the variabel in line 73 ? Sorry no idea?

And what should I do with line 100?
Ok here is my new code now , but it has still some mistakes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace 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) ===|


Can you please help me to solve this problems?

Can somebody please correct my programm?
for system you need to #include <cstdlib>

http://www.cplusplus.com/reference/cstdlib/system/

opt:
int opt;
Now the code looks so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <cstdlib> 
using namespace 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'




Oh hell how can I fix this?


cout << "bye">> endl;;

One of these << is not like the other.
Oh hell how can I fix this?
thinking?

cout << "bye">> endl; // as you can see there's a >> after "bye"

cout doesn't support >> so:
cout << "bye" << endl;


you should consider to use only one output language. Otherwise one could think that's not all yours

Btw on line 105: "wrong typed:" and then? nothing?
Topic archived. No new replies allowed.
Pages: 12