Menu option that prints the inserted inputs

I am writing a simple code that solves a systeam of two linear equations inserted from a user.
One of the options of the Menu is to print the inserted equations to check if the inputs are correct. While this is easy to setup, I am having a hard time figuring out how can i show an error message and go back to the menu, in case one or none of the equations were insrted.

Here's what I tried so far.

[/code]
#include<iostream>
#include<string>

using namespace std;

int main() {

menu:
cout << "Menu" << endl;
cout << "1 - Insert first equation" << endl;
cout << "2 - Insert second equation" << endl;
cout << "3 - Print the system" << endl;
cout << "4 - Solve the system" << endl;
cout << "0 - Exit" << endl;

char menu_number;
cin >> menu_number;

switch (menu_number)
{

case '1': {

cout << "1 - Insert first equation" << endl;
string equation1;
cin >> equation1;

break;
}

case '2': {

cout << "2 - Insert second equation" << endl;
string equation2;
cin >> equation2;

break;
}


case '3': {

cout << "3 - print the system" << endl;

string equation1;
string equation2;

if ( (equation1) || (equation2) ){

cout << equation1 << endl;
cout << equation2 << endl;

}

else if (equaqtion1) {

cout << "Insert second equation" << endl;

}

else if (equation2) {

cout << "Insert second equation" << endl;

}

else {

cout << "Insert both equations" << endl;

}

break;
}

case '0': {

exit(0);

}

default: {

cout << "Select a number from the list" << endl;

}

}

goto menu;

}

[\code]

I also tried to use a int variable (eq1 and eq2) to check whether or not the inputs were inserted:

[/code]
#include<iostream>
#include<string>

using namespace std;

int main() {

int eq1 = 0;
int eq2 = 0;

menu:
cout << "Menu" << endl;
cout << "1 - Inserire la prima equazione" << endl;
cout << "2 - Inserire la seconda equazione" << endl;
cout << "3 - Stampare il sistema" << endl;
cout << "4 - Risolvere il sistema" << endl;
cout << "0 - Esci" << endl;

char menu_number;
cin >> menu_number;

switch (menu_number)
{

case '1': {

cout << "1 - Inserire la prima equazione" << endl;
int eq1 = 1;
string equation1;
cin >> equation1;

break;
}

case '2': {

cout << "2 - Inserire la seconda equazione" << endl;
int eq2 = 1;
string equation2;
cin >> equation2;

break;
}


case '3': {

cout << "3 - Stampare il sistema" << endl;

if ( (eq1==1) || (eq2==1) ){

string equation1;
string equation2;
cout << equation1 << endl;
cout << equation2 << endl;

}

else if (eq1==1) {

cout << "Inserire la seconda equazione" << endl;

}

else if (eq2==1) {

cout << "Inserire la prima equazione" << endl;

}

else {



}

break;
}

case '0': {

exit(0);

}

default: {

cout << "Seleziona un numero della lista" << endl;

}

}

goto menu;

}

[\code]

This is mmyy first project in C++ and I'm very new to the language. How can I face this issue? thanks
Last edited on
https://www.cplusplus.com/articles/jEywvCM9/
You're nearly there, it's [/code] not [\code]
Perhaps something like:

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

int main() {
	char menu_number {};
	std::string equation1, equation2;

	do {
		std::cout << "Menu\n" <<
			"1 - Insert first equation\n" <<
			"2 - Insert second equation\n" <<
			"3 - Print the system\n" <<
			"4 - Solve the system\n" <<
			"0 - Exit\n" <<
			"Please enter option: ";

		std::cin >> menu_number;

		switch (menu_number) {
			case '1':
				std::cout << "1 - Insert first equation\n";
				std::cin >> equation1;
				break;

			case '2':
				std::cout << "2 - Insert second equation\n";
				std::cin >> equation2;
				break;

			case '3':
				std::cout << "3 - print the system\n";
				if (equation1.empty())
					std::cout << "Please use option 1 to enter equation 1\n";

				if (equation2.empty())
					std::cout << "Please use option 2 to enter equation 2\n";

				if (!equation1.empty() && !equation2.empty()) {
					std::cout << equation1 << '\n';
					std::cout << equation2 << '\n';
				}
				break;

			case '4':
				std::cout << "Not yet implemented!\n";
				break;

			case '0':
				break;

			default:
				std::cout << "Invalid input. Select a number from the list\n";
				break;
		}
	} while (menu_number != '0');
}

Topic archived. No new replies allowed.