Thread: how do i fix it?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    36

    how do i fix it?

    hi i m new 2 the whole C++ n i think its lods of fun i'vebeen puttin a program 2gether...but 4 some reason it always seems 2 crash when askin 4 the person status( married etc..)


    the program has been written in french....but i doubt any1 is interested in wat is written with scanf

    i would like 2 thank u all in advance 4 ur time n help
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int nblivre, c;   
        float kg, cels, frnht;
        
        printf("Taper le poids en livres:\n");
        scanf("%d", &nblivre);
        
        printf("Taper la temperture en degres farenheit:\n");
        scanf("%f", &frnht);
        
        
        
        cels = ((frnht - 32) / 9) * 5;
        kg = 0.454 * nblivre;    
        
        printf("\nLe patient pese %.1f kg, a une temperature de %.1f degres", kg, cels);
         fflush(stdin);
        getchar();
    
        printf("Selectionnez votre status civile:");
         fflush(stdin);
        getchar();
    
         printf("\n\nAffichage avec switch (suivant que) :\n");
        
        
        switch (c)
        {
                        case 'c':
                             printf("celibataire.");
                             break;
                        case 'C': 
                             printf("celibataire.");
                             break;  
                        case 'a':
                             printf("un autre cas.");
                             break;
                        case 'A':
                             printf("un autre cas.");
                             break;
                        case 'm':
                             printf("marie.");
                             break;
                        case 'M':
                             printf("marie.");
                             break;
                        case 'd':
                             printf("divorce.");
                             break;
                        case 'D':   
                             printf("divorce."); 
                             break;
      
      printf("\n");
        fflush(stdin);
        getchar();
        return 0;   
    }
    }

    believe my problem is located around the switch level, n i dont know how 2 use it 2 well, that is IF i used it well....tks 4 every1;s help

    with the c>>> As far as I can tell, the variable c is never assigned a value. That may or may not be your problem.



    how do i set it up as a value 4 letters...do i use char?


    ty all for ur time n help

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't flush stdin, it's naughty see, http://faq.cprogramming.com/cgi-bin/...&id=1043284351 for an alternative

    Consider converting uppercase to lowercase in the switch, rather than repeating code.
    Code:
    #include <ctype.h>
    
    /* ... */
    
    switch(tolower(c))
    {
        case 'c':
    /* ... etc */
    'c' isn't assigned a value, call
    Code:
    c = getchar();

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    program still crashes :S dont know y...when i want 2 do this part:
    Code:
     printf("Selectionnez votre status civile:");
         fflush(stdin);
        getchar();
    
         printf("\n\nAffichage avec switch (suivant que) :\n");
        
        
        switch (c)
        {
                        case 'c':
                             printf("celibataire.");
                             break;
                        case 'C': 
                             printf("celibataire.");
                             break;  
                        case 'a':
                             printf("un autre cas.");
                             break;
                        case 'A':
                             printf("un autre cas.");
                             break;
                        case 'm':
                             printf("marie.");
                             break;
                        case 'M':
                             printf("marie.");
                             break;
                        case 'd':
                             printf("divorce.");
                             break;
                        case 'D':   
                             printf("divorce."); 
                             break;
      
      printf("\n");
        fflush(stdin);
        getchar();
        return 0;   
    }
    }
    in dos mode it crashes..:S


    wat must i use instead of flush stding?

    i m very new 2 C adn i dont know 2 much..so bare with me


    n thakes the upper n lower

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    You have defined 'c' as an integer, but you are trying to use it as a char. I doubt any of your switch statements are going to work that way. Change your definition of 'c' to char for starters. And nowhere do you assign the getchar() to c !!

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    its all good i got it workin tks alot

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > You have defined 'c' as an integer, but you are trying to use it as a char. I doubt any of your switch statements are going to work that way. Change your definition of 'c' to char for starters. And nowhere do you assign the getchar() to c !!

    getchar() returns an int... it should be an int.

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Just a little notice... there's another way of structuring your switch statement you might want to know..

    Code:
    switch (c)
    {
                        case 'c':
                        case 'C':
                             printf("celibataire.");
                             break;
                        case 'a':
                        case 'A':
                             printf("un autre cas.");
                             break;
                        case 'm':
                        case 'M':
                             printf("marie.");
                             break;
                        case 'd':
                        case 'D':
                             printf("divorce.");
                             break;
    }
    or
    Code:
    switch (c)
    {
                        case 'c':
                        case 'C':
                             printf("celibataire.");
                             break;
                        case 'm':
                        case 'M':
                             printf("marie.");
                             break;
                        case 'd':
                        case 'D':
                             printf("divorce.");
                             break;
                        default:
                            printf("un autre cas.");
                            break;
    }
    And yeah, the remarks of kcpilot about the int thing isn't right...

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    foxman ur from quebec? so u speak french, caliss finalemen qqun qui peut vraiment m'aider

    check mec g un devoir a rendre c l'autre poste est ce que je px t'envoyer exactement ce que g a faire comme ca tu px m'expliquer ce que g a faire...merci

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 03:38 AM
  3. Drag and Drop code (please help me fix)
    By John_L in forum C# Programming
    Replies: 1
    Last Post: 11-17-2007, 06:11 PM
  4. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 02:48 PM
  5. Help me fix my mess!!!!
    By Starr in forum C++ Programming
    Replies: 35
    Last Post: 02-01-2006, 03:40 PM