...

View Full Version : Strange issue with a do_while loop!C++



pbracing33b
10-17-2012, 08:20 PM
I have this issue where my program runs fine except that it iterates the number like is should but immediately after that number I get this"5AFEED48" This is a guessing game where the user enters a number and the while loop keeps track of how many guesses have been attempted. So my code works fine except I don't know why I am getting this" guess #15AFEED48, guess #25AFEED48,
guess #35AFEED48, and so on! So I can see that it is keeping track but why is it giving me all the extra stuff? And what does it mean?

Thanks.

oracleguy
10-17-2012, 09:34 PM
The problem is on line 15, that semicolon shouldn't be there...

Just kidding, seriously though we need to see some code in order to answer your question.

pbracing33b
10-18-2012, 12:11 AM
The problem is on line 15, that semicolon shouldn't be there...

Just kidding, seriously though we need to see some code in order to answer your question.





#include <iostream>
#include <cstdlib> // rand and srand
#include <ctime> // For the time function

using namespace std;

int main()
{
int random;
int guess = 0;
int userInput = 0;
srand(time(NULL));
random = rand() % 100 + 1;

//Input Section
cout<<"I am thinking of a number from 1 - 100??\n";


do
{

cout<<"What is my number?";
cin>>userInput;
guess++;

if (userInput == random)
{
cout<<"You win! \n\n";
system("pause");
return 0;
}

else if (userInput < random)
{

cout<<"Guess #"<<guess<<cout<<"is too low \n\n";

}
else if (userInput > random)
{

cout<<"Guess #"<<guess<<cout<<"is too high \n\n";

}
}
while(userInput != random);

system("pause");
return 0;
}

oracleguy
10-18-2012, 07:21 PM
Oh easy. The lines where you are printing out the response you are also printing out cout which is just printing the memory address of cout.

cout<<"Guess #"<<guess<<cout<<"is too low \n\n";

Get rid of that and it will work fine.


cout<<"Guess #"<<guess<<"is too low \n\n";

pbracing33b
10-18-2012, 09:29 PM
Oh my I didn't even see that or realize that I was doing that. I have no idea why I put a cout statement there, must of had a brain fart or something, cause I never do that typically, I didn't even realize that I typed it there. I just kept looking at the logics of things instead of the syntax.

Thanks Oracle!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum