C++: While Looping Amount of Times - c++

So I am trying to write a basic program that asks the user to input any number other than 5, and after 10 iterations of the user not entering the number 5, I want the program to print to screen.
Here is my code so far:
#include <iostream>
#include <string>
using namespace std;
int main(){
int num;
cout << "Please enter a number other than 5." << endl;
cin >> num;
while (num != 5){
cout << "Please enter a number other than 5." << endl;
cin >> num;
}
return 0;
}
I just don't know how to tell the computer to stop the loop at 10 iterations and output to the screen.

this is a suitable time to utilize the
do while
loop
the way it works is it will execute the statement within the block without evaluating any conditions and then evaluate the condition to determine if the loop should run again
this is what your program could look like
#include <iostream>
using namespace std;
int main(void)
{
int counter = 0, num;
do
{
if (counter > 10) // or >=10 if you want to display if it is 10
{
cout << "exiting the loop!" << endl;
break; // the break statement will just break out of the loop
}
cout << "please enter a number not equal to 5" << endl;
cin >> num;
counter++; // or ++counter doesn't matter in this context
}
while ( num != 5);
return 0;
}

#include <iostream>
#include <string>
using namespace std;
int main(){
int num;
int counter=1;
cin >> num;
cout <<num;
if(num==5)
cout << "Please enter a number other than 5." << endl;
while (num != 5&&counter<=10){
cin >> num;
cout <<num;
if(num==5)
cout << "Please enter a number other than 5." << endl;
counter=counter+1;
}
return 0;
}

Related

Can't exit the while loop after guessing the right number

I'm trying to make a simple number guessing program with while loop but even after finding the right number (I changed the num with 5 and it didn't work) it does not exit the while loop. Can you tell me where the problem is?
int guess;
int num;
cout << "Enter the number (0-10): ";
cin >> guess;
while(guess != num){
int num = rand()%10;
cout << "Nope, keep trying." << endl;
cout << "Enter the number (0-10): ";
cin >> guess;
}
cout << "Congrats, you've found the number!" << endl;
int num = rand() % 10; is a declaration of a new variable num within the loop body and that shadows the num defined at the top of the program: while (guess != num) is using the latter.
The solution is to write num = rand() % 10; instead.
You will need to initialise num to a value before attempting to read it, else technically the behaviour of your program is undefined. Consider reworking to a do while loop.
The while loop is evaluating a num variable that is never assigned a value. The assignment being done inside the loop is to a different num variable that shadows the outer variable of the same name.
Try this instead:
#include <iostream>
#include <limits>
#include <cstdlib>
using namespace std;
int main() {
int guess;
int num;
do {
cout << "Enter the number (0-10): ";
if (cin >> guess) {
if (guess == num)
break;
num = rand() % 10;
cout << "Nope, keep trying." << endl;
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Not a number, keep trying." << endl;
}
}
while (true);
cout << "Congrats, you've found the number!" << endl;
}

C++ trying to count how many times a user has entered a specific number

I am trying to write a program that counts how many times a specific number is inputted by a user from a while loop. Here is the idea:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
bool progLoop = true;
int Number;
char response;
while (progLoop == true)
{
cout << "Please enter a number: " << endl; \
cin >> Number;
cout << "Number collected!" << endl;
cout << "Number of times that number has been entered: " << endl; //IDK how to do this part which is what I'm looking for.
cout << "Do you want to enter another number?" << endl;
cin >> response;
if (response == 'y')
{
progLoop == true;
}
else if(response == 'n')
{
progLoop == false;
}
}
}
I'm looking for a way that I can store a sort of value of how many times that specific number has been entered into the program. Would like to clarify if there are any questions! Thanks! (Modifying my code would be great!)
Use a hashmap:
#include <unordered_map>
#include <iostream>
#include <ctime>
using namespace std;
int main() {
int number;
char response = 'y';
unordered_map<int, int> m; // your hashmap
while (response == 'y')
{
cout << "Please enter a number: " << endl;
cin >> number;
cout << "Number collected!" << endl;
cout << "Number of times that number has been entered: " << ++m[number] << endl; // preincrement your hashmap at key number
cout << "Do you want to enter another number? (y/n)" << endl;
cin >> response;
response = tolower(response); // give a chance to exit the loop if your user like to use caplock (like Trump)
}
}
Expanding my comment into an answer
One way of doing this is to use an std::vector and every time a new number is inputted, it is pushed back into the vector. Then you can std::count that number when you need to check. In your program, it would look something like this:
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int main() {
bool progLoop = true;
int Number;
std::vector<int> numCount;
char response;
while (progLoop == true)
{
cout << "Please enter a number: " << endl;
cin >> Number;
numCount.push_back(Number);
cout << "Number collected!" << endl;
cout << "Number of times that number has been entered: " << std::count(numCount.begin(), numCount.end(), Number) << endl;
cout << "Do you want to enter another number?" << endl;
cin >> response;
if (response == 'y')
{
progLoop = true; // You accidentally used == here
}
else if(response == 'n')
{
progLoop = false; // You accidentally used == here
}
}
}

What loop should I use for the Question posted down below?

Question is how to get the program to ask for input until a valid choice (4-20) is given by the user? I just need to know how to repeat a question. So if a user inputs the wrong number then it will ask the user to input a valid number until the user enters a correct number between 4-20.
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
int num; //integer for number
int roll; //integer for roll
srand(time(0)); //seed random number generator
cout << "You are about to roll a single die" << endl;
cout << "How many sided die would you like to roll(4-20)? ";
cin >> num;
cin.ignore();
roll = 1 + rand() % num; //mods random number
if (num >= 4 && num <= 20) {
cout << "You rolled: " << endl;
cout << roll << endl;
}
else
{
cout << "Please play again and enter a number between 4 and 20" << endl;
cout << "Press RETURN to continue..."<<endl;
cin.get();
cout<<"Exiting"<<endl;
}
return 0;
}
Try this
while(1)
{
cout << "Please enter a number between 4 and 20: ";
cin >> num;
if (num >= 4 && num <= 20)
{
//mods random number
//do something
break;
}
}
Do a while loop to keep on asking for input and have a if statement to break when it is between 4 and 20.

Check if the user input has entered a repeated number

So i am still a beginner at this and still practising. Basically i need to make a program that continues to asks the user to enter any number other than 5 until the user enters the number 5.
I have that done but i could't figure out how to check if the user entered a repeating number.For example:
1
2
3
3 - The program should end
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main() {
cout << setw(15) << setfill('*') << "*" << endl;
cout << "Number 5" << endl;
cout << setw(15) << setfill('*') << "*" << endl;
int num;
cout << "Enter a number: ";
cin >> num;
if (num == 5) {
cout << "\nWhy did you enter 5? :) " << endl;
_getch();
exit(0);
}
for (int i = 1; i < 10;i++) {
cin >> num;
if (num == 5) {
cout << "\nWhy did you enter 5? :) " << endl;
_getch();
exit(0);
}
}
cout << "Wow, you're more patient then I am, you win." << endl;
_getch();
}
The previous answer does not meet the requirement in the linked article, which the querist himself seemed to not grasp:
★★ 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.)
This variant complies:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
cout <<"Please enter any number other than " <<i <<": ";
int num;
cin >>num;
if (num == i)
return cout <<"Hey! you weren't supposed to enter " <<i <<"!\n", 0;
}
cout <<"Wow, you're more patient then I am, you win.\n";
}
You could add all inputted numbers to a vector, and whenever you get a new number, check if it's already in the vector. Include these headers:
#include <vector>
#include <algorithm> // for std::find
Make the vector like this
std::vector<int> pastEntries;
Do the check like this:
if (std::find(pastEntries.begin(), pastEntries.end(), num) != pastEntries.end()) {
std::cout << "\nWhy did you enter " << num << "? :) " << endl;
...
And when the number was not found, add it to the vector like this (you can put this after the if block):
pastEntries.push_back(num);
Alternatively, you can use std::set:
std::set<int> pastEntries;
Insert into the set like this:
pastEntries.insert(num);
And find the number in the set like this:
if (pastEntries.find(num) != pastEntries.end()) {
Or insert the number while finding out whether it has already been inserted like this:
if (!pastEntries.insert(num).second) {

Prompt user for positive number, and continues to prompt the user until they enter a positive number c++

//review3
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
int number;
int main()
{
cout << "Enter a positive number" << endl;
cin >> number;
while (number < 0)
{
cout << "Enter a positive number" << endl;
}
if (number > 0)
{
cout << "Awesome job!" << endl;
}
return 0;
}
This is my code so far. I started with an else if but if the user entered a negative number the program would simply close. I changed this to a while loop and got stuck in an infinite loop. Before I had an if and else if statement. I need to continue to prompt the user until they enter a positive number in c++.
Your while() loop doesn't continue to prompt for input, that's why you're getting an infinite loop - because number never changes!
You can put the input operation into the while() loop like this:
while (cin >> number && number < 0)
{
cout << "Enter a positive number: " << endl;
}
if (cin)
{
cout << "Awesome job" << endl;
}
Thus, during each iteration of the loop the user will be prompted for input.
We check the state of cin afterwards to make sure that the above loop didn't stop because of invalid input (or no input at all).
#include <iostream>
using namespace std;
int number;
int main()
{
cout << "Enter a positive number" << endl;
cin >> number;
if (number < 0) {
cout << "Enter a positive number" << endl;
}
else {
cout << "Awesome job!" << endl;
}
return 0;
}
You can check if you get number or string here
sure in this case you should get input to string variable. If you want to convert it to integer you can use std::stoi
#include <iostream>
#include <string>
#include <cstring>
int main()
{
while(true)
{
std::cout << "Pleaase enter a positive number." << std::endl;
std::string buf;
int x = -255;
std::cin >> buf;
x = std::atoi(buf.c_str());
if(x > 0)
{
break;
}
}
std::cout << "Awesome Job!" << std::endl;
}
You can also do this snippet . Use #include <ctype.h> or <cctype>
while(1){
cin>>number;
if(number<0 || isalpha(number)) return 0;
cout<<"Awesome Job";
}