My C++ code, any suggestions?
I wrote some code in C++ that was supposed to choose a random number between 1 and 100 and the user had to guess the number. If the user guessed too high or too low it would inform them accordingly. Also if the user input a number above 100 or below 1 it would inform them to that as well. Anyways here's the code. I got 22 errors and 2 warnings compiling it, so any help would be greatly appreciated. Thanks in advance! =)
#include <iostream>
using namespace std;
int main() { string s1 = "Higher.\n"; string s2 = "Lower.\n"; int i1 = rand(1*100) cout<<"Guess any number between 1 and 100.\n"; cin>>int i2 while(i2 != i1){ cout<<"Guess any number between 1 and 100.\n"; } if(int i2 > 100){ cout<<"That number is too high. Please enter a number between 1 and 100.\n"; } else if(int i2 < 1){ cout<<"That number is too low. Please enter a number between 1 and 100.\n"; } else if(i2 > 1)&& (i2 < i1){ cout<<s1 } else if(i2 < 100)&& (i2 > i1){ cout<<s2 } else{ cout<<"You got it!"; } return 1; }
Ok, I edited your code. It's still far from perfect.. but it does roughly what you were trying. There are still some things it does wrong.. but it compiles and does the random number.
using namespace std;
int main()
{
string s1 = "Higher.\n";
string s2 = "Lower.\n";
srand((unsigned)time(0));
int i1 = ((rand() % 100 - 1) + 1 + 1);
cout<<"Guess any number between 1 and 100.\n";
int i2;
cin >> i2;
while(i2 != i1){
cout<<"Guess any number between 1 and 100.\n";
if(i2 > 100){
cout<<"That number is too high. Please enter a number between 1 and 100.\n";
cin >> i2;
}
else if(i2 < 1){
cout<<"That number is too low. Please enter a number between 1 and 100.\n";
cin >> i2;
}
else if((i2 > 1) && (i2 < i1)){
cout<<s1;
cin >> i2;
}
else if((i2 < 100) && (i2 > i1)){
cout<<s2;
cin >> i2;
}
}
cout<<"You got it!\n";
system("pause");
return 0;
}
Next time mayble look at a few tutorials or simply google the function your using. If none of the above, at least post what some of you errors are next time.
didnt work for me```
but i agree that it's annoying that people never disable smileys. maybe it should default on disabled and you have to enable smileys.
ynori7 wrote: @swartmumba
didnt work for me```
but i agree that it's annoying that people never disable smileys. maybe it should default on disabled and you have to enable smileys.
I think that's what h meant.. not sure though haha.
and Yeah, the header files is a good point.. when I edited his code though, I didn't need any others. Must be Dev-C++ including string.h on its own or something, anyone know if it does do that?
reaper4334 wrote: I think that's what h meant.. not sure though haha. and Yeah, the header files is a good point.. when I edited his code though, I didn't need any others. Must be Dev-C++ including string.h on its own or something, anyone know if it does do that?
i dont remember, i havnt used dev in a while. i switched to visual studio, but i wouldnt think it would automatically include time.h which is what you need for random. whatever.
#include <iostream>
using namespace std;
int main() { string s1 = "Higher.\n";
string s2 = "Lower.\n";
int i1 = rand(1*100); // forgot a ; here
cout<<"Guess any number between 1 and 100.\n";
cin>>int i2; // one here
while(i2 != i1){ cout<<"Guess any number between 1 and 100.\n";
} if(int i2 > 100){ cout<<"That number is too high. Please enter a number between 1 and 100.\n"; } if(int i2 < 1){ cout<<"That number is too low. Please enter a number between 1 and 100.\n"; } if(i2 > 1)&& (i2 < i1){ cout<<s1 } if(i2 < 100)&& (i2 > i1){ cout<<s2 } else{ cout<<"You got it!"; } return 1; }
use that and get a new rand operator.
SwartMumba wrote: Wouldn't it be great if a person were to include the code tag in their post, then the "Disable Smileys in this Post" check box would automatically become checked?
The best solution would be to disable smileys, but only within the code tags. You could use smileys throughout your post, but if they were between the code and /code tags they would appear as text.