5 Replies - 190 Views - Last Post: 07 February 2012 - 06:00 PM Rate Topic: -----

Topic Sponsor:

#1 seechless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-October 09

C programming loops?

Posted 07 February 2012 - 04:18 PM

Can't figure out why its only returning guesses 1,3,and sometimes 5. How can I write this to show all 5 guesses and output with if it is too high or low the guess.









 


#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main(void)
{
    int answer=0, guess1=0, guess2=0, guess3=0, guess4=0, guess5=0;
    srand(time(NULL));
    
    do{
        answer=rand()%50+1;             
                      
       printf("I am thinking of a number between 1 and 50.  Try to guess it!");
       
       
       
       
       
      
       
       printf("\n\n Guess #1 :");
       scanf("%d",&guess1);
    
    if(guess1==answer) {
            printf("Congratulations %d is the correct answer",guess1);
            break;
            }
            
    if(guess1>answer){
                     printf("Sorry %d is too high\n\n",guess1);  
                       }
      if(guess1<answer){
                     printf("Sorry %d is too low\n\n",guess1);  
                       }
      
     
     
     
    
                              
      if (guess1==!answer) {
             printf("\n\n Guess #2 :");
              scanf("%d",&guess2);
    
    if(guess2==answer) {
            printf("Congratulations %d is the correct answer",guess2);
            }
    if(guess2>answer){
                     printf("Sorry %d is too high\n\n",guess2);  
                       }
      if (guess2<answer){
                     printf("Sorry %d is too low\n\n",guess2);  
           }
           }  
          
          
          
          
           
           
          else if(guess2==!answer) {
             printf("\n\n Guess #3 :");
              scanf("%d",&guess3);
    
    if(guess3==answer) {
            printf("Congratulations %d is the correct answer",guess3);
            }
    if(guess3>answer){
                     printf("Sorry %d is too high\n\n",guess3);  
                       }
      if(guess3<answer){
                     printf("Sorry %d is too low\n\n",guess3);  
           }
           }  
           
           
           
           
           
           
          else if(guess3==!answer) {
             printf("\n\n Guess #4 :");
              scanf("%d",&guess4);
    
    if(guess4==answer) {
            printf("Congratulations %d is the correct answer",guess4);
            }
    if(guess4>answer){
                     printf("Sorry %d is too high\n\n",guess4);  
                       }
      if(guess4<answer){
                     printf("Sorry %d is too low\n\n",guess4);  
           }
           }  
           
           
           
           
           
           
           
           
        else if(guess4==!answer) {
             printf("\n\n Guess #5 :");
              scanf("%d",&guess5);
    
    if(guess5=answer) {
            printf("Congratulations %d is the correct answer",guess5);
            }
    if(guess5>answer){
                     printf("Sorry %d is too high\n\n",guess5); 
                       }
      if(guess5<answer){
                     printf("Sorry %d is too low\n\n",guess5 );
           }
           }  
           
           
           
           
           else{
                printf("Sorry the correct answer was %d",answer);
                }
           
           
           
           
           
           
           
                    
    
}while(answer==!guess1||answer==!guess2||answer==!guess3||answer==!guess4||answer==!guess5);
  system("PAUSE");	
  return 0;
}







This post has been edited by seechless: 07 February 2012 - 04:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C programming loops?

#2 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 122
  • Joined: 24-January 12

Re: C programming loops?

Posted 07 February 2012 - 04:51 PM

look at
if(guess5=answer) 


to start with, and I would recommend cleaning the code up with indents that are easier to follow and clear up some of the whitespace.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: C programming loops?

Posted 07 February 2012 - 05:00 PM

You obviously know how to use loops, but did you know that you can embed loops inside other loops? Why not combine all the guesses into a loop instead of writing essentially the same code 5 times?
Was This Post Helpful? 0
  • +
  • -

#4 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 123
  • Joined: 08-July 08

Re: C programming loops?

Posted 07 February 2012 - 05:30 PM

also I thought NOT was !=. I wasn't aware you could use ==!
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian
  • member icon

Reputation: 469
  • View blog
  • Posts: 1,589
  • Joined: 02-August 09

Re: C programming loops?

Posted 07 February 2012 - 05:45 PM

View Postbuffalobill, on 07 February 2012 - 05:30 PM, said:

also I thought NOT was !=. I wasn't aware you could use ==!


Just to be clear, "==!" is two operators, the (comparison) equality operator (==), and the logical negation operator (!).
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 1893
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: C programming loops?

Posted 07 February 2012 - 06:00 PM

The not equal operator is != the ==! will compile but I doubt that it will produce what you expect.


The following line: if (guess1==!answer) actually evaluates to if(guess == (!answer)). The !answer will either be true or false (1 or 0). So you will be comparing guess1 to a 0 or 1. Probably not what you want.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1