Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 28th, 2005, 6:28 PM   #1
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Help with some source code

Ugh, this is almost embarassing, first time posting source code. ._.

I have an error with a program I constructed. It compiles fine, I just can't seem to locate the problem. The program is suppose to continue to guess random numbers and compare it to a number that the user inputted and continue until it guesses it... Buuut it stops after the first try. ;.;

Quote:
// Guess My Number, Round 2
// A White-Hat` Production

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

using namespace std;

int main()
{
srand(time(0)); // seed the random number generator

int tries = 0, theNumber;
int theGuesser = rand() % 100 + 1; // random number between 1 and 100
cout << "\tWelcome to the Guess My Number game; White-hat` edition!\n\n";

do
{
cout << "Please enter your number, so the computer may guess it: ";
cin >> theNumber;

cout << "Test run: " << theGuesser << "\n";
++tries;

if (theGuesser != theNumber) {
theGuesser = rand() % 100 + 1;
}
} while (theGuesser == theNumber);

cout << "\nLooks like he guessed it in " << tries << " tries! Uh oes!";

return 0;
}
Any help is appreciated!

-Me

Edit: I think it might be my loop, considering switching to a single while loop versus the do. Any invoice would be helpful, as I can't test it at the moment, I have to go run some errands.

Double edit: Ugh, I just realized the problem. Will attempt to fix it asap. 8)
White-Hat` is offline   Reply With Quote
Old Sep 28th, 2005, 6:36 PM   #2
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Okay, I really have to get going, but this is the new source. Now It won't cout the guess and ends the loop. Any help on my half-assed new source would be appreciated! 8)


Quote:
// Guess My Number, Round 2
// A White-Hat` Production

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

using namespace std;

int main()
{
srand(time(0)); // seed the random number generator

int tries = 0, theNumber;
int theGuesser = rand() % 100 + 1; // random number between 1 and 100
cout << "\tWelcome to the Guess My Number game; White-hat` edition!\n\n";

cout << "Please enter your number, so the computer may guess it: ";
cin >> theNumber;
cin.ignore();

while (theGuesser == theNumber) {
cout << "Test run: " << theGuesser << "\n";
++tries;

if (theGuesser != theNumber) {
theGuesser = rand() % 100 + 1;
}
}
cout << "\nLooks like he guessed it in " << tries << " tries! Uh oes!";

return 0;
}


Ooookay, I'm sorry, just delete this. I fixed it. I'm a stubborn little boy. ._.
White-Hat` is offline   Reply With Quote
Old Sep 28th, 2005, 6:37 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
[php]#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{
srand(time(NULL));

int tries = 0,
theNumber = -1,
theGuesser = -1;

cout << "\tWelcome to the Guess My Number game; White-hat` edition!" << endl << endl;

while(theNumber < 1 || theNumber > 100)
{
cout << "Please enter your number, so the computer may guess it: ";
cin >> theNumber;
}

while(theNumber != theGuesser)
{
theGuesser = (rand() % 100) + 1;
tries++;
}

cout << "\nLooks like he guessed it in " << tries << " tries! Uh oes!" << endl << endl;

return 0;
}[/php]
__________________

tempest is offline   Reply With Quote
Old Sep 28th, 2005, 6:40 PM   #4
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Oh, I didn't see your code. I'mma toy with that soon as I get some time. I ended up with this:

Quote:
// Guess My Number, Round 2
// A White-Hat` Production

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

using namespace std;

int main()
{
srand(time(0)); // seed the random number generator

int tries = 0, theNumber;
int theGuesser = rand() % 100 + 1; // random number between 1 and 100
cout << "\tWelcome to the Guess My Number game; White-hat` edition!\n\n";

cout << "Please enter your number, so the computer may guess it: ";
cin >> theNumber;
cin.ignore();

while (theGuesser != theNumber) {
cout << "Test run: " << theGuesser << "\n";
++tries;

if (theGuesser != theNumber) {
theGuesser = rand() % 100 + 1;
}
}
cout << "\nLooks like he guessed it in " << tries << " tries! Uh oes!";

return 0;
}
White-Hat` is offline   Reply With Quote
Old Sep 29th, 2005, 12:59 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You're only real problem was this:
while (theGuesser == theNumber)
It should have been !=, as you finally realised.

Oh, and please use [code] tags, not [quote] tags next time.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC