Thread: Novice Class Assignment Plz Help

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    India
    Posts
    35

    Novice Class Assignment Plz Help

    Code:
    main()
    {
    int a,b,c;
    char z;
    
    printf("Enter the 3 nos: \n");
    
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);   
          
          
    printf("\n");
    printf("Enter Greatest or Smallest");
    scanf("%c",&z); //Program Quits On This Line!!
          
          
          
           if(z=='g')
               {         
                        if(a>b && a>c)
                                  {
                                         printf("%d is greatest",a);
                                         
                                  }
                                  else if(b>a && b>c)
                                  {
                                  printf("%d is greatest",b);
                                  
                                  }
                                  else
                                  {
                                  printf("%d is greatest",c);
                                  }
                 }
             else if (z=='s')
                 {
                         if(a<b && a<c)
                                  {
                                         printf("%d is smallest",a);
                                         
                                  }
                                  else if(b<a && b<c)
                                  {
                                  printf("%d is smallest",b);
                                  
                                  }
                                  else
                                  {
                                  printf("%d is smallest",c);    
                                      
                                  }  
                      
                  }
              else
                  {
                  printf("Please Enter g for greatest and s for smallest!");    
                  }
                  
                  getch();  
                  
                  
    
    
    
    
    /*
    
    It compiles perfectly but quits at the mentioned point!
    I have just started c programming so the problem may look stupid to many!
    Plz bear with me!
    :biggrin:
    */
    }
    

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Someone will give a better explanation than me, but it's because that last scanf is taking the newline/return from the previous scanf as its input. If you put a getchar() before that last scanf(), it works fine.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Oh yeah, so I believe the "official" explanation is that newline character is hanging in the input buffer, so when you do the scanf("%c",&z), it grabs the newline character. Some people opt to do fflush(stdin) to fix this problem, but such a line will produce undefined behavior. fflush is only to be used on output buffers, AFAIK.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have to get used to cleaning up after function calls or just avoiding the issue altogether:

    Code:
    fgets(input, sizeof input, stdin);
    converted = sscanf(input "%d%c", &a, &ch);
    if (converted == 2 && ch == '\n') 
    {
       /* good */
    }
    else
    {
       /* bad */
    }
    An awful lot for a single integer, but then you can always put this into a function.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    India
    Posts
    35
    Thanks that did the trick,and i think i partially understood the reason for it!

    Does this mean that i have to always use getchar() before accepting a character input,,i mean it doesn't make sense to me as to why it would take newline/return as a input!

  6. #6
    Registered User
    Join Date
    Dec 2011
    Location
    India
    Posts
    35
    Quote Originally Posted by whiteflags View Post
    You have to get used to cleaning up after function calls or just avoiding the issue altogether:

    Code:
    fgets(input, sizeof input, stdin);
    converted = sscanf(input "%d%c", &a, &ch);
    if (converted == 2 && ch == '\n') 
    {
       /* good */
    }
    else
    {
       /* bad */
    }
    An awful lot for a single integer, but then you can always put this into a function.
    I am not sure i understand the code you used...i am just beginning out with c! :-)

    Anyways thanks alot guys

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Anitrex View Post
    Does this mean that i have to always use getchar() before accepting a character input
    Not necessarily, your scanf could have been written as :
    Code:
    printf("Enter Greatest or Smallest");
    scanf(" %c",&z);
    The space in front of the %c lets the scanf function know that it should discard any leading whitespace in the buffer before it starts looking for a character to extract.

    Quote Originally Posted by Anitrex View Post
    i mean it doesn't make sense to me as to why it would take newline/return as a input!
    Because %c accepts a character from input and a newline character is a valid character to get. If a newline character happens to be the first available character in the input buffer then that's what it is going to extract.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    You could just simply:
    Code:
    scanf("%s", &z);
    This will grab the newline character was well, you'll still have the character you need in z.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That's dangerous, because %s has to store a terminating zero in z, which it doesn't have room for, which is a logic error.

  10. #10
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    I don't think it's dangerous for this little project, as long as he eventually understands that scanf() is terrible to use.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well the worst that could happen is that it segfaults and the obvious logic error has to be corrected anyway. I don't think undefined behavior teaches anyone anything.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by C_Sparky View Post
    I don't think it's dangerous for this little project, as long as he eventually understands that scanf() is terrible to use.
    Maybe not... but tolerating faults in the ineterest of expediency is one pee poor habit to get into.

  13. #13
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    Both of you just relax, anyone good programmer would be able to survive this pain.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by C_Sparky View Post
    Both of you just relax, anyone good programmer would be able to survive this pain.
    Nope... a good programmer wouldn't go there in the first place.

  15. #15
    Registered User
    Join Date
    Dec 2011
    Location
    India
    Posts
    35
    Ok guys thanks a alot for the interest...I have a entire project to do in the comming week...

    The Project comprises of 50 or more individual assignments...

    I will let you guys know if i get stuck somewhere else.

    Is it okay if i continue posting in this thread or do i have to create a new thread for every problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class assignment, help!
    By TriKri in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2007, 08:17 AM
  2. Class assignment
    By tmnismo91 in forum C Programming
    Replies: 18
    Last Post: 06-19-2007, 07:00 PM
  3. Another class assignment, please help.
    By WinterInChicago in forum C++ Programming
    Replies: 16
    Last Post: 11-09-2006, 11:51 AM
  4. Novice Pointers/Class Question
    By C++Gamer in forum C++ Programming
    Replies: 8
    Last Post: 06-28-2006, 05:36 PM
  5. Replies: 10
    Last Post: 06-17-2005, 10:00 PM