Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Trouble with loops

Trouble with loops

Scheduled Pinned Locked Moved C / C++ / MFC
questiongame-devhelplounge
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StateofMind09
    wrote on last edited by
    #1

    I've got an assignment that I've been struggling with all day, and after re-doing the program several times I believe I've narrowed down my problem to this:

    #include #include int random;
    int guess;
    char play = 'y';
    int game = 1;

    int main()
    {
    while(game == 1)
    {
    if(play == 'y')
    {
    random = 1 + rand() % 1000;

    	printf("\\nI have a number between 1 and 1000.\\nCan you guess my number?\\nType your first guess:");
    
    	play = 'v';
    
    }
    	scanf("%i",&guess);
    
    	if(guess == random)
    	{
    		printf("\\nExcellent! You guessed the number!\\nWould you like to play again?(y/n)");
    		scanf("%c",&play);
    		if(play == 'n')
    		{
    			 game = 0;
    		}
    	}
    
    	else
    	{
    		if(guess < random && guess != random)
    		{
    			printf("\\nToo low, try again!");
    		}
    
    		if(guess > random && guess != random)
    		{
    			printf("\\nToo high, try again!");
    		}
    	}
    }
    
    	return 0;
    

    }

    When I enter the if(guess == random){} block and answer the (y/n) question, the program falls apart. No matter what I seem to do, it'll shut down, stop functioning, or repeat that block once more before it moves on to what it's supposed to do. I don't understand what I'm doing wrong.

    S CPalliniC S 3 Replies Last reply
    0
    • S StateofMind09

      I've got an assignment that I've been struggling with all day, and after re-doing the program several times I believe I've narrowed down my problem to this:

      #include #include int random;
      int guess;
      char play = 'y';
      int game = 1;

      int main()
      {
      while(game == 1)
      {
      if(play == 'y')
      {
      random = 1 + rand() % 1000;

      	printf("\\nI have a number between 1 and 1000.\\nCan you guess my number?\\nType your first guess:");
      
      	play = 'v';
      
      }
      	scanf("%i",&guess);
      
      	if(guess == random)
      	{
      		printf("\\nExcellent! You guessed the number!\\nWould you like to play again?(y/n)");
      		scanf("%c",&play);
      		if(play == 'n')
      		{
      			 game = 0;
      		}
      	}
      
      	else
      	{
      		if(guess < random && guess != random)
      		{
      			printf("\\nToo low, try again!");
      		}
      
      		if(guess > random && guess != random)
      		{
      			printf("\\nToo high, try again!");
      		}
      	}
      }
      
      	return 0;
      

      }

      When I enter the if(guess == random){} block and answer the (y/n) question, the program falls apart. No matter what I seem to do, it'll shut down, stop functioning, or repeat that block once more before it moves on to what it's supposed to do. I don't understand what I'm doing wrong.

      S Offline
      S Offline
      ShiXiangYang
      wrote on last edited by
      #2

      your program is too difficult to get the right number. because every time after one loop your random have changed, who can guess a unmber just equal one random number form 1 to 1000.

      1 Reply Last reply
      0
      • S StateofMind09

        I've got an assignment that I've been struggling with all day, and after re-doing the program several times I believe I've narrowed down my problem to this:

        #include #include int random;
        int guess;
        char play = 'y';
        int game = 1;

        int main()
        {
        while(game == 1)
        {
        if(play == 'y')
        {
        random = 1 + rand() % 1000;

        	printf("\\nI have a number between 1 and 1000.\\nCan you guess my number?\\nType your first guess:");
        
        	play = 'v';
        
        }
        	scanf("%i",&guess);
        
        	if(guess == random)
        	{
        		printf("\\nExcellent! You guessed the number!\\nWould you like to play again?(y/n)");
        		scanf("%c",&play);
        		if(play == 'n')
        		{
        			 game = 0;
        		}
        	}
        
        	else
        	{
        		if(guess < random && guess != random)
        		{
        			printf("\\nToo low, try again!");
        		}
        
        		if(guess > random && guess != random)
        		{
        			printf("\\nToo high, try again!");
        		}
        	}
        }
        
        	return 0;
        

        }

        When I enter the if(guess == random){} block and answer the (y/n) question, the program falls apart. No matter what I seem to do, it'll shut down, stop functioning, or repeat that block once more before it moves on to what it's supposed to do. I don't understand what I'm doing wrong.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        StateofMind09 wrote:

        if(guess < random && guess != random) { printf("\nToo low, try again!"); } if(guess > random && guess != random) { printf("\nToo high, try again!"); }

        Funny. :-D Do you really think

        if ( guess < random )
        {
        //...
        }
        else
        {
        //..
        }

        is not enough? [added] BTW: It shouldn't 'fall apart'. Why don't you use the debugger to see what actually happens? [/added] :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        modified on Thursday, April 23, 2009 4:11 AM

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • S StateofMind09

          I've got an assignment that I've been struggling with all day, and after re-doing the program several times I believe I've narrowed down my problem to this:

          #include #include int random;
          int guess;
          char play = 'y';
          int game = 1;

          int main()
          {
          while(game == 1)
          {
          if(play == 'y')
          {
          random = 1 + rand() % 1000;

          	printf("\\nI have a number between 1 and 1000.\\nCan you guess my number?\\nType your first guess:");
          
          	play = 'v';
          
          }
          	scanf("%i",&guess);
          
          	if(guess == random)
          	{
          		printf("\\nExcellent! You guessed the number!\\nWould you like to play again?(y/n)");
          		scanf("%c",&play);
          		if(play == 'n')
          		{
          			 game = 0;
          		}
          	}
          
          	else
          	{
          		if(guess < random && guess != random)
          		{
          			printf("\\nToo low, try again!");
          		}
          
          		if(guess > random && guess != random)
          		{
          			printf("\\nToo high, try again!");
          		}
          	}
          }
          
          	return 0;
          

          }

          When I enter the if(guess == random){} block and answer the (y/n) question, the program falls apart. No matter what I seem to do, it'll shut down, stop functioning, or repeat that block once more before it moves on to what it's supposed to do. I don't understand what I'm doing wrong.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Your problem is that when you enter the correct guess, you type a return character after the number to terminate the line. The scanf("%c",&play); line will then read that character as your (first) response, rather than the 'y' or 'n'. I would recommend altering that bit of the program into a loop, so it validates your input, so instead of this:

          		scanf("%c",&play);
          		if(play == 'n')
          		{
          			 game = 0;
          		}
          

          have this:

               do
               {
                  scanf("%c", &play);
               } while (play != 'y' && play != 'n');
               if(play == 'n')
               {
                  game = 0;
               }
          

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups