Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: C++ help pls

(130 views • 2 replies)

This topic is 1 page long.

<< < > >>
None

klaw224

Reply To Post Reply & Quote

Posted at: 5/7/08 12:56 AM

klaw224 EVIL LEVEL 09

Sign-Up: 08/18/05

Posts: 111

Okay so im gonna make a guess my number game but i still dont know what this code does.

Code:

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

using namespace std:

int main()
{
srand(time(0));

int theNumber = rand() % 100 + 1;
int tries = 0, guess;

cout << "\t Welcome to guess my number";

do
{

cout << "enter a guess: ";
cin >> guess;
++tries

if (guess > theNumber)
cout << Too high!\n\n";

if (guess < theNumber)
cout << Too low!\n\n";

} while (guess != theNumber);

cout << "\nThat's it! You got it in " << tries << "guesses!\n";

return 0;
}

Help pls :(


None

slimt2100

Reply To Post Reply & Quote

Posted at: 5/7/08 03:27 AM

slimt2100 EVIL LEVEL 13

Sign-Up: 12/23/03

Posts: 173

The first thing I notice is that using namespace std has a colon at the end, not a semicolon, and ++tries has no ; at the end. Also, instead of two if statements, make it an if - else if statement (no big deal, just more efficient that way). Also (I'm not sure what the correct ways are on C++) but the argument in time shouldn't just be 0, it should be NULL or '\0', but, as I said, I am just going off what I know from C in regards to that. Just put NULL to be safe, as I know for sure that works (and it's not a bad habit of getting into anyway. Also, little things like new lines at the end of the Welcome message and a space before "guesses" in the last output message are just a few things I saw when I glanced through it.


None

Silkey

Reply To Post Reply & Quote

Posted at: 5/9/08 06:58 PM

Silkey NEUTRAL LEVEL 10

Sign-Up: 07/23/06

Posts: 148

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

using namespace std;

int main()
{
srand(time(0));

int theNumber = rand() % 100 + 1;
int tries = 0; // int tries = 0,guess; What are you trying to do here?

cout << "\t Welcome to guess my number";

do
{

cout << "enter a guess: ";
cin >> guess;
tries ++;

if (guess > theNumber)
cout << Too high!\n\n";

if (guess < theNumber)
cout << Too low!\n\n";

} while (guess != theNumber);

cout << "\nThat's it! You got it in " << tries << "guesses!\n";

return 0;
}

All times are Eastern Daylight Time (GMT -4) | Current Time: 08:35 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!