Like Tree1Likes
  • 1 Post By anduril462

c problem

This is a discussion on c problem within the C Programming forums, part of the General Programming Boards category; Hi, i have a program that asks the user for a number and i want it to give an error ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Cool c problem

    Hi,
    i have a program that asks the user for a number and i want it to give an error message when the user enters a letter. Until now it freezes when that happens. I know that's a noob question but can anybody help me please?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Show us the code you're using (don't forget to use code tags) so that we can help you out.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    
    int main(void)
    {
        int horasdepijama; 
        
        printf("Quantas horas por dia, em media, passa o mora de pijama?\n");
        scanf("%d", &horasdepijama);       
       
        while (horasdepijama !=22){
              
              if (horasdepijama < 22){
                                system("cls");
                                printf("Errado\n");
                                printf("Lembra-te, e o mora. Tenta um valor superior\n");
                                printf("\n");
                                }
                                
              else            
              if (horasdepijama == 23){
                                system("cls");
                                printf("Errado\n");
                                printf("Ok e o mora, mas ainda nao e tanto. Tenta um valor inferior\n");
                                printf("\n");
                                }
               
              else 
              if (horasdepijama == 24){
                                system("cls");
                                printf("Errado\n");
                                printf("Ok e o mora, mas ainda nao e tanto. Tenta um valor inferior\n");
                                printf("\n");
                                }
                   
              else       
              if (horasdepijama > 24 && horasdepijama != 'a'){
                                system("cls");
                                printf("Burro!\n");
                                printf("Um dia so tem 24horas!\n");
                                printf("\n");
                                }
                                                                  
              printf("Quantas horas por dia, em media, passa o mora de pijama?\n");
              scanf("%d", &horasdepijama);
              }
              
            
              if (horasdepijama == 22){
                                system("cls");
                                printf("CERTO! 22horas de pijama! Que bela criatura!\n");
                                printf("\n");
                                printf("As restantes sao 1 para ir ao cafe e outra para tomar banho\n");
                                printf("\n");
                                printf("By: JOAO GASPAR\n");
                                }
              
              
        
                           
      system("PAUSE");	
      return 0;
    }
    Thanks for your attention .

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Try checking the return value of scanf.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    26
    Try doing this at the end of your loop.

    Code:
     
              printf("Quantas horas por dia, em media, passa o mora de pijama?\n");
              fflush(stdin);
              scanf("%d", &horasdepijama);

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Quote Originally Posted by Pztar View Post
    Try doing this at the end of your loop.

    Code:
     
              printf("Quantas horas por dia, em media, passa o mora de pijama?\n");
              fflush(stdin);
              scanf("%d", &horasdepijama);
    No! Do NOT do that. It's undefined behavior, and likely wont work at all, unless you're still using that 20 year old piece of crap Borland Turbo C compiler (which you should stop doing immediately and upgrade to something decent). Read the FAQ on why it's wrong and how to fix it:
    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    Cprogramming.com FAQ > Flush the input buffer
    CommonTater likes this.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Try checking the return value of scanf.
    By way of expanding on the above... scanf() does pattern matching on the keyboard buffer... the return value is the number of conversions it completed... so if you're asking for numbers ( %d ) and it gets letters, the return value will be 0.

    (Added because the documentation isn't always clear about this)

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    3
    The example given is with the var type 'char' and my code is with the 'int' type. I'm trying adjust it but i'm not doing it right. Can somebody get me an example with a code more like my own?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Check out int isalpha() or int isdigit() of the ctype header.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  3. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21