C++ counter variable not working properly - c++

I am writing a program that asks a user for a difficulty level then gives them multiplication questions while counting the correct answers. My issue is my counter (numCorrect) updates for false answers too and I can't understand why. Can someone tell me why?
int main()
{
int n; //difficulty level
int a, b, atimesb; // random generated numbers and multiplication
string name;
int numCorrect=0; // initilize counter to 0
int numAsked=0; // initilize counter to 0
int exitCond = 1; // loop condition continue
cout << "this program tests your multiplication skills!" << endl;
cout << "what is your name?" << endl;
getline(cin, name);
cout << " Enter a difficulty level" << endl;
cin >> n; // user input for difficulty level
while (exitCond != 0) // loop to continue asking until user ends with 0
{
MakeQuestion(n, a, b, atimesb); // calls function to make a question
UserAnswerIsCorrect(a, b, atimesb); // calls function to ask question and evaluate it
if (UserAnswerIsCorrect) // update if correct
{
numCorrect++;
}
numAsked++; // update total questions
cout << " Enter 0 to quit, 1 to go again" << endl;
cin >> exitCond; // user input for difficulty level
}
PrintScore(numCorrect, numAsked); // calls function to print score
return 0;
}
int NewRandomNumber(int n)
{
int val;
val = rand() % n + 2; // creates a number between 2 and n
return val;
}
void MakeQuestion(int n, int& a, int& b, int& atimesb)
{
a = NewRandomNumber(n);
b = NewRandomNumber(n);
atimesb = a*b;
return;
}
bool UserAnswerIsCorrect(int a, int b, int atimesb)
{
int userAns;
cout << a << "X" << b << "=" << endl;
cin >> userAns;
if (userAns == atimesb)
{
cout << "Correct!";
return true;
}
else
{
cout << "false, correct answer is:" << atimesb << endl;
return false;
}
}
void PrintScore(int numCorrect, int numAsked)
{
cout << "your score is: " << numCorrect << "/" << numAsked << " or " <<
(numCorrect / numAsked) * 100 << "%" << endl;
return;
}

UserAnswerIsCorrect(a, b, atimesb); // calls function to ask question and evaluate it
if (UserAnswerIsCorrect) // update if correct
{
numCorrect++;
}
should be
if (UserAnswerIsCorrect(a, b, atimesb)) // update if correct
{
numCorrect++;
}
You ignored the return value of UserAnswerIsCorrect in your code.

You can do this
bool corr;
corr = UserAnswerIsCorrect( a, b, atimesb);
if(corr) {
numCorrect++;
}
Although it just happened because you was ignoring the return value.

try this condition
if(UserAnswerIsCorrect == true){
....
}

Related

How to stop the loop from printing the *?

I have created a project that breaks a big number to its roots, it works very well, but it prints an extra * at the end of the last root.
#include <iostream>
using namespace std;
int multinum(int a, int b);
int primeOp(int a);
int main()
{
char ch;
do {
int a=0, b=0;
multinum(a,b);
cout << "\n\nDo you want to continue?(y/n)";
cin >> ch;
} while (ch=='y');
return 0;
}
int multinum(int num1, int num2)
{
cout<< "\nPlease enter the first number : ";
cin >> num1;
cout << "\nPlease enter the second number : ";
cin >> num2;
cout << num1 <<" = ";
primeOp(num1);
cout << endl;
cout << num2 <<" = ";
primeOp(num2);
return 0;
}
int primeOp(int a)
{
int i, x, power;
x=a;
if (a%2==0)
{
power=0 ;
while(a%2==0)
{
a/=2;
power++;
}
cout << 2 <<"^"<<power<< "*";
}
for (i=3; i<=x/2; i+=2)
{
power=0 ;
while(a%i==0)
{
a/=i;
power++;
}
if (power!=0)
cout << i <<"^"<< power << "*";
if (power!=0 && a%i== 0)
cout << "*";
}
if(a==x)
cout<< x << "^" << 1;
return 0;
}
I tried to print * in different ways but none of them had any effect, I also tried to stop printing by the use of the last "i" or "power" but it was useless.
What should I do, to stop the * bring printed when it's not needed?
Example: 24 = 2^3 * 3^1 * --- it should become: 24 = 2^3*3^1
To be able to print something only sometimes you need to print it under an if, and you need a condition that will control that print. A bool flag should do the trick. The other part of the trick is to print the asterisk before the next component, not after.
void PrintComponent(int root, int power, bool& printStar)
{
if (printStar)
cout << " * ";
cout << root << "^" << power;
printStar = true;
}
int primeOp(int a)
{
int i, x, power;
bool printStar = false;
x = a;
if (a % 2 == 0)
{
...
PrintComponent(2, power, printStar);
}
for (i = 3; i <= x / 2; i += 2)
{
...
if (power != 0)
PrintComponent(i, power, printStar);
}
if (a == x)
PrintComponent(x, 1, printStar);
return 0;
}
If finding the last print is not easy make the first print special.
Print the first power like this:
cout << 2 <<"^"<<power;
Then print all the rest via
cout << "*2^"<<power;
I dont understand your code fully, but to know it is the first print you can use a boolean flag.
You can suppres this issue by printing backspaces at the end of the result.
In primeOp add:
cout <<"\b \b";
Just above return statement

How can I set this so I can't enter any more than four numbers for cin?

I'm trying to make a simple user input. I tried to set it up so there would be four numbers entered by the user. It works for four inputs from user. It does not end after four separate numbers. Also managed to find out that I can trigger an endless repeating loop if one really long number is entered. Then I have to press cntrl+C to stop the code from running. This is in Microsoft Visual Studio if that is important.
#include <iostream>
using namespace std;
void GameBoy ()
{
cout<< "\nYou think you are this badass hacker so...." <<endl;
cout<< "Please enter the correct combination of numbers..." <<endl;
int a {};
int b {};
int c {};
int d {};
cin >> a >> b >> c >> d;
double sum = a + b + c + d;
int prod = a * b * c * d;
double average = sum / 4;
cout << average << endl;
if (sum != average && average == sum)
{
cout << "You're a goober!!" << endl;
}
else
{
cout << "You're still an goober :-P" << endl;
}
}
int main()
{
while (true)
{
GameBoy ();
}
return 0;
}
You can modify your function to return bool (true if all numbers are in the correct range, false if some number is out of range). Something like this:
#include <iostream>
using namespace std;
bool GameBoy() {
cout << "\nYou think you are this badass hacker so...." << endl;
cout << "Please enter the correct combination of numbers..." << endl;
int a{};
int b{};
int c{};
int d{};
cin >> a >> b >> c >> d;
double sum = a + b + c + d;
int prod = a * b * c * d;
double average = sum / 4;
cout << average << endl;
if (sum != average && average == sum) {
cout << "You're a goober!!" << endl;
} else {
cout << "You're still an goober :-P" << endl;
}
// if (Correct condition)
// return true;
// else Wrong condition
// return false
}
int main() {
while (GameBoy()) {
}
return 0;
}
I feel silly now. Just finished posting this question and found out what I needed to stop that endless loop. Once I added this then the issue stopped. Wow won't forget that!.
cin.clear();
cin.ignore();

how can i know what is the result of the non-void function without executing it

the problem that in the main body the getwhattheywant function is executing twice what I want is this
getwhattheywant execute then the user entered one then if the user entered one do the summation operation but what is happening with me that it's reasking the user to enter a number.
#include <iostream>
using namespace std;
double dothesum(int x, int y)
{
int sum = x + y;
return sum;
};
int getwhatheywant()
{
int choice;
cout << "1- for sum " << endl;
cout << "2- for quit ";
cin >> choice;
return choice;
}
void the_sum()
{
int x, y;
cout << " enter the first number " << endl;
cin >> x;
cout << " enter the second number " << endl;
cin >> y;
cout << " the sum of the two number is " << dothesum(x, y) << endl;
}
int main()
{
int;
while (getwhatheywant() != 2) {
if (getwhatheywant() == 1) {
the_sum();
}
}
return 0;
}
Change your main():
int main()
{
int whatTheyWant;
while ( (whatTheyWant = getwhatheywant()) != 2) {
if (whatTheyWant) == 1) {
the_sum();
}
}
return 0;
}
This stores the value from a single call to getwhattheywant() so you can first see if they're asking to quit, and if not, you can see what else they might want. Now, I'd write it slightly differently:
bool working = true;
while(working) {
int choice = getWhatTheyWant();
switch(choice) {
case 1: the_sum(); break;
case 2: working = false; break;
}
}

C++ Creating array based on what functions were called

For example lets say I have this simple code:
using namespace std;
char re = 'y';
void mult(int one, int two)
{
int mult = 1;
mult = one * two;
cout << mult << endl;
}
void add(int one, int two)
{
int add = 0;
add = one + two;
cout << add << endl;
}
void rep(int one, int two)
{
}
void ask()
{
int re;
cout << "do you want return to the menu? (1/2)" << endl;
cin >> re;
}
int main()
{
char re;
int one;
int two;
cout << "enter the number one:" << endl;
cin >> one;
cout << "enter the number two:" << endl;
cin >> two;
cout << endl;
cout << "Multiply - 1" << endl;
cout << "Add - 2" << endl;
cout << "Reprint - 3" << endl;
cout << endl;
int menu;
if (re == 'y')
{
cout << "select the function ";
cin >> menu;
switch (menu)
{
case 1:
mult(one, two);
ask();
break;
case 2:
add(one, two);
ask();
break;
case 3:
rep(one, two);
break;
default:
cout << "no such thing" << endl;
break;
}
}
else if (re != 'y')
{
}
return 0;
}
I need a way function rep to print out the answer of previously called functions.
For example if function mult was called it should print mult answer, IF mult and add were called then it should print mult and add function answers, if only add then only add.
I was thinking about creating a zero array and changing it whether function one or two was called out, and then somehow call the answers out. But no idea how to do it.
You could use a global variable.
int last_result = 0;
void mult(int a, int b)
{
last_result = a * b;
std::cout << last_result << std::endl;
}
void add(int a, int b)
{
last_result = a + b;
std::cout << last_result << std::endl;
}
void rep()
{
std::cout << "Last result: " << last_result << std::endl;
}
You could also have the mult and add functions return a result and store the result in main (and pass it to the rep function).
You could pass the last_result variable by reference to the add and mult functions.

How do I loop a variable?

I have made a "program" that just says welcome! Type two numbers you want to be added to each other:
there you type the two numbers and then you get the answer out...
When that is done it says: Press any key to continue . . .
When you press a key the program shuts down, but I want it to restart when you pres any key...
How do I do that? I use Microsoft visual studio express 2013 for windows desktop...
langue is C++
This is my code:
#include <iostream>
#include <limits>
#include <cstdio>
using namespace std;
int Add(int x, int y)
{
cout << "Calculating the sum of " << x << " + " << y << "\n";
return (x + y);
}
int main()
{
cout << " Welcome!\n";
int a, b, c;
cout << "Type two numbers you want to be added to each other: ";
cin >> a;
cin >> b;
c = Add(a, b);
cout << "The answere is: " << c;
cout << "\nShutting down....\n\n";
system("pause");
return 0;
}
To loop, you can use while.
For example:
while (false) {
std::cout << "You will never see this output" << std::endl;
}
bool loop = true;
while (loop) {
std::cout << "Type 'quit' to quit this loop." << std::endl;
std::string input;
// This will grab a *single word* from the input. If you want a line, look
// at std::getline
std::cin >> input;
if (input == "quit") {
loop = false;
}
}
while (true) {
std::cout << "This will be repeated forever" << std::endl;
}
There are also two other forms, do while:
std::string input;
do {
std::cout << "Type 'quit' to quit." << std::endl;
std::cin >> input;
} while (input != "quit");
... and for (which is generally used for loop over a defined list of things):
for (size_t i = 0; i < 10; ++i) {
std::cout << i << " out of 10" << std::endl;
}
Technically you can use any of these loop types for any kind of looping, but I suspect the type you want is either one of the two standard infinite loops (whichever one you prefer):
while (true) {
// stuff to repeat forever
}
for (;;) {
// stuff to repeat forever
}
... or a do while loop similar to the do { ... } while (input != "quit"); loop above.
int main()
{
cout << " Welcome!\n";
int a, b, c;
while (true)
{
cout << "Type two numbers you want to be added to each other: ";
cin >> a;
cin >> b;
c = Add(a, b);
cout << "The answere is: " << c;
cout << "\nPress a key to go again....\n\n";
system("pause");
};
return 0;
}
You can do something like this:
int main()
{
cout << " Welcome!\n";
int a, b, c;
while(true) {
cout << "Type two numbers you want to be added to each other: ";
cin >> a;
cin >> b;
c = Add(a, b);
cout << "The answere is: " << c;
cout << "\nPress any key to continue\n";
system("pause");
}
return 0;
}
Use a do while loop mentioning your condition
before exiting the program so that you can determine when to continue and when to exit
I am not sure I understand your question, but I think this is what you are looking for. Have a boolean that determines if the program will loop or not.
int main() {
// stillRun is true while we want to keep looping the program
boolean stillRun = true;
while(stillRun) {
runProgram() ; // this function has all the other code in your old main() function
cin >> stillRun ;
}
}