patterncppMinor
"Favorite food" program
Viewed 0 times
favoritefoodprogram
Problem
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
Please let me know if you still see any "forbidden" code.
EDITED:
```
#include
#include
using std::cout;
using std::endl;
using std::string;
using std::cin;
int main () {
I'm asking because I initially put a lot of
gotos 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
#include
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
#include
using std::cout;
using std::endl;
using std::string;
using std::cin;
int main () {
Solution
Armond, the main thing you should do is to learn about functions. Putting everything in
So you would get something like:
Also note that
EDIT
To be honest, I like the edited version less than the original. You are now testing the
Here is how I might have coded what you are doing:
main is not practicable beyond very small programs. And nested loops are generally a bad idea too. So for example you might extract the content of the first loop into a function and extract the second loop into another function.So you would get something like:
int main(int argc, char **argv)
{
do {
user_interaction();
} while (user_says_exit() == false);
return 0;
}Also note that
while (true) {...} is more commonly used than do {... } while (true); although do{} while() loops clearly have their uses.EDIT
To be honest, I like the edited version less than the original. You are now testing the
y_n_decision as the condition in both loops. And you are relying on the variable having been default-initialized. Moreover the loops are now while instead of do-while and are arguably more wrong now than they were before. A while (true) loop is preferable to your original do ... while (true) because the condition (true) is immediately visible at the top of the loop. But the new loops using while (condition) rely upon the initial value of the condition - and in your case you have to force that to be false by using an initializer that is not y/q. This is at best inelegant.Here is how I might have coded what you are doing:
#include
#include
#include
using std::cout;
using std::cin;
using std::endl;
static bool user_says_exit(void)
{
std::string decision;
do {
cout << "Please press y to restart, or q to quit" << endl;
getline(cin, decision);
} while (decision != "y" && decision != "q");
return (decision != "y");
}
static void user_interaction(void)
{
std::string age;
std::string favfood;
std::string name;
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 << ", your fav food is "
<< favfood << " and your age is " << age << endl;
}
int main(int argc, char **argv)
{
do {
user_interaction();
} while (user_says_exit() == false);
cout << "Quitting now" << endl;
return 0;
}Code Snippets
int main(int argc, char **argv)
{
do {
user_interaction();
} while (user_says_exit() == false);
return 0;
}#include <iostream>
#include <string>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
static bool user_says_exit(void)
{
std::string decision;
do {
cout << "Please press y to restart, or q to quit" << endl;
getline(cin, decision);
} while (decision != "y" && decision != "q");
return (decision != "y");
}
static void user_interaction(void)
{
std::string age;
std::string favfood;
std::string name;
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 << ", your fav food is "
<< favfood << " and your age is " << age << endl;
}
int main(int argc, char **argv)
{
do {
user_interaction();
} while (user_says_exit() == false);
cout << "Quitting now" << endl;
return 0;
}Context
StackExchange Code Review Q#24301, answer score: 5
Revisions (0)
No revisions yet.