Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

My C++ code, any suggestions?


ghost's Avatar
0 0

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; }


ghost's Avatar
0 0

Just glanced over it, this will prolly do the trick include the right headers for the string and random function


reaper4334's Avatar
Member
0 0

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 = &quot;Higher.&#92;n&quot;;
string s2 = &quot;Lower.&#92;n&quot;;
srand((unsigned)time(0));
int i1 = ((rand() % 100 - 1) + 1 + 1);
cout&lt;&lt;&quot;Guess any number between 1 and 100.&#92;n&quot;;
int i2;
cin &gt;&gt; i2;
while(i2 != i1){
cout&lt;&lt;&quot;Guess any number between 1 and 100.&#92;n&quot;;

if(i2 &gt; 100){
cout&lt;&lt;&quot;That number is too high. Please enter a number between 1 and 100.&#92;n&quot;;
cin &gt;&gt; i2;
}
else if(i2 &lt; 1){
cout&lt;&lt;&quot;That number is too low. Please enter a number between 1 and 100.&#92;n&quot;;
cin &gt;&gt; i2;
}
else if((i2 &gt; 1) && (i2 &lt; i1)){
cout&lt;&lt;s1;
cin &gt;&gt; i2;
}
else if((i2 &lt; 100) && (i2 &gt; i1)){
cout&lt;&lt;s2;
cin &gt;&gt; i2;
}
}


cout&lt;&lt;&quot;You got it!&#92;n&quot;;
system(&quot;pause&quot;);
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.


ghost's Avatar
0 0

Thanks for your help guys. =)


ynori7's Avatar
Future Emperor of Earth
0 0

like Anarcho-Hippie said, you're missing header files. i think you need to include string.h and time.h


ghost's Avatar
0 0

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?


ynori7's Avatar
Future Emperor of Earth
0 0

@swartmumba

didnt work for me```

but i agree that it&#39;s annoying that people never disable smileys. maybe it should default on disabled and you have to enable smileys.

reaper4334's Avatar
Member
0 0

ynori7 wrote: @swartmumba

didnt work for me```

but i agree that it&#39;s annoying that people never disable smileys. maybe it should default on disabled and you have to enable smileys.

I think that&#39;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&#39;t need any others. Must be Dev-C++ including string.h on its own or something, anyone know if it does do that?

ynori7's Avatar
Future Emperor of Earth
0 0

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.


yours31f's Avatar
Retired
10 0

#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.


ghost's Avatar
0 0

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.


yours31f's Avatar
Retired
10 0

lol use his sig ^^^^