I'm having an issue figuring out how to hide a specific variable (if that is even possible) in a cout function. Basically I need to do a number guess game, which would be easy enough except our teacher wants us to do it like a random math equation. Which... would honestly still be fairly easy except the way we have to do it is the program has to randomly create the problem and then randomly pick one of the 3 numbers to display and the user has to guess the other two missing numbers. For example if the program picked 20 + 32 = 52 it could potentially display __ + 32 = __.
I've gotten that far however I can't figure out how to make it so it displays like that but still allows me to put the line something like this
cout << num1 //Hidden << " + " << num2 << " = " << num3 //Hidden
However like I said I don't even know if that is possible if not then I will probably have to rewrite the whole program. This is what I have so far:
int main()
{
int num1, num2, num3, random1, guess1, guess2;
string play = "";
cout << "Would you like to run the number guessing program? (enter yes or no): ";
getline(cin, play);
for (int i = 0; i < play.length(); i++)
{
play[i] = tolower(play[i]);
}
//Random seed
srand(time(0));
while (play == "yes")
{
//Generate random numbers and num3
num1 = 1 + rand() % 50 + 1;
num2 = 1 + rand() % 50 + 1;
num3 = num1 + num2;
int pickRandom[3] = { num1, num2, num3 };
//Display random elements
random1 = pickRandom[rand() % 3];
if (random1 == num1){
cout << "\nYour randomly generated number problem: " << num1 << " + " << "__" << " = " << "__" << endl;
}
if (random1 == num2){
cout << "\nYour randomly generated number problem: " << "__" << " + " << num2 << " = " << "__" << endl;
}
if (random1 == num3){
cout << "\nYour randomly generated number problem: " << "__" << " + " << "__" << " = " << num3 << endl;
}
//Get Guesses
cout << "\nBased off of this information please make an educated guess as to what the two missing numbers are.";
cout << "\n\nGuess for number 1 (between 1 and 100): ";
cin >> guess1;
while ((guess1 > 100) || (guess1 < 0))
{
cout << "\nSorry you need to enter an integer between 1 and 100" << endl;
cout << "\nGuess for number 1 (between 1 and 100): ";
cin >> guess1;
}
cout << "\n\nGuess for number 2 (between 1 and 100): ";
cin >> guess2;
while ((guess2 > 100) || (guess2 < 0))
{
cout << "\nSorry you need to enter an integer between 1 and 100" << endl;
cout << "\nGuess for number 2 (between 1 and 100: ";
cin >> guess2;
}
if (guess1 == )
}
return 0;
}
I don't think you can hide variables in cout. But you can use a variable instead of hardcoding "__".
For instance, you can simply write this:
if(guessed_var1_correctly)
var1 = num1
else
var1 = "__"
if(guessed_var2_correctly)
var2 = num2
else
var2 = "__"
if(guessed_var3_correctly)
var3 = num3
else
var3 = "__"
cout << "\nYour randomly generated number problem: " << var1 << " + " << var2 << " = " << var3" << endl;
where var1, var2, var3 are output variables. If the player guesses it correctly, it'll display the actual value num1, num2, or num3. If not, it'll simply display "__".
Related
I'm writing a math quizzing program that does addition, subtraction, multiplication, and division. In my subtraction function, I can't figure out how to ignore a user inputted "-" sign on the cin >> unserInput portion, as it's not always required (like if it's positive).
I have an if statement that if the machine calculated resultant is negative, to have the ability to cin the "-" char, but the program crashes if the user omits it. How can I validate if the "-" char is present, ignore it if it is and continue to pull in int values, and if it's not present, just pull in the values?
void subFractions(int num1 , int num2 , int denom1 ,int denom2 ,int answerNum
, int answerDenom , int resultNum , int resultDenom , int number , int
numCorrect = 0)
{
// use random number to work
srand(time(NULL));
// begin counter
number = 1;
// count number of math problems to be pushed to user
while ( number < 11)
{
//Assigning the numerators and denominators to random numbers
//between 1 to 10
num1 = rand() % 10 + 1; // numerator 1
num2 = rand() % 10 + 1; // numerator 2
denom1 = rand() % 10 + 1; // denom 1
denom2 = rand() % 10 + 1; // denom 2
char sign; // operator sign (/)
char neg; // pull negative sign in if required
// push header to user
cout << "Question " << number << ": What is the result of the
following?" << endl;
cout << right << setw(3) << num1 << right << setw(8) << num2 << endl;
cout << right << setw(3) << " ---" << right << setw(3) << "-" <<
right << setw(5) << " ---" << endl;
cout << right << setw(3) << denom1 << right << setw(8) << denom2 <<
endl;
//Calculate resultant
resultNum = ( ( num1 * denom2 ) - (denom1 * num2 ) );
resultDenom = ( denom1 * denom2 );
double resultant = resultNum / resultDenom;
//getting the answer from the user
cout << "Enter your answer in the form numerator / denominator: ";
// check resultant for negative number
if (resultant < 0)
{
cout << "neg answer" << endl;
cin >> neg >> answerNum >> sign >> answerDenom;
answerNum = (-1) * answerNum;
cout << endl;
} // end negative number check
else
{
cout << "pos answer" << endl;
cin >> answerNum >> sign >> answerDenom;
cout << endl;
} // end positive number check
double userResultant = answerNum / answerDenom;
//if the answer is right
if (resultant == userResultant)
{
cout << "Correct!" << endl;
numCorrect++;
} // end correct statement
else
{
cout << "That is not correct. " << endl;
//Reshowing the fraction with answer of the numerator
cout << right << setw(3) << num1 << right << setw(8) << num2 <<
right << setw(8) << resultNum << endl;
//Showing the fraction and the symbols
cout << right << setw(3) << " ---" << right << setw(3) << "-" <<
right << setw(5) << " ---" <<
right << setw(3) << "=" << right << setw(5) << "---" << endl;
//Showing the denominator with answer of the denominator
cout << right << setw(3) << denom1 << right << setw(8) << denom2
<< setw(8) << resultDenom << endl;
cout << "" << endl;
} // end incorrect statement
If the user does not enter the "-", it should kick out the incorrect statement. As it is, it's just crashing. Presumably because it's loading values into the incorrect variable and leaving the last one empty.
I am writing a program to simulate a dice roll with random function.
This is my code, but I am having infinite loop.
The program is supposed to ask the user how many times they would like to roll the dice.
Then I used a for loop to roll the dice from 1 to 6
and I put all that in a do-while in order to only allow the user to select between 1 and 6 , if the selection is outside of 1 to 6 it is supposed to say it is an invalid selection.
I am not allowed to use functions or arrays on this
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//i is the counter for the loop
int i = 0;
//store total count for landing number
int num1 =0;
int num2 =0;
int num3 =0;
int num4 =0;
int num5 =0;
int num6 =0;
int Num1 =0;
int Num2 =0;
int Num3 =0;
int Num4 =0;
int Num5 =0;
int Num6 =0;
int randNum;
int times;
//User selection
int selection;
/* initialize random seed: */
srand ((unsigned int)time(NULL));
/* generate random number: */
randNum = rand() % 6 + 1;
cout<<"How many times would you like to roll the dice? " << endl;
cin >> selection;
do{
for(i = 1; i <= selection; i++)
{
if(randNum==1)
{
num1++;
}
else if(randNum==2)
{
num2++;
}
else if(randNum==3)
{
num3++;
}
else if(randNum==4)
{
num4++;
}
else if(randNum==5)
{
num5++;
}
else if(randNum==6)
{
num6++;
}
}
Num1 = num1/times *100;
Num2 = num2/times *100;
Num3 = num3/times *100;
Num4 = num4/times *100;
Num5 = num5/times *100;
Num6 = num6/times *100;
cout <<"# Rolled \t # Times \t % Times" << endl;
cout <<"----- \t ------- \t -------" << endl;
cout <<" 1 \t " << num1 << "\t " << fixed << setprecision(2) << Num1 << "%\n";
cout <<" 2 \t " << num2 << "\t " << fixed << setprecision(2) << Num2 << "%\n";
cout <<" 3 \t " << num3 << "\t " << fixed << setprecision(2) << Num3 << "%\n";
cout <<" 4 \t " << num4 << "\t " << fixed << setprecision(2) << Num4 << "%\n";
cout <<" 5 \t " << num5 << "\t " << fixed << setprecision(2) << Num5 << "%\n";
cout <<" 6 \t " << num6 << "\t " << fixed << setprecision(2) << Num6 << "%\n";
}
while(i >= 1 || i <= 6);
{
cout << "This is an invalid number. \n"
<< "The number of rolls should be equal to or greater than 1.\n"
<< "Please enter again.\n";
}
}
Your conditional statement: i = 1; i <= selection; i++ should go in the final while you have.
Try:
do {
if (randNum==1) {
num1++;
} else if(randNum==2) {
num2++;
} else if(randNum==3) {
num3++;
} else if(randNum==4) {
num4++;
} else if(randNum==5) {
num5++;
} else if(randNum==6) {
num6++;
}
i += 1;
} while (i <= selection);
The infinite loop is the do - while...
I'm not sure what you were trying to do there, but you will always get a number greater than 1 or smaller than 6 in your test - you can't fail both...
I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.
There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.
int main()
{
int NumsToCal = 5;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
} while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
Xsami is absolutely right. You only need to include one more line like:
cin>>NumstoCal;
Though it won't be bad to change the way you output stuff for a bit more clarity.
Here is my code:
https://ideone.com/BXREP9
The only thing that you have to do is read NumsToCal again, and you have to do something like this after cin >> WantMore;
if ( WantMore == 'y' )
{
Num1 = 0;
Num2 = 1;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << Num1 << " " << Num2 << " " ;
}
This is my code: http://ideone.com/a8um5Z
Not a great title I know but I'm not sure how to word it. Anyway, in brief I am trying to calculate the number of scores, the total of scores and the average and grade of the scores. I am using for... loops to complete this task. So...
my function prototypes:
int validateNumber(int);
void summary(int,int,float,char);
char getGrade(float);
float getAvg(int,int);
probably only the validateNumber(int) is relevant here but just in case.
The main()
int num, total, scores;
cout << over4 << "How many scores do you want to average? " << endl;
cout << over4 << "Enter a value from 1 to 4: ";
cin >> num;
And the calls(?):
total = validateNumber(num);
summary(num,total,average,grade);
And then the definitions:
int validateNumber(int num)
{
int total = 0, score;
while (num < 1 || num > 4)
{
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++)
{
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << over3 << score << " is not between 0 and 100! Renter the score: "
<< i << ": ";
cin >> score;
}
total += score;
}
return total;
}
and:
void summary(int num,int total,float average,char grade)
{
cout << over4 << "Number of scores : " << num << endl;
cout << over4 << "Scores total : " << total << endl;
cout << over4 << "Average : " << average << "%" << endl;
cout << over4 << "Grade : " << grade << endl;
cout << down11;
}
When the user enters a num value between 1 and 4, there is no problem, the program works as it should. However when the user enters a value for num not in that range, the function works as it should BUT the summary will tell me that the number of scores was that first erroneous value and as a result mess up my average/grade.
in your function you are passing by value not by reference, so the change which is done in your function int validateNumber(int); is only in that function's scope, outside num's value is same as it was first. you should send value by reference. in this way :
int validateNumber(int &);
What happens is that you pass num to the validateNumber function by value. The local num in the validateNumber function copies the global num's value and get's processed accordingly. However, the global num variable stays as it were. In order to change the num itself you will have to pass it by reference. Change the parameters on your function definition to: int validateNumber(int &num) { ... }
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int num1, num2, num3, num4, num5, result, result1, result2, result3, value, cont;
//number user enters/variable
int main()
{
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
;cin >> value
;cout << "please enter the first number number:";
cin >> num1
;cout << "please enter the second number: ";
cin >> num2
;if(value == '+' )
;result = num1 + num2;
cout << num1 << " plus " << num2 << " is equal to: " << result << ".\n";
if(value = '-' )
;result = num1 - num2;
cout << num1 << " minus " << num2 << " is equal to: " << result << ".\n";
if(value = '*' )
;result = num1 * num2;
cout << num1 << " times " << num2 << " is equal to: " << result << ".\n";
if(value = '/' )
;result = num1 / num2;
cout << num1 << " divided by " << num2 << " is equal to: " << result << ".\n";
}
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value1
cout << "please enter the next number:";
cin num3
else(cont = 0)
else(value1 = +)
;result1 = result + num3;
cout << result << " plus " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = -)
;result1 = result - num3
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = *)
;result1 = result * num3
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = /)
;result1 = result / num3
cout << result << " divided by " << num3 << " is equal to: " << result1 << ".\n";
}
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value1
;cout << "please enter the next number: ";
cin >> num4
else(cont = 0)
else(value2 = +)
;result2 = result1 + num4;
cout << result1 << " plus " << num4 << " is equal to: " << result2 << ".\n";
else(value2 = -)
;result2 = result1 + num4;
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value2 = *)
;result2 = result1 * num4
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value2 = /)
;result2 = result1 / num4
cout << result << " divided by " << num3 << " is equal to: " << result1 << ".\n";
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value2
;cout << "please enter the next number: ";
cin >> num5
if(value3 = +)
;result3 = result2 + num5;
cout << result2 << " plus " << num5 << " is equal to: " << result3 << ".\n";
else(value3 = -)
;result3 = result2 - num5
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value3 = *)
;result3 = result2 * num5
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value3 = /)
;result3 = result2 / num5
return 0;
the error happens at line 34, where it says {
so please help me!
the code is for a basic calculator
feel free to use it if you can correct line 34!
I have no idea what is causeing it
I am a noob to the c++
codeing so please help!
I have done my own research and I cannot find it.
You can't say stuff like
else(value1 = +)
in C++. You must mean something else, but it is hard to guess what because there are so many errors in your code. In general, you cannot just type random characters and expect a functioning program.
The error stems from the additional { ... } blocks following the main function, since the compiler does not know what to do with code outside of declarations . But that is not the only problem with your code:
Putting a semicolon directly after an if statement means "if the condition is true do nothing anyway", and the next statement is executed either way.
if(value = +) should be if(value == '+') etc - you mixed the association = with comparison ==, plus you try using an operator + instead of a character '+'* what is else(something) supposed to do? Code blocks are put in {}s, not ()s
May I suggest you start programming in an easier language like Python? Its meaningful indentation and the lack of semicolons makes life a lot easier...
Get rid of all of the
}
{
and you'll be able to proceed to fixing your next error.
When you write braces like the following: }
you're closing your block of code, which in this case is your main function.
and when you write the following: {
The compiler thinks that you're trying to start a new function, but there's not function signature and you get an error.
It looks to me like you intend all of this code to be inside of your main function, so you want something like the following:
int main()
{
//insert all of your code here
return 0;
}