-3
while(guess!=ans1){
        cout << "Enter your first guess: ";
        cin >> guess;
    } 

This is the loop I am using but its not working can someone please tell me how I fix this?? (I am not using an ide btw.)

EDIT

Full code

#include <iostream>
using namespace std;

int main()
{
    int ans1 = 3;
    int ans2 = 7;
    int ans3 = 12;
    int guess;
    while (guess != ans1)
    {
        cout << "Enter your first guess: ";
        cin >> guess;
    }
    cout << "\nYou guessed correctly!";
    while (guess != ans2)
    {
        cout << "Enter your second guess: ";
        cin >> guess;
    }
    cout << "\nYou guessed correctly!";
    while (guess != ans3)
    {
        cout << "Enter your final guess: ";
        cin >> guess;
    }
    cout << "\nYou win!!!";
    return 0;
}
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
the
  • 9
  • 3
  • Please post your entire code. – infinitezero Aug 06 '22 at 06:34
  • @infinitezero okay #include using namespace std; int main() { int ans1 = 3; int ans2 = 7; int ans3 = 12; int guess; while(guess!=ans1){ cout << "Enter your first guess: "; cin >> guess; } cout << "\nYou guessed correctly!"; while(guess!=ans2){ cout << "Enter your second guess: "; cin >> guess; } cout << "\nYou guessed correctly!"; while(guess!=ans3){ cout << "Enter your final guess: "; cin >> guess; } cout << "\nYou win!!!"; return 0; } – the Aug 06 '22 at 06:35
  • Also take a look at this: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – infinitezero Aug 06 '22 at 06:35
  • @infinitezero ok thanks I will try removing the namespace and doing it directly. – the Aug 06 '22 at 06:38
  • 2
    `guess` is uninitialized the first time you attempt to compare it with `ans1`. This is UB (undefined behavior). – wohlstad Aug 06 '22 at 06:38
  • Your code works fine for me (well, the spacing is funny). What do you see? – Tim Roberts Aug 06 '22 at 06:38
  • Please don't add "solved" to the title. Either delete the question if you think it's not useful to others, or press the checkmark next to an answer to mark it as solved. – HolyBlackCat Aug 06 '22 at 06:47
  • @HolyBlackCat Ok I will remember that. – the Aug 06 '22 at 06:50
  • Why would you learn C++ Anti-Tank Missiles? – user4581301 Aug 06 '22 at 06:51

2 Answers2

1

Look at this code (with my comments)

int guess; 
while(guess!=ans1)     // <-- here the variable guess is checked
{ 
   cout << "Enter your first guess: "; 
   cin >> guess;       // <-- here the variable guess is first given a value
} 

Do you see the problem? You are checking the variable before it has been given a value. You can only access a variable after it has been given a value.

This code is better

int guess;
do
{ 
   cout << "Enter your first guess: "; 
   cin >> guess;
} 
while(guess!=ans1);

Now the variable guess is being checked after it gets a value not before.

john
  • 85,011
  • 4
  • 57
  • 81
  • Ok nice I think I will do that. Although I did realize that it wasn't working because I forgot to save the file after I made a typo. – the Aug 06 '22 at 06:46
0

I offer you some alternatives:

#include <iostream>

int main()
{
    using std::cout; // this allows you to only use cout instead of std::cout
    using std::cin; // and not import the entire namespace. Also, only within 
                   // the main function. So no global pollution.
    
    int const ans1 = 3; // these do not change, so they should be const
    int const ans2 = 7;
    int const ans3 = 12;
    int guess = 0;
    while (guess != ans1)
    {
        cout << "Enter your first guess: ";
        cin >> guess;
    }
    cout << "\nYou guessed correctly!\n";
    while (guess != ans2)
    {
        cout << "Enter your second guess: ";
        cin >> guess;
    }
    cout << "\nYou guessed correctly!";
    while (guess != ans3)
    {
        cout << "Enter your final guess: ";
        cin >> guess;
    }
    cout << "\nYou win!!!";
}

But surely you notice we copied a lot of code. This is where we can make use of arrays. A lot of free tutorials will teach you C-style arrays, but this is the C++ way to do it:

#include <array>
#include <iostream>

int main()
{
    using std::array;
    using std::cout;
    using std::cin;
 
    // alternatively
    
    array<int, 3> const answers = {3, 7, 12};
    int guess = 0;
    
    for( std::size_t i = 0; i < answers.size(); i++ ){ // (iterating loop, the "standard" for loop
        while( guess != answers[i] ){
            cout << "Enter your first guess: ";
            cin >> guess;
        }
        cout << "\nYou guessed correctly!\n";    
    }
    
    // alternatively
    
    for( auto const& answer : answers ){ // range based loop (more complicated to understand)
        while( guess != answers ){
            cout << "Enter your first guess: ";
            cin >> guess;
        }
        cout << "\nYou guessed correctly!\n";    
    }
    
    
    return 0;
}
infinitezero
  • 1,610
  • 3
  • 14
  • 29
  • Thanks. You should keep the checkmark on the other answer (because they actually answered your question) but you can still give my answer an upvote :) – infinitezero Aug 06 '22 at 07:01