Hello. I would like a review of my attempt at one of Bazzy's beginner exercises.
Exercise
Write a program that ccontinues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.
★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)
#include <iostream>
usingnamespace std;
int main()
{
cout << "Loop-de-Loop!\n" << endl;
int number = 0;
int control = 1;
while (number != 5 && control != 11)
{
cout << "Enter a number other than 5: ";
while (! (cin >> number))
{
cin.clear();
cin.ignore(1000, '\n');
cout << "\nInvalid input.\n\nEnter a number other than 5: ";
}
if (number == 5)
{
cout << "\nHey! you weren't supposed to enter 5!\n";
if (control == 1)
{
cout << "\nYou have entered a number " << control << " time.\n"
<< "\nEnter any number other than " << control << ": ";
}
else
{
cout << "\nYou have entered a number " << control << " times.\n"
<< "\nEnter any number other than " << control << ": ";
}
int other_number = 0;
while (! (cin >> other_number))
{
cin.clear();
cin.ignore(1000, '\n');
cout << "\nInvalid input.\n\nEnter any number other than " << control << ": ";
}
if (other_number == control)
{
cout << "\nWrong! Go away!\n";
break;
}
else
{
cout << "\nWell done! Now go away!\n";
}
}
++control;
if (control == 11)
{
cout << "\nWow, you're more patient than I am, you win.\n";
}
}
return 0;
}
As well as any general tips and advice, I would appreciate if someone could show me how to make this a function:
1 2 3 4 5 6 7
while (! (cin >> number))
{
cin.clear();
cin.ignore(1000, '\n');
cout << "\nInvalid input.\n\nEnter a number other than 5: ";
}
I would really like help with this, as I would like to know if there are better ways of accomplishing the task set out in the exercise or if my code style needs refinement. Also, it would be nice to know how to make the input error checker a function.
I would appreciate if someone could show me how to make this a function
Maybe
1 2 3 4 5 6 7 8 9 10 11 12 13
int getNum()
{
int number = 0;
do {
cout << "Enter a number other than 5: ";
if (cin >> number) {
return number;
}
cin.clear();
cin.ignore(1000, '\n');
cout << "\nInvalid input.\n\n";
} while (true);
}
As for the rest of it, maybe:
1 2 3 4 5 6 7 8 9 10 11 12 13
int number = 0;
for (int i=0; i<10; ++i) { // This is a very common way of looping 10 times
number = getNum();
if (number == 5) { // I prefer putting braces on the same line to reduce
// the amount of whitespace.
cout << "\nHey! you weren't supposed to enter 5!\n";
return 0; // The assignment says to return on this condition.
}
}
cout << "\nWow, you're more patient than I am, you win.\n";
return 0;
I think the third part of the assignment is asking you to change the program so that it no longer asks for a number other than 5. In that case, my code changes to:
int getNum(int badNum)
{
int number = 0;
do {
cout << "Enter a number other than " << badNum << endl;
if (cin >> number) {
return number;
}
cin.clear();
cin.ignore(1000, '\n');
cout << "\nInvalid input.\n\n";
} while (true);
}
int main()
{
int number = 0;
for (int i=0; i<10; ++i) { // This is a very common way of looping 10 times
number = getNum(i);
if (number == i) {
cout << "\nHey! you weren't supposed to enter " << i << "!\n";
return 0;
}
}
cout << "\nWow, you're more patient than I am, you win.\n";
return 0;
}
Notice that getNum() doesn't check if the number is bad. One function should do one thing.