This is my first program written in C++. I chose to make this public in order to receive suggestions on further improvements and to know if I'm using any "forbidden" code.
I'm asking because I initially put a lot of goto
s in the program, but later found out that using goto
was "evil." Therefore, after a lot of trial and error, I removed them all and replaced them with a couple of do
-while
loops. I must say using do
-while
makes the final code much easier to read.
Please let me know if you still see any "forbidden" code.
#include <iostream>
#include <string>
using namespace std;
int main () {
string name, favfood, age, y_n_decision;
do {
cout << "welcome to my program in c++" << endl;
cout << "Please enter your name" << endl;
getline (cin, name);
cout << "enter your favourite food" << endl;
getline (cin, favfood);
cout << "awesome! " << favfood << " is my favourite food too" << endl;
cout << "enter your age" << endl;
getline (cin, age);
cout << " so your name is " << name << " and your fav food is "
<< favfood << " and your age is " << age << endl;
cout << "please press Y to restart, or Q to quit" << endl;
do {
getline (cin, y_n_decision);
if (y_n_decision == "q") {
cout << "quitting now" << endl;
return 0;
}
else if (y_n_decision == "y") {
cout << "nice! you typed y. Exiting current loop and returning to int main() aka the beginning." << endl;
break;
}
else {
cout << "you didn't type either y or q. YOU MUST PRESS Y OR Q OR I WILL PROMPT U FOREVER!!" << endl;
}
} while (y_n_decision == "y" || "n");
} while (y_n_decision == "y" || "n");
return 0;
}
EDITED:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::cin;
int main () {
string name, favfood, age;
string y_n_decision;
while (y_n_decision != "y" || y_n_decision != "q") {
cout << "welcome to my program in c++" << endl;
cout << "Please enter your name" << endl;
getline (cin, name);
cout << "enter your favourite food" << endl;
getline (cin, favfood);
cout << "awesome! " << favfood << " is my favourite food too" << endl;
cout << "enter your age" << endl;
getline (cin, age);
cout << " so your name is " << name << " and your fav food is "
<< favfood << " and your age is " << age << endl;
cout << "please press y to restart, or q to quit" << endl;
while (y_n_decision != "y" || y_n_decision != "q") {
getline (cin, y_n_decision);
if (y_n_decision == "q") {
cout << "quitting now" << endl;
return 0;
}
else if (y_n_decision == "y") {
cout << "nice! you typed y. Exiting current loop and returning to int main() aka the beginning." << endl;
break;
}
else {
cout << "you didn't type either y or q. YOU MUST PRESS y OR q OR I WILL PROMPT U FOREVER!!" << endl;
}
}
}
return 0;
}