Feb 4, 2022 at 1:22pm UTC
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 Feb 5, 2022 at 1:47am UTC