Is there a way to hide variables in cout (C++)? -
i'm having issue figuring out how hide specific variable (if possible) in cout function. need number guess game, easy enough except our teacher wants random math equation. which... still easy except way have program has randomly create problem , randomly pick 1 of 3 numbers display , user has guess other 2 missing numbers. example if program picked 20 + 32 = 52 potentially display __ + 32 = __.
i've gotten far can't figure out how make displays still allows me put line this
cout << num1 //hidden << " + " << num2 << " = " << num3 //hidden however said don't know if possible if not have rewrite whole program. have far:
int main() { int num1, num2, num3, random1, guess1, guess2; string play = ""; cout << "would run number guessing program? (enter yes or no): "; getline(cin, play); (int = 0; < play.length(); i++) { play[i] = tolower(play[i]); } //random seed srand(time(0)); while (play == "yes") { //generate random numbers , 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 information please make educated guess 2 missing numbers are."; cout << "\n\nguess number 1 (between 1 , 100): "; cin >> guess1; while ((guess1 > 100) || (guess1 < 0)) { cout << "\nsorry need enter integer between 1 , 100" << endl; cout << "\nguess number 1 (between 1 , 100): "; cin >> guess1; } cout << "\n\nguess number 2 (between 1 , 100): "; cin >> guess2; while ((guess2 > 100) || (guess2 < 0)) { cout << "\nsorry need enter integer between 1 , 100" << endl; cout << "\nguess number 2 (between 1 , 100: "; cin >> guess2; } if (guess1 == ) } return 0; }
i don't think can hide variables in cout. can use variable instead of hardcoding "__".
for instance, can 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 output variables. if player guesses correctly, it'll display actual value num1, num2, or num3. if not, it'll display "__".
Comments
Post a Comment