hi all I am a beginner to C++ and I need some advice for my programme

#include <iostream>
#include<string>
using namespace std;
int main(int argc, char** argv)
{
do
{
char choice;
cout<<"Menu"
cout<<"A.Information"<<endl;
cout<<"B.Question"<<endl;
cout<<endl<<"Your Choice: ";
cin>>choice;

switch(choice)
{
case 'A':
{
system("cls");
cout<<"All the information";
break;
}
case'B'
{
system("cls");
cout<<"All the question"
break;
default:
{
cout<<"Please enter A or B";
break;
}
}
cout<<"Enter 1 to back to menu or Enter 2 to exit";
cin>>x;
}while(x==1);
=>how Can I return to the menu?<=
=>it also say x is not declared<=
=>is it possible to use continue; in this situation?<=


return 0;
}
Last edited on
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
#include <iostream>
#include<string>
using namespace std;
int main()
{
    char choice{' '};
    
    while (toupper(choice) != 'Q' )
    {
        
        cout<<"Menu\n";
        cout<<"A.Information"<<endl;
        cout<<"B.Question"<<endl;
        cout<<"Q.Quit"<<endl;
        
        cout<<endl<<"Your Choice: ";
        cin>>choice;
        
        switch(toupper(choice) )
        {
            case 'A':
            {
                cout<<"All the information\n";
                break;
            }
            case'B':
            {
                cout<<"All the question\n";
                break;
            }
            default:
            {
                cout<<"Please enter A, B or Q\n";
                break;
            }
        }
    }
    
    return 0;
}
Learn to use code tags. Here is your code with some changes, I left some comments so you could see what was wrong:


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
#include <iostream>
#include<string>

using namespace std;

int main(int argc, char** argv)
{
	//You had no variable "x", I created it here:
	int x;

	do
	{
		char choice;
		cout << "Menu"; // You were missing the semicolon
		cout << "A.Information" << endl;
		cout << "B.Question" << endl;
		cout << endl << "Your Choice: ";
		cin >> choice;

		switch (choice)
		{
		case 'A':
		//{ Don't use brackets for individual cases in a switch
			system("cls");
			cout << "All the information";
			break;
		//}
		case'B': //Missing Colon Here
		//{
			system("cls");
			cout << "All the question"; //Missing semicolon
			break;
		default:
		//{
			cout << "Please enter A or B";
			break;
		//}
		}

		cout << "Enter 1 to back to menu or Enter 2 to exit";
		cin >> x;
	}while (x == 1);

	//2 slashes are for a single line comment

	//This is how to make a multiline comment:
	/*
	=> how Can I return to the menu ? <=
	=> it also say x is not declared <=
	=> is it possible to use continue; in this situation ? <=
	*/
	
	return 0;
}
Last edited on

zapshe wrote:
//{ Don't use brackets for individual cases in a switch


Why not?

Why not?

They're not needed, just creates a scope for every case - brings no use to him. He didn't properly match the brackets on case 'B'. By making this as simple as possible it makes learning it easier.

A better thing to have said would be, "not needed", but no one's life will change with this minor change.


I forgot to mention that using system() is bad practice.
Here is your fixed code i add some change a little bit

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
#include <iostream>
#include<string>
using namespace std;
int main(int argc, char** argv)
{
int x;
do
{
char choice;
cout<<"Menu"<<endl;
cout<<"A.Information"<<endl;
cout<<"B.Question"<<endl;
cout<<endl<<"Your Choice: ";
cin>>choice;


switch(choice)
{
case 'A':
{
system("cls");
cout<<"All the information"<<endl;
break;
}
case 'B':
{
system("cls");
cout<<"All the question"<<endl;
break;
}
default:
{
cout<<"Please enter A or B";
break;
}
}
cout<<"Enter 1 to back to menu or Enter 2 to exit"<<endl;
cout<<"your choice = "; cin>>x;



}while( x == 1 );
return 0;
}
It would be more usual for Quit to be an option on the menu - rather than a separate prompt.

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
#include <iostream>
#include <cctype>

int main() {
	for (char choice {}; choice != 'Q'; ) {
		std::cout << "\nMenu\n"
			<< "A. Information\n"
			<< "B. Question\n"
			<< "Q. Quit\n"
			<< "\nYour Choice: ";

		std::cin >> choice;

		choice = static_cast<char>(std::toupper(static_cast<unsigned char>(choice)));

		switch (choice) {
			case 'A':
				//system("cls");
				std::cout << "All the information\n";
				break;

			case 'B':
				//system("cls");
				std::cout << "All the question\n";
				break;

			case 'Q':
				break;

			default:
				std::cout << "Please enter A, B or Q\n";
				break;
		}
	}
}

Topic archived. No new replies allowed.