Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    70
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Strange issue with a do_while loop!C++

    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.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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.
    OracleGuy

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    70
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oracleguy View Post
    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.
    Code:
    
    #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;
    }

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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.
    Code:
    cout<<"Guess #"<<guess<<cout<<"is too low \n\n";
    Get rid of that and it will work fine.

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

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    70
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •