The main problem with your first program was that you declared int guess at the beginning of main, and then you declared another int guess inside the do-while loop. The second one is a local variable that exists only inside that loop and is completely independent of the first "guess" which is controlling the loop. So the program would have worked if you simply eliminated the keyword "int" inside the loop. But also, as nimitzhunter said, you should only call srand once, and do it before calling rand. Also, if you seed with 0, you will always get the same series of "random" numbers. That's why you should seed with time(NULL). Like this:
Code:
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int myNumber,tries=0, guess;
srand(time(NULL));
cout<<"What's your secret number 1-100 : ";
cin>>myNumber;
do
{
guess = (rand() %100) +1;
cout<<guess<<endl;
++tries;
}
while (myNumber!=guess);
cout<<"\nDang it YOU GUESSED IT\n";
cout<<"It took you "<<tries<<" tries.";
return 0;
}
The second error is because cin doesn't "know how" to handle your enum. But since the enumerated values are just aliases for ints, you should read the input into an int and use that in the comparisons, like this:
Code:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";
enum difficulty {Easy=1,Normal,Hard};
difficulty myDifficulty=Easy;
cout<<"\nWhat Difficulty Would You like ? \n";
cin>>number;
if (number==Easy)
cout<<"Easy for You\n";
else if (number==Normal)
cout<<"Normal, Just Right\n";
else
cout <<"Hardcore\n";
return 0;
}
Notice also that you needed "else" before the second "if"; otherwise, the code
Code:
if (number==Normal)
cout<<"Normal, Just Right\n";
else
cout <<"Hardcore\n";
would be executed regardless of the result of the first "if" statement.