issue with terminating through -1 - c++

I need to use -1 to terminate but still display the summary.
Every time that I've tried and have gotten it to terminate the program, it doesn't go ahead and display the summary.
There are up to 10 trials, if you don't have enough information for 10 and you want to stop at 8, you type -1 and it goes to the summary and then terminates the program
while(i<10)
{
do
{
cout << "Enter result """ << i+1 << """ (or -1 if no more results): ";
cin >> score[i];
if(score[i] >=0 && score[i] <=49)
{
cout << "Grade " << "U" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=50&& score[i] <=59)
{
cout << "Grade " << "P" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=60 && score[i] <=69)
{
cout << "Grade " << "C" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=70 && score[i] <=89)
{
cout << "Grade " << "B" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=90 && score[i] <=100)
{
cout << "Grade " << "A" << " will be assigned to this result\n";
bool test=true;
i++;
}
else
{
test=false;
cout << "Invalid Input!\n";
}
}
while(test);
}
cout << "\nSummary of the results:\n";
for(int a=0;a< 10;a++)
{
std::cout << std::fixed << std::setprecision(2) << "Result " << a+1 << " " << score[a] << " Grade " << determine_grade(score[a]) << "\n";
}
cout << "\nThe average of the results = " << calc_average(score) << "\n";
cout << "The lowest of the results = " << find_lowest(score) << "\n";
cout << "The highest of the results = " << find_highest(score) << "\n";
system("Pause");

You don't want two loops, only one. You need to combine your two conditions into one i<10 && test.
Also you have declared your test variable in the wrong places. You should declare it once at the beginning of your loop.
bool test = true;
while(i<10 && test)
{
cout << "Enter result """ << i+1 << """ (or -1 if no more results): ";
if(score[i] >=0 && score[i] <=49)
{
cout << "Grade " << "U" << " will be assigned to this result\n";
i++;
}
...
else
{
test=false;
cout << "Invalid Input!\n";
}
}

Try to use break; when -1 is inputted inside the while loop. Also, you can use 1 loop, instead of two as john mentioned above.
Another thing to look for is your last for loop, it goes from 0 to 9, but in case someone used -1 and only inputted 3 grades, there might be odd values for the solutions.

Related

How do I go about fixing my calculator answers in my c++ program with a basic menu?

I have a basic menu program that gives the user different programs to choose from and run, when I run the simpleCalculator option and iput values and the operation of arithmetic the output is always the wrong answer but when I first programmed the calculator program without the menu and it being in its own function it would run just fine and give the the right answers, what exactly is going wrong here?
Ignore the third option in the menu since i haven't added code for the prime number checker program
double first_arg;
double second_arg;
string arithmeticOperation;
int numBottles = 99;
double simpleCalculator()
{
cout << "Enter first value: ";
cin >> first_arg;
cout << "Enter choice of arithmetic operation:\n* = multiplication\n/ = division\n+ = addition\n- = subtraction\n\n";
cin >> arithmeticOperation;
cout << "\nEnter second value: ";
cin >> second_arg;
if(arithmeticOperation == "*")
{
cout << "\nYour answer is " << first_arg * second_arg;
}
else if(arithmeticOperation == "/")
{
cout << "\nYour answer is " << first_arg / second_arg;
}
else if(arithmeticOperation == "+")
{
cout << "\nYour answer is " << first_arg + second_arg;
}
else if(arithmeticOperation == "-")
{
cout << "\nYour answer is " << first_arg - second_arg;
}
else
{
cout << "\nPlease enter a valid choice of an arithmetic operation\n";
}
}
string bottlesProgram()
{
while(true)
{
if (numBottles > 2)
{
cout << numBottles << " bottles of beer on the wall\n" << numBottles << " bottles of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottles of beer on the wall.";
cout << "\n\n";
}
else if(numBottles == 2)
{
cout << numBottles << " bottles of beer on the wall\n" << numBottles << " bottles of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottle of beer on the wall.";
cout << "\n\n";
}
else if(numBottles == 1)
{
cout << numBottles << " bottle of beer on the wall\n" << numBottles << " bottle of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottles of beer on the wall.";
cout << "\n\n";
}
else if(numBottles == 0)
{
cout << "No more bottles of beer on the wall,\nNo more bottles of beer.\nGo to the store and buy some more,\n" << "99 bottles of beer on the wall...";
cout << "\n\n";
}
else
{
break;
}
numBottles--;
}
}
int main()
{
int menuInput;
cout << " ***Programs Menu***\n" << "------------------------------------------\n" << "1. Run a simple Calculator\n" << "2. Run 99 bottles of beer on the wall song\n" << "3. Run prime number checker program\n" << "------------------------------------------\n";
cin >> menuInput;
if(menuInput == 1)
{
cout << simpleCalculator();
}
else if (menuInput == 2)
{
cout << bottlesProgram();
}
}
You seem to be confused about returning values from a function, and printing values in a function. These are not the same thing.
If you print the values in the function then the function should be void (i.e. nothing is returned) and the printing happens inside the function (obviously)
void simpleCalculator()
{
...
cout << "\nYour answer is " << first_arg * second_arg;
...
}
simpleCalculator(); // no cout
On the other hand if you return a value from the function then the function is not void and the printing happens outside the function
double simpleCalculator()
{
...
return first_arg * second_arg; // return not cout
...
}
cout << "\nYour answer is " << simpleCalculator();
Your code is doing half one version and half the other.

Why does the if statement is always the else part even if the if is true?

I'm having a problem with the if statement at the end.
**if the sum of the cubs of the number a user inputs, is equal to the number itself, say "....". Else, say "....." **
The problem is that it always jumps the if part to the else.
Its a task from the uni, no homework or nothing, just training. IF you have suggestions on how to better I would appreciate that too.
Thank you!
{
int n;
cout << "Write a number different from 0 -> ";
cin >> n;
while (n == 0)
{
cout << "Choose another number -> ";
cin >> n;
}
cout << "Good number " << n << " is!" << "\n";
cout << "lets separate each digit:" << "\n" << " -----------------------------------" << endl;
Sleep(1000);
vector<int> vecN;
while (n != 0)
{
int digit = n % 10;
n /= 10;
cout << n << endl;
cout << "Digit: " << digit << endl;
vecN.push_back(digit);
Sleep(750);
}
cout << "There you go!" << endl;
Sleep(1000);
cout << "Next stage, let's find the cubes for each one of the digits!" << endl;
Sleep(2500);
vector<int> sums;
for (auto i = vecN.begin(); i != vecN.end(); i++)
{
Sleep(500);
int Cubes = pow(*i, 3);
cout << Cubes << endl;
sums.push_back(Cubes);
}
Sleep(1300);
cout << "Now let's sum the cubs and see if the number is an Armstrong Number" << endl;
Sleep(3000);
int armSum = accumulate(sums.begin(), sums.end(), 0);
if ( armSum == n )
{
cout << "Sum: " << armSum << endl;
Sleep(500);
cout << "That's an Armstrong Number!" << "\n"
"The sum of the cubs of each digit in the number is equal to that same number!" << endl;
}
else
{
cout << "Sum: " << armSum << endl;
Sleep(500);
cout << "That's not an Armstrong Number!" << endl;
}
return 0;
} ```
When the if-part is entered, the else-part won't be entered any more. Note that your if/else is not surrounded by a loop. So when control passes by once, e.g. when having entered n==0, then it has passed by and won't step into neither the if nor the else-part a second time.
Try something like
while (n==0) {
cout << "Choose another number -> ";
cin >> n;
}
// continue here; n is != 0

issue with program loops and if statements

I'm writing a program that tells a user when they enter a negative or positive and even or odd number
then the program ask a question, " Would you like to enter another number? y(es) or n(o)
I need to account for the user entering in something else besides 'y' and 'n'
and I need to account for if the user does not enter an integer. Last if the user selects yes, the program will need to go through the loop process of determining if they enter an integer and if its (positive or negative and odd or even)
int value;
char choice;
cout << "Please enter a number" << endl;
cin >> value;
while (!(cin >> value)) {
cin.clear();
cin.ignore(999, '\n');
cout << "Invalid data type! Please enter 'value' again" << endl;
}
if (value > 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is positive" << endl;
}
else if (value < 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is negative" << endl;
}
else if (value > 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is postive" << endl;
}
else if (value < 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is negative" << endl;
}
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;
while (choice != 'y' &&choice != 'n') {
cin.clear();
cin.ignore(999, '\n');
cout << "Invalid response! Please enter 'choice' again" << endl;
}
do {
if (choice == 'y') {
cout << "Please enter a number" << endl;
cin >> value;
if (!(cin >> value)) {
cin.clear();
cin.ignore(999, '\n');
cout << "Invalid data type! Please enter 'value' again" << endl;
if (value > 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is positive" << endl;
}
else if (value < 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is negative" << endl;
}
else if (value > 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is postive" << endl;
}
else if (value < 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is negative" << endl;
}
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;
}
}
} while (choice == 'n');
cout << "Thank you for using my program. Goodbye!" << endl;
return 0;
}
It would take nested do-while loops to check all your conditions.
Here I am using cin.fail(). cin.fail() detects whether the value entered fits the value defined in the variable.
int value;
char choice;
do{
cout << "Please enter a number" << endl;
cin >> value;
if(cin.fail()) // check if input is int
{
cout<<"Not an int";
choice = 'y';
}
else
{
if (value > 0 && value % 2 == 0)
{
cout << value << " is even" << endl;
cout << value << " is positive" << endl;
}
else if (value < 0 && value % 2 != 0)
{
cout << value << " is odd" << endl;
cout << value << " is negative" << endl;
}
else if (value > 0 && value % 2 != 0)
{
cout << value << " is odd" << endl;
cout << value << " is postive" << endl;
}
else if (value < 0 && value % 2 == 0)
{
cout << value << " is even" << endl;
cout << value << " is negative" << endl;
}
do{
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;
}while(choice != 'y' || choice != 'n');
}
}while (choice == 'n');
Also you should read this: Checking input value is an integer

How to skip over >= in C++

I can't figure out how to make my code go to the next else if statement if my user input satisfies the previous if state.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
in your code if input > 60 it will satisfy condition and will not execute else part so first check wether input > 86400 if not then check if input > 36000 if not then check for input > 60
try below code in which if conditions are reversed
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
return 0;
}
Make it other way round.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else
{
cout << "The time is " << input << " seconds." << endl;
}
return 0;
}
Hope this is what you want!
There are multiple ways to do this, you can implement a check between 2 values using conditions like >= val1 and < val2 or ensure that the order in which the checks happen is different.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
or another way would be to change the order, once the particular if statement is valid, it doesn't check with the rest of the if statements.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input < 60)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
Your if else conditions are not completely defined... you have to set those in a range... otherwise is not going to work...
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600))
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400))
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}

Add a countdown timer to a math program quiz

I am trying to add a countdown timer to this program. I would like the timer to start when the first math fact question is asked and upon expiration i want the program to give the grade. What's the code to do this in c++ if possible?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int main(int args, char* argv[])
{
int i;
int result;
int solution;
char fact;
bool done = false;
int correct = 0;
int count = 0;
do {
try {
cout << "Enter (m)ultiplication or "
<< "(a)ddition." << endl; /*or (s)ubstraction. */
cin >> fact;
while (!cin)
throw fact;
if (fact != 'A')
if (fact != 'a')
if (fact != 'M')
if (fact != 'm')
while (!cin)
throw fact;
cout << "Now, enter the number of the fact that
you would like to do." << endl;
cin >> i;
int wrong = 0;
int score = 0;
int j = 0;
while (!cin)
throw i;
switch (fact) {
case 'm':
case 'M':
while (j < 13) {
cout << "What's " << i << " x " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i * j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " x " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " x " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i * j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
case 'a':
case 'A':
while (j < 13) {
cout << "What's " << i << " + " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i + j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " + " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " + " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i + j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
if (j == 13) {
system("pause");
correct = score - wrong;
score = (correct * 100) / 13;
}
if (score >= 80) {
cout << "Excellent!!!!!" << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Keep up the good work." << endl;
} else if (score >= 70) {
cout << "Congratulations!!!!!" << endl
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Let's see if we can score even higher next time." << endl;
} else {
cout << "You scored below 70 which means that you may need some"
<< " more practice." << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "You might want to try the " << i << " facts again."
<< " Goodluck!!!!!" << endl;
}
}
} catch (char fact) {
cout << "Invalid input. You can only enter (m)ultiplication or"
<< " (a)ddition. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (int i) {
cout << "Invalid input0. You can only enter a
number here. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (...) {
cout << "Invalid input2. You can only enter a number here.
Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
}
} while (!done);
return 0;
}
The task is quite hard, but if you dare trying, I suggest doing it in two steps:
Implement inaccurate solution: timer expiration is checked between queries to user.
If there is some time left, next question is asked, otherwise statistics is shown. So program always waits for user input on the last question despite timer has run out. Not what exactly quizzes look like, but good move to start with.
Method: before starting quiz save current time, before each question take delta between saved time and current one and compare with time limit. Example with chrono (starting from C++11), example with oldschool clock
Add middle-question interruption
This part requires function, which will wait for user input not longer, than specified amount of time. So instead of using std::cin() you'll need to calculate amount of time left (time limit minus delta between cur time and start time) and call some sort of cin_with_timeout(time_left).
The hardest thing is implementing cin_with_timeout(), which requires solid knowledge of multithreading and thread synchronization. Great inspiration can be found here, but it is direction to start thinking rather than complete solution.