I'm having a lot of trouble with this assignment for my first C++ course.
I have figured out how to get it to properly ask the user what operation they would like to use (add, subtract, multiply), generate random numbers between 0-9, and how to ask the user to solve the problem and respond if it is correct or incorrect.
After this point, the program is supposed to ask the user if they would like to continue (by pressing y) or quit (by pressing Q), with an error message for the user when they enter any other letter, but for some reason this part doesn't display when running the program.
How do I get the loop to work correctly, allowing me to do the final prompt, and THEN repeat the whole program only when pressing Y or quit when pressing Q?
Note: I'm VERY new to coding in general, and this is my very first C++ course, so I do not know yet how to make this code more succinct:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
while (true)
{
// Generate two random single-digit integers btwn 0-9
srand(time(0));
int num1 = rand() % 10;
int num2 = rand() % 10;
int operation, play, num3, guess, Y, Q;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
}
else if (operation == 1)
{
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
else if (operation == 2)
{
cout << "You chose subtraction." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
else if (operation == 3)
{
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
while (guess != num3)
{
int play, Y, Q;
cout << "Would you like to play again? Press Y for yes or Q for
quit" << endl;
cin >> play;
if (play != Y || play != Q)
{
cout << "That is not a valid choice. Please choose Y for yes
or Q to quit. " << endl;
cin >> play;
}
else
{
if (play == Y)
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else if (play == Q)
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
break;
}
}
}
return 0;
}
There are few things here...
1. Only seed once
Move srand(time(0)); out of the while loop and to the top of main. If you repeatedly seed in the same second (if time(0) doesn't change), you'll get the same "random" numbers twice.
2. What happens to num3 if they don't enter a valid operation?
You never initalize num3, so if they don't choose a valid operation, num3 will have a junk value. You then go on to run a loop whose condition depends on num3's value! (while (guess != num3))
3. else { ... if { is the same as else if {
In your final loop, bring the if (play == Y) and else if (play == Q) out of that nested if and make them else if's.
4. Your last loop condition is incorrect
Is while (guess != num3) really right? You want to loop until they enter valid input, so why are you looping while guess != num3?
The problem is found with in the second while-loop. The play variable should be declared as a char rather than an int. Plus you don't need to compare it with Y and Q integer variables. Here is a solution. I hop it will help you:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
bool loop = true;
while (loop)
{
// Generate two random single-digit integers btwn 0-9
srand(time(0));
int num1 = rand() % 10;
int num2 = rand() % 10;
int operation, play, num3, guess, Y, Q;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
cout << "Choose an operation.\n\t-----------------------" << endl;
cout << "\tEnter 1 to add,\n\tEnter 2 to subtract, or\n\tEnter 3 to multiply\n\t-----------------------\n\t\tEnter: ";
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Invalid choice! Please try again." << endl;
continue;
}
else if (operation == 1)
{
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " + " << num2 << " = ";
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "What is " << num1 << " + " << num2 << " = ";
cin >> guess;
}
else if (guess == num3)
cout << "That is correct!" << endl;
}
else if (operation == 2)
{
cout << "You chose subtraction." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " - " << num2 << " = ";
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "What is " << num1 << " - " << num2 << " = ";
cin >> guess;
}
else if (guess == num3)
cout << "That is correct!" << endl;
}
else if (operation == 3)
{
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " = ";
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "What is " << num1 << " * " << num2 << " = ";
cin >> guess;
}
else if (guess == num3)
cout << "That is correct!" << endl;
}
while (true)
{
char play;
cout << "Would you like to play again? Press Y for yes or Q for quit: ";
cin >> play;
if (play == 'Y' || play == 'y')
break;
else if(play == 'Q' || play == 'q')
{
loop = false;
cout << "Good bye.\n";
break;
}
else
cout<< "Invalid choice.\n";
}
}
return 0;
}
Making the menu a little interactive is also good, peace.
It's could be better to use a switch case to select the correct operation like this:
Switch(operation) {case 1: break;}
You need to add one more while too
SO the correct code should be like this:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess,num1,num2,temp;
srand(time(0));
char play;
do
{
// Generate two random single-digit integers btwn 0-9
num1 = rand() % 10;
num2 = rand() % 10;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
}
}while(operation>3 || operation<1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
}
do
{
cout << "Would you like to play again? Press Y for yes or Q for quit" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
{
cout << "That is not a valid choice. Please choose Y for yes or Q to quit. " << endl;
}
}while(play!='Y' && play!='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}while(play=='Y');
return 0;
}
Solution:
P29: Practice Arithmetic Skills (if/else , loop)
Description:
"Write a program to let a child practice arithmetic skills.
The program should first ask for what kind of practice is wanted: +, -, * , and let the user repeat the practice as many times as desired, until "Q" is entered.
Two random numbers will be generated from (0 - 9).
If the child answers the equation correctly, a message should appear , and they can then go to the next problem(two different numbers generated).
If the child answers incorrectly, a message should appear & the problem should be repeated (same numbers used)."
Finally fixed!:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
{
num1 = rand() % 10;
num2 = rand() % 10;
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
}
}while (operation > 3 || operation < 1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
}
do
{
cout << "Would you like to play again? Press Y for yes or Q for quit" << endl;
cout << "" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
{
cout << "That is not a valid choice. Please choose Y for yes or Q to quit. " << endl;
cout << "" << endl;
}
}
while(play !='Y' && play !='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}
while(play=='Y');
return 0;
}
Related
So im trying to make a calculator and i added a part so i can calculate area, first i asked for integer or geometry math, when i choose geometry, it skips my question of you want to calculate volume. But there were no compiler errors. It everything after " else if (choice == "geometry") {" and until the last line. Anyone knows how to fix.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string choice;
cout << "choose integer or geometry\n";
cin >> choice;
if (choice == "integer") {
double num1{ 0 };
double num2{ 0 };
cout << "pick a number\n";
cin >> num1;
cout << "pick another number\n";
cin >> num2;
string integerChoice;
cout << "choose addition, subtraction, multipliction, or division\n";
cin >> integerChoice;
if (integerChoice == "addition") {
cout << num1 << " + " << num2 << " is " << num1 + num2
<< '\n';
}
else if (integerChoice == "subtraction") {
cout << num1 << " - " << num2 << " is " << num1 - num2
<< '\n';
}
else if (integerChoice == "multiplication") {
cout << num1 << " * " << num2 << " is " << num1 * num2
<< '\n';
}
else if (integerChoice == "division") {
cout << num1 << " / " << num2 << " is " << num1 / num2
<< '\n';
}//integer is done
}
else if (choice == "geometry") {
string geoChoice1;
cout << "do you want to calculate volume, enter yes or no\n";
cin >> geoChoice1;
if (geoChoice1 == "yes") {
cout << "choose retangular prism(incudes cubes), cone, or cylinder\n";
string volumeChoice;
cin >> volumeChoice;
if (volumeChoice == "rectangular prism") {
double recPrismLength{ 0 };
double recPrismWidth{ 0 };
double recPrismHeight{ 0 };
cout << "Enter the length\n";
cin >> recPrismLength;
cout << "Enter the width\n";
cin >> recPrismWidth;
cout << "Enter the height\n";
cin >> recPrismHeight;
cout << recPrismLength << " * " << recPrismWidth << " * " << recPrismHeight << " is " <<
recPrismLength * recPrismWidth * recPrismHeight << '\n';
}
else if (volumeChoice == "cylinder") {
float cHeight;
float cRadius;
const double pi{ 3.14159265358979323846 };
float cFormula{ pi * pow(2.0, cRadius) * cHeight };
cout << "Enter the height of the cylinder\n";
cin >> cHeight;
cout << "Enter the radius of the cylinder\n";
cin >> cRadius;
cout << cFormula;
}
else if (geoChoice1 == "no") {
Fixed Code:
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
std::string ToLower(std::string str) {
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return std::tolower(c); });
}
int main()
{
string choice;
cout << "choose integer or geometry\n";
cin >> choice;
if (choice == "integer") {
double num1{ 0 };
double num2{ 0 };
cout << "pick a number\n";
cin >> num1;
cout << "pick another number\n";
cin >> num2;
string integerChoice;
cout << "choose addition, subtraction, multipliction, or division\n";
cin >> integerChoice;
if (integerChoice == "addition") {
cout << num1 << " + " << num2 << " is " << num1 + num2
<< '\n';
}
else if (integerChoice == "subtraction") {
cout << num1 << " - " << num2 << " is " << num1 - num2
<< '\n';
}
else if (integerChoice == "multiplication") {
cout << num1 << " * " << num2 << " is " << num1 * num2
<< '\n';
}
else if (integerChoice == "division") {
cout << num1 << " / " << num2 << " is " << num1 / num2
<< '\n';
}//integer is done
}
else if (choice == "geometry") {
string geoChoice1;
while (!(geoChoice1 == "yes" || geoChoice1 == "no"))
cout << "do you want to calculate volume, enter yes or no\n";
cin >> geoChoice1;
ToLower(geoChoice1);
if (geoChoice1 == "yes") {
cout << "choose retangular prism(incudes cubes), cone, or cylinder\n";
string volumeChoice;
cin >> volumeChoice;
ToLower(volumeChoice);
if (volumeChoice == "rectangular prism") { //this will never be executed, as the >> operator skips whitespace, and therefore will only read "rectangular"
//I will leave this for you to fix
double recPrismLength{ 0 };
double recPrismWidth{ 0 };
double recPrismHeight{ 0 };
cout << "Enter the length\n";
cin >> recPrismLength;
cout << "Enter the width\n";
cin >> recPrismWidth;
cout << "Enter the height\n";
cin >> recPrismHeight;
cout << recPrismLength << " * " << recPrismWidth << " * " << recPrismHeight << " is " <<
recPrismLength * recPrismWidth * recPrismHeight << '\n';
}
else if (volumeChoice == "cylinder") {
float cHeight;
float cRadius;
const double pi{ 3.14159265358979323846 };
double cFormula{ pi * pow(2.0, cRadius) * cHeight }; //this will always be the same, please note, because cHeight and cRadius are only filled after this function
cout << "Enter the height of the cylinder\n";
cin >> cHeight;
cout << "Enter the radius of the cylinder\n";
cin >> cRadius;
cout << cFormula;
break;
}
}
else if (geoChoice1 == "no") break;
else cout << "Please enter something I understand, either 'yes' or 'no'.\n";
} //this was missing
} //this was missing
Your code did not take into account if the user inputted "Yes" with a capital Y. The code would just conclude that "Yes" would not be equal to "yes". I made a function which converts your input into a lowercase form, then evaluates it, and if it is not "yes" or "no" asks the user to put it again.
There are some other errors which I have put comments on, that you will have to fix.
I'm ALMOST done with this assignment, but still having trouble getting the code to display the error messages continuously until the correct input is entered.
For example, if the user enters "4" for operation (which should be between 1-3), it correctly displays: "Your operation choice isn't valid! Please try again, using 1, 2, or 3." However, if the user enters another invalid number for the operation (such as 5), it doesn't repeat the error message, but just continues forward.
Anyone able to help me figure out how to get each error message to repeat until valid numbers or characters are entered for each prompt?
Note: I am very new to coding, and still figuring out stackoverflow...I think I have followed all the MCVE suggestions/format. THANK YOU!!
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
{
num1 = rand() % 10;
num2 = rand() % 10;
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " <<
endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try
again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
}
while (operation > 3 || operation < 1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: "
<< endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "What is " << num1 << " - " << num2 << " ?: " <<
endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." <<
endl;
cout << "" << endl;
cout << "What is " << num1 << " - " << num2 << " ?:
" << endl;
cout << "" << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " ?: " <<
endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." <<
endl;
cout << "" << endl;
cout << "What is " << num1 << " * " << num2 << " ?:
" << endl;
cout << "" << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
}
do
{
cout << "Would you like to play again? Press Y for yes or Q for
quit" << endl;
cout << "" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
{
cout << "That is not a valid choice. Please choose Y for yes
or Q to quit. " << endl;
cout << "" << endl;
}
}
while(play !='Y' && play !='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}
while(play=='Y');
return 0;
}
/*Sample Run:
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
3
You chose multiplication.
What is 4 * 1 ?:
4
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
1
You chose addition.
What is 6 + 1 ?:
7
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
2
You chose subtraction.
What is 5 - 0 ?:
5
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
1
You chose addition.
What is 7 + 1 ?:
9
That is incorrect. Please try again.
What is 7 + 1 ?:
10
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
2
You chose subtraction.
What is 7 - 3 ?:
5
That is incorrect. Please try again.
What is 7 - 3 ?:
6
Would you like to play again? Press Y for yes or Q for quit
Q
Thank you for playing! See you next time!
Process returned 0 (0x0) execution time : 43.057 s
Press any key to continue.
*/
P29: Practice Arithmetic Skills (if/else , loop)
Description:
"Write a program to let a child practice arithmetic skills.
The program should first ask for what kind of practice is wanted: +, -, * , and let the user repeat the practice as many times as desired, until "Q" is entered.
Two random numbers will be generated from (0 - 9).
If the child answers the equation correctly, a message should appear , and they can then go to the next problem(two different numbers generated).
If the child answers incorrectly, a message should appear & the problem should be repeated (same numbers used)."
Finally fixed!:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
{
num1 = rand() % 10;
num2 = rand() % 10;
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
}
}while (operation > 3 || operation < 1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
}
do
{
cout << "Would you like to play again? Press Y for yes or Q for quit" << endl;
cout << "" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
{
cout << "That is not a valid choice. Please choose Y for yes or Q to quit. " << endl;
cout << "" << endl;
}
}
while(play !='Y' && play !='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}
while(play=='Y');
return 0;
}
Your do while loop
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
}
while (operation > 3 || operation < 1);
should be
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
}
}
while (operation > 3 || operation < 1);
Do not use the do-while loop if you want to give only one reattempt to the user to select the correct operation. Keep that block this way
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try
again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
OR
if you want to print the error message only for first wrong attempt and for all succedding wrong attempts you can do in the following way:
int flag=0;
do{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if ((operation > 3 || operation < 1)&&flag==0)
{
flag=1;
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
} while(operation > 3 || operation < 1);
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main(){
int gnumber, rnumber;
char choice;
int tries;
do {
cout << "Welcome to the Number Guessing Game!" << endl;
cout << endl; // breakline
cout << "How many tries: ";
cin >> tries;
cout << endl;
while (tries > 0){
srand(time(NULL));
rnumber = rand() % 99;
cout <<"Enter an integer greater than or equal to 0 and less than 100:";
cin >> gnumber;
system("cls");
if (tries != 1){
if (gnumber < 100 && gnumber >= 0){
if (gnumber == rnumber){
cout << "Congratulations! You've guessed the number." << endl;
tries--;
cout << "Remaining tries: " << tries << endl;
}
else if (gnumber > rnumber){
cout << "Your guess is higher than the number." << endl;
tries--;
cout << " Guess Again!" << endl;
cout << "Remaining tries: " << tries << endl;
}
else{
cout << "Your guess is lower than the number." << endl;
tries--;
cout << " Guess Again!" << endl;
cout << "Remaining tries: " << tries << endl;
}
}
else
cout << "Must greater or equal to 0 and lesser than 100!" << endl;
}
else
{
cout << "Game over!" <<" The number is: " << rnumber << endl;
cout << "Play Again? (Y/N)" << endl;
cin >> choice;
system("cls");
}
}
}while(choice == 'Y' || choice == 'y'); //
system("pause");
return 0;
}
EVEN IF I ENTER CHOICE AS 'N' OR 'n' IT WONT STOP THE LOOP.
And even if I enter 'Y' or 'y', it does not ask how many tries i wanted.
Instead it just asks directly what integer I would want to enter.
Please try to copy and compile the code to further understand what the problem is. Please help.
P.S.: This is a guessing program I'm making by the way...
the bug in your inner loop if the use enters a value other than one in else you don't decrement tries so it gets stuck:
else
{
cout << "Game over!" <<" The number is: " << rnumber << endl;
cout << "Play Again? (Y/N)" << endl;
cin >> choice;
system("cls");
tries--; // add this
}
I am working with a c++ program, but I am stuck with annoying bug. The bug is that when i type the password, it counts backspace as a character so can I fix it? Here is the code.
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
string password, username, lon, nu, np;
char c;
int StarNum = 0, humanproof;
cout << "Do you wanna create a user or login?";
cout << "\nLogin" << endl;
cout << "New" << endl;
cin >> lon;
if(lon=="login"){
goto login;
}
if(lon=="new"){
goto newa;
}
login:
cout << "Username: ";
cin >> username;
lol:
cout << "Password: ";
while (c != 13)
{
c = (char)getch();
if(c == 13) break;
StarNum++;
password += c;
cout << "*";
if (c == 127 || c == 8){
//go here to fix the problem
}
password = "";
goto lol;
}
}
if(username == "user" && password == "pass" || username == nu && password == np){
cout << "\nYou are logged in.";
goto options;
} else {
cout << "\nusername or password is wrong" << endl;
return 0;
}
return 0;
newa:
cout << "Username:";
cin >> nu;
cout << "password:";
cin >> np;
cout << "Type the number fourhoundred and twentie three too proof you are a human: ";
cin >> humanproof;
if(humanproof == 423){
cout << "The username is " << nu << endl;
for(int no = 0; no <= 100; no++){
cout << endl;
}
goto login;
return 0;
}
if(humanproof!=423){
cout << "wrong answer!";
return 0;
}
options:
int op;
cout << "\nwhat do you want to do?" << endl;
cout << "1. Calculator" << endl;
cout << "2. About"<< endl;
cout << "3. Just for fun" << endl;
cout << "4. Exit" << endl;
cin >> op;
if(op==1){
goto calculator;
}
if(op==2){
goto info;
}
if(op==3){
goto fun;
}
if(op==4){
return 0;
}
else{
cout << "you entered a invalid number. " << endl;
return 0;
}
calculator:
double n1, n2, sum;
int opa;
cout << "Choose a operation" << endl;
cout << "1. Addition/+" << endl;
cout << "2. Subscraction/-" << endl;
cout << "3. Multiplication/x" << endl;
cout << "4. Divsion/ /" << endl;
cin >> opa;
if(opa == 1){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 + n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 2){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 - n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 3){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 * n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 4){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 / n2;
cout << "the sum is " << sum;
return 0;
}
if(opa > 4){
cout << "You entered a invalid number";
goto calculator;
}
info:
cout << "Created by Bergur 2013";
return 0;
fun:
cout << "You want an eyepad(ipad)?";
}
Your code already checks:
while (c != 13)
And prevent newline from being handled, do similar to what you need with the backspace character, whose number is 8
To fix your issue, before:
StarNum++;
Add:
if (c == 8 && StarNum > 0) {
StarNum--;
if (password.size () > 0)
password.resize (password.size () - 1);
continue;
}
Please format your code. Also, try to provide a minimal code which reproduce the problem, not the whole code.
Unrelated to your problem
Try not to use goto.
Do not use ASCII values, but use the char literals instead, i.e. use '\n' instead of 13.
Also, you can simply add an iother condition inside your while:
while (c != '\n' && c != ' ')
I'm trying to make my calcuator loop back to the top after it finishes the calculation? I've tried while loops and seen tutorials on it but I just can't put it into context.
If you could show me how to actually use it in this program, that would be fantastic.
#include<iostream>
using namespace std;
int main() {
double num1, num2;
char op;
cout << "********C++ CALCULATOR********" << endl;
cout << "Please enter your first number" << endl;
cin >> num1;
cout << "Please enter your operand (+, -, *, /)\n" << endl;
cin >> op;
cout << "Please enter your second number\n" << endl;
cin >> num2;
if (op== '+') {
cout << "The answer is: " << num1 + num2 << endl;
} else if (op == '-') {
cout << "The answer is: " << num1 - num2 << endl;
} else if (op == '/') {
cout << "The answer is: " << num1 / num2 << endl;
} else if (op == '*') {
cout << "The answer is: " << num1 * num2 << endl;
} else {
cout << "That was an invalid command!" << endl;
}
}
I think you want anything like this:
#include<iostream>
using namespace std;
int main() {
double num1 = 0, num2 = 0;
char op = '';
char answer = '';
while(answer != 'n') { // Check condition
cout << "********C++ CALCULATOR********" << endl;
cout << "Please enter your first number" << endl;
cin >> num1;
cout << "Please enter your operand (+, -, *, /)" << endl;
cin >> op;
cout << "Please enter your second number\n" << endl;
cin >> num2;
if (op == '+') {
cout << "The answer is: " << num1 + num2 << endl;
} else if (op == '-') {
cout << "The answer is: " << num1 - num2 << endl;
} else if (op == '/') {
cout << "The answer is: " << num1 / num2 << endl;
} else if (op == '*') {
cout << "The answer is: " << num1 * num2 << endl;
} else {
cout << "That was an invalid command!\n Exit." << endl;
}
cout << "Do you want repeat? \"y\" or \"n\"\n" << endl;
cin >> answer;
}
}
The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loop checks the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.
For something that you want to run at least once you can also try a do/while statement.
In place of the word "do" you could use the 'while (again!='n');' at that loops lines curly brace (removing that "while again!='n';" check in the process) to have a standard while loop.
while(value==true) { ... }
as opposed to
do { ... } while(value==true);
This would require a properly initialized test variable though.
I included a second while loop in the larger do/while loop for further demonstration.
#include<iostream>
using namespace std;
int main() {
double num1 = 0, num2 = 0;
char op;
char again;
do { // set start point for loop
cout << "********C++ CALCULATOR********" << endl;
cout << "Please enter your first number" << endl;
cin >> num1;
cout << "Please enter your operand (+, -, *, /)" << endl;
cin >> op;
cout << "Please enter your second number" << endl;
cin >> num2;
if (op == '+') {
cout << "The answer is: " << num1 + num2 << endl;
} else if (op == '-') {
cout << "The answer is: " << num1 - num2 << endl;
} else if (op == '/') {
cout << "The answer is: " << num1 / num2 << endl;
} else if (op == '*') {
cout << "The answer is: " << num1 * num2 << endl;
} else {
cout << "That was an invalid command!" << endl;
}
cout << "\nRun again? \"y\" or \"n\"" << endl; // prompt user
cin >> again;
// here is a while loop in do/while statement to check for valid input if the user wants
// to go again
while(again!='y' && again!='n'){
cout << "Invalid input. Run again? y or n" << endl;
cin >> again;
}
cout << endl;
} while(again!='n'); // now check if user wants to go again // end of do loop
// the condition for while loop could be like
// while (1); // any non zero being true
// if you want executions until program termination
}
}
Just wrap everything into an endless loop
#include<iostream>
using namespace std;
int main() {
for (;;) {
// your code here
}
}