Hi there! Im a bit struggling with the loop conditions.
I need to write a basic program where the user is requested to enter a digit between 1-30 up to 5 times and display the same amount of * for the amount entered. Not counting the times they enter a incorrect number i.e. Above 30 or below 0.
The code below is what I have so far. The whole program does repeat 5 times and thats it.
How would I write it so that the program repeats 5 times only when correct numbers are entered and repeat unlimited or up to 50 times if incorrect numbers keep being put in.
I thought yo might have used a while loop, but the for loop works.
Right now you will input five numbers right or wrong.
I offer the following code for two reasons:
To show you the "j--" that will only count correct answers.
And to show hoe when the {}s line up in the same column it is easier to read and much easier to find a missing closing brace.
I changed the for condition so it would only work until "j" becomes less then zero causing the for loop to end because of to many incorrect choices. Otherwise it will loop until five correct numbers are added.
I will see what I can come up with using a while loop.
I was working with your code and have some suggestions:
Line 23 is a little misleading. It might work better as "Please enter a digit between 1-30 inclusive." and then the second if statement would need to be "< 1" to work properly. Also I would copy line 23 and paste it at line 14. It gives a better start to the output.
Your header files "iomanip' and "cmath" are not used at this time unless you have a use for them later.
In your if statements the numbers "30" and "0" I would do something like I did with "MAXTRIES", so they can easily be changed later if needed.
I found lines 40 and 41 did not work well when I first used your program. Generally I use this to pause the program before it ends:
1 2 3 4 5 6
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
Once you understand it the comments do not have to be there.
Just for fun I changed the for loop to a while loop. Give it a look and see what you think.
#include <iostream>
#include <string>
//#include <iomanip> // <--- Not used right now.
//#include <cmath> // <--- Not used right now.
usingnamespace std;
int main()
{
constexprint MAXTRIES{ 5 };
const std::string TRIES[]{ "first", "second", "third", "forth", "fifth" };
int digit{}, count{};
cout << "Enter 5 digits: " << endl;
cout << "Please enter a digit between 1-30 inclusive." << std::endl;
while (count < MAXTRIES)
{
cout << "\n" << TRIES[count] << " number -> ";
cin >> digit;
// <--- Stays in loop until a correct number is entered.
while (digit < 1 && digit>30)
{
cout << "Please enter a digit between 1-30 inclusive.";
cout << "\n" << TRIES[count] << " number -> ";
cin >> digit;
}
for (int i = 0; i < digit; i++)
{
cout << "*";
}
count++; // <--- Only counts correct answers.
std::cout << std::endl;
}
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
//cin.sync();
//cin.get();
return(0);
}