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

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.

Related

simple while loop calculator

Hello im stuck at SUBTRACTION AND DIVITION AND I CANT FIGURE OUT WHAT CODE to use because when I try to subtract 10 i inputed it then it will loop since the while condition is not meet which it needs to be negative to terminate the loop and i inputed 2 for the second number then loop again then i putted -number which lead to terminate loop and subtract all the number but the result is -12 its always wrong in every number cant figure out why Please help
Also with divition, only my addition is working havent started the divition cuz i cant figure out how
#include <iostream>
using namespace std;
int amt2, total;
double subNumbers();
double amt=1;
double number=0;
int main() {
int chc=0;
int amt = 0;
int amt2 = 1;
cout << "Welcome again User!\n";
cout << "______________________________________________________________\n" << endl;
cout << "Mathematical Operations(Improved):\n\n";
cout << "\t[1]-Addition" << endl;
cout << "\t[2]-Subtraction" << endl;
cout << "\t[3]-Multiplication" << endl;
cout << "\t[4]-Division\n" << endl;
cout << "______________________________________________________________\n" << endl;
cout << "Type the number corresponding to your chosen operation: ";
cin >> chc;
```
switch (chc) {
case 1:
```
system ("cls");
cout << "\n\n\tOperation chosen: Addition";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
cout << "Enter your number: ";`
cin >> number;
while (number >= 0) {
// add all positive numbers
amt += number;
// take input again if the number is positive
cout << "Enter another number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum of all the numbers is: " << amt << endl;
break;
```
case 2:
system ("cls");
cout << "\n\n\tOperation chosen: Subtraction";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
do{
cout << "Enter your number: ";
cin >> number;
amt=number-number ;
}while (number >= 0);// subtract all positive numbers
// display the difference
cout << "\nThe difference of all the numbers is: "<<amt;
return 0;
}}
```
enter code here
You are subtracting number from number:
amt = number - number; // Which is always 0
So that's why amt == 0 always.
So just change your loop to this:
while (true) {
cout << "Enter your number: ";
cin >> number;
if (number < 0) break;
if (amt == 0) amt = number;
else if (number >= 0) amt -= number;
}
What this does is that if amt == 0, then set amt to number. I have done this because as the default value of amt is 0 (due to int amt = 0;), when amt == 0, then we can assume that the user has entered the first number, and thus we can set amt to number. And then we can use -= operator, which basically means:
amt = amt - number;
But before all this, using if (number < 0) break; we can check if the user has entered a negative number, and if the user has entered a negative number, then the break keyword will break out of the while loop.

testing an isbn number to see if its valid

I am having trouble with a homework assignment where I need to check if the ISBN code is valid. This is a beginner C++ class, and I am new to all of this. Right now, no matter what ISBN I put in, it tells me they are all valid. If someone could help point me in the right direction to figure out if my formula is correct when in my for() statement.
I have no problem with the outside loops, it's just the conversion I believe I am doing wrong.
being this is an intro c++ class, we are still only using basic functions, so the professor suggested reading in the ISBN and storing it under a char variable.
**#include <iostream>
using namespace std;
int main()*
{
int Counter;
int WeightedSum;
char ISBN[11] = { 0 };
char Choice;
char Again{};
int Sum = 0;
const char X = 10;
int IterationCounter = 0;
do // begininng of do/while loop
{
cout << "Would you like to check if your ISBN is valid or not? ";
cout << "Press Y/y for yes or N/n for no and to end program: ";
cin >> Choice; //choice input
cout << endl << endl; // blank line
while (Choice != 'Y' && Choice != 'y' && Choice != 'N' && Choice != 'n') // beginning of while loop. checks user input to see if valid y/n choice
{
cout << "Invalid choice! Enter either Y/y or N/n"; // displays when anything other than y/n is entered
cout << "Press Y/y for yes or N/n for no and to end program: "; //gives user another chance to enter y/n
cin >> Choice;
cout << endl << endl;
}
if (Choice == 'Y' || Choice == 'y')
{
cout << "Enter the ISBN you wish to convert: " << endl;
cin >> ISBN;
cout << ISBN;
cout << endl << endl;
for (Counter = 0; Counter < 10; Counter++)
{
WeightedSum = ISBN[10] * (10 - Counter);
Sum = WeightedSum + Sum;
}
cout << Sum;
if (Sum % 11 == 0)
{
cout << Sum;
cout << " Is a valid ISBN " << endl;
}
else
{
cout << Sum;
cout << " Is invalid " << endl;
}*
The problem is this line
WeightedSum = ISBN[10] * (10 - Counter);
You're always performing math against the character at position 11. Because you multiply it by 10 - Counter, which is 0 through 9, you're going to basically be multiplying it by the sum of 1 through 10, or 55. Since 55 is divisible by 11, you'll always end up with if (Sum % 11 == 0) evaluating to true. Worst case, you may even hit an access violation since you're not checking the length of ISBN before accessing it.

Validation While Loop Only Repeating Every Other Time

I'm creating a program that is an arithmetic quiz. The program displays a menu and presents a quiz depending on the option that the user chooses (1 for addition, 2 for subtraction, etc.).
My while loops that validate if the user answered a problem correctly or incorrectly seems to be repeating every other time as opposed to every time. This ends up affecting the correct/incorrect counters for how many questions a user answers correctly/incorrectly, and I haven't been able to figure out why this is happening - what do I need to fix?
I am also having issues where the loop doesn't end even if the user enters -1 to stop.
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
void showMenu();
int add(int, int);
// main function
int main()
{
int choice = 0, answer = 0; // user input
int num1, num2; // numbers for problem
int correct_sum; // correct answer
int correct = 0, incorrect = 0; // counters
do
{
// display program purpose to user
cout << "********* Welcome to the Arithmetic Quiz *******\n";
cout << '\n';
// call showMenu function
showMenu();
cin >> choice; // user inputs menu choice
cout << '\n';
// switch statement for menu choice
switch (choice)
{
// addition
case 1:
do {
// randomize numbers
srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// display problem to user
cout << "How much is " << num1 << " plus " << num2 << "?\n";
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
// correct sum formula & call addition() function
correct_sum = add(num1, num2);
// while user's answer is WRONG and not -1
while (answer != correct_sum && answer != -1)
{
// display incorrect message
cout << "No. Please try again.\n";
incorrect++; // increment incorrect counter
// ask user for input again
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
}
// while user's answer is RIGHT
while (answer == correct_sum)
{
// display correct message
cout << "Very Good!\n";
correct++; // increment correct counter
// randomize numbers again for different problem
srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// show more problems to user until -1 is entered
cout << "How much is " << num1 << " plus " << num2 << "?\n";
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
}
} while (answer != -1);
if (answer == -1)
{
// display addition results when answer == STOP
cout << "********* RESULTS *******\n";
cout << '\n';
cout << "Addition problems solved: " << (correct + incorrect) << '\n';
cout << "Number answered correctly: " << correct << '\n';
cout << "Number answered incorrectly: " << incorrect << '\n';
}
}
// --------------------------------------------------------------------------------------
// showMenu function
void showMenu()
{
// display menu options
cout << "MENU:\n";
cout << '\n';
cout << "1. Enter 1 for Addition\n";
cout << "2. Enter 2 for Subtraction\n";
cout << "3. Enter 3 for Multiplication\n";
cout << "4. Enter 4 for Division\n";
cout << "5. Enter 5 for Modulus\n";
cout << "6. Enter 6 to Exit\n";
cout << '\n';
}
// --------------------------------------------------------------------------------------
// add function
int add(int num1, int num2)
{
return (num1 + num2);
}
You don't have yo use while loop inside do..while loop, if condition will work there, Try below code i have commented some part of code.
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
void showMenu();
int add(int, int);
// showMenu function
void showMenu()
{
// display menu options
cout << "MENU:\n";
cout << '\n';
cout << "1. Enter 1 for Addition\n";
cout << "2. Enter 2 for Subtraction\n";
cout << "3. Enter 3 for Multiplication\n";
cout << "4. Enter 4 for Division\n";
cout << "5. Enter 5 for Modulus\n";
cout << "6. Enter 6 to Exit\n";
cout << '\n';
}
// --------------------------------------------------------------------------------------
// add function
int add(int num1, int num2)
{
return (num1 + num2);
}
// main function
int main()
{
int choice = 0, answer = 0; // user input
int num1, num2; // numbers for problem
int correct_sum; // correct answer
int correct = 0, incorrect = 0; // counters
do
{
// display program purpose to user
cout << "********* Welcome to the Arithmetic Quiz *******\n";
cout << '\n';
// call showMenu function
showMenu();
cin >> choice; // user inputs menu choice
cout << '\n';
// switch statement for menu choice
switch (choice)
{
// addition
case 1:
do {
// randomize numbers
// srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// display problem to user
cout << "How much is " << num1 << " plus " << num2 << "?\n";
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
// correct sum formula & call addition() function
correct_sum = add(num1, num2);
// while user's answer is WRONG and not -1
while(answer != correct_sum && answer != -1)
{
// display incorrect message
cout << "No. Please try again.\n";
incorrect++; // increment incorrect counter
// ask user for input again
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
correct_sum = add(num1, num2);
cout << '\n';
}
// while user's answer is RIGHT
if (answer == correct_sum)
{
// display correct message
cout << "Very Good!\n";
correct++; // increment correct counter
// randomize numbers again for different problem
// srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// show more problems to user until -1 is entered
//cout << "How much is " << num1 << " plus " << num2 << "?\n";
//cout << "Enter your answer (-1 to return to the menu)\n";
//cin >> answer;
cout << '\n';
}
} while (answer != -1);
if (answer == -1)
{
// display addition results when answer == STOP
cout << "********* RESULTS *******\n";
cout << '\n';
cout << "Addition problems solved: " << (correct + incorrect) << '\n';
cout << "Number answered correctly: " << correct << '\n';
cout << "Number answered incorrectly: " << incorrect << '\n';
}
}
}while (answer != -1);
}
// --------------------------------------------------------------------------------------
Output of above code now if answer is not correct
********* Welcome to the Arithmetic Quiz *******
MENU:
1. Enter 1 for Addition
2. Enter 2 for Subtraction
3. Enter 3 for Multiplication
4. Enter 4 for Division
5. Enter 5 for Modulus
6. Enter 6 to Exit
1
How much is 6 plus 9?
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
I hope you are expecting this.

C++: While Looping Amount of Times

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;
}

Why does one of my 'while' statements execute when another one of my 'while' statements is true?

Note: I am using C++
Why does one of my 'while' statements ('while' number 1) execute anyway when another one of my 'while' statements ('while' number 2) placed above it is valid? This bothers me, because though 'while' number 1 is not true, but 'while' number 2 is true, 'while' number 1 executes anyway instead of 'while' number 2. Can anyone help me, or explain this? Here is my code:
#include <iostream>
#include <string>
using namespace std;
void PancakeGlutton()
{
int answer;
cout << "Good morning!" << endl;
cout << "Would you like to enter pancake data? Press 1 to accept, press 2 to decline: ";
cin >> answer;
while (answer == 1) {
int totalPeople = 10;
int totalPancakes = 0;
int input;
int lowest = 100000;
int highest = 0;
for (int i = 9; i >= 0; --i) {
cout << "How many pancakes did you eat this morning? I will be asking this question " << i << " more times." << endl;
cin >> input;
totalPancakes += input;
if (input >= highest) {
highest = input;
}
if (input <= lowest) {
lowest = input;
}
}
double pancakeAverage = double(totalPancakes) / double(totalPeople);
cout << "The total number of pancakes eaten was " << totalPancakes << " pancakes " << endl;
cout << "The average number of pancakes eaten was " << pancakeAverage << " pancakes " << endl;
cout << "The highest number of pancakes eaten was " << highest << " pancakes" << endl;
cout << "The lowest number of pancakes eaten was " << lowest << " pancakes" << endl;
cout << "" << endl;
cout << "Do you want to enter more pancake data? Press 1 to accept, press 2 to decline: ";
cin >> answer;
}
// while number 1:
while (answer == 2) {
break;
}
// while number 2:
while (answer != 1 || answer != 2) {
cout << "Error: please enter a valid answer. 1 or 2? ";
cin >> answer;
}
}
int main()
{
PancakeGlutton();
system("PAUSE");
return EXIT_SUCCESS;
}
while (answer != 1 || answer != 2) {
cout << "Error: please enter a valid answer. 1 or 2? ";
cin >> answer;
}
must be
while (answer != 1 && answer != 2) {
cout << "Error: please enter a valid answer. 1 or 2? ";
cin >> answer;
}