Hi, I'm new to C++, and I made this program that makes either 1, or 2 pop up. And for 1, a text field is suppost to pop up to show you won, and if 2 pops up, it's suppost to say you lose. I got everything down (The unmber generator and stuff) But I'm having trouble with the IF and ELSE statement... The IF statement works fine, but my ELSE statement doesn't show up.... Here is the code....

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
     system("CLS");
     cin.clear();
     
 
    srand((unsigned)time(0)); 
    int random_integer; 
    for(int index=0; index<1; index++){ 
        random_integer = (rand()%2)+1; 
        cout << random_integer << endl; 
        if(index=1)
        {
        cout << "You won!" << endl << endl;
        }else{      
              cout << "You lose...."<< endl << endl;
        }
        system("PAUSE");
        return 0;
    } 
}

Please if anyone can point out what is wrong, please tell me, thx :D

Recommended Answers

All 4 Replies

line 18: use the boolean operator == instead of the assignment operator =.

Now only the ELSE statement is coming up... It shows the number and says "You Lose...." for both numbers....????

back to that same line: what is index? shouldn't it be random_integer ?

Omgs it works, thx lots!!!! Nice pic too :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.