Thread: help plz

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are presumably running this from inside an IDE, and since it's a command line applicaiton, it will end whenever it finishes, and although it doesn't crash, you don't have enough time to see the last thing it does, because the window disappears.

    There are two solutions:
    1. use getchar() to hold up the program. In your previous attempt, it didn't work because there is still a newline in the input buffer from you hitting enter after "m". So you need to clear the input buffer with something like
    Code:
    while(getchar() != '\n') ;
    getchar();
    That way, you have to hit enter before the program exits, and any previous enter is soaked up by the while-loop.

    2. Use a command window to run your application - use Start Menu->run->"cmd" to run a command prompt window. If you go to the directory where your application .exe lives, then you can run the application by typing it's name in and hitting enter.

    Since it's now running in a pre-existing window, the last thing the application does will still be displayed after it exits.

    I prefer #2 myself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    perfect it actually does stay now...thanks alot
    1 more question tho,

    how do i set up a printf for after i select one of the possibilities?
    For example if i press M and then i hit enter the window stays, but then i hit enter again, then the window closes, therefor i must have a problem with showing the switch am i mistaken?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, I'm not sure what you mean in your last question.

    So, you enter some numbers for your weights and temperature, then get the question of "civil status" which should be answered wtih C, M, D or A. You hit M then enter, and the applicaiton prints the relevant message [which by the way, you should use the simplified form that was suggested before, and you have posted a version of earlier - duplicating things is just making it messy].

    You should then get to press enter again before the application finishes.

    If this is not the case, then please post the current version of the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    ok wat i m tryin 2 say is

    when i hit M, i want it to show on dos ( by using scanf i believe) taht you are married and if you hit D instead that means you are divorced.
    Here is the cde with the required changes :
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int nblivre, c;   
        float kg, cels, frnht;
        
        printf("Taper le poids en livres:\n");
        scanf("&#37;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("Rentrer le statut civil:\n");
       c = getchar();
       
    while(getchar() != '\n') ;
    getchar();
        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;
      
      printf("\n");
     
        return 0;   
    }
    }

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Which I'm pretty sure your applicaiton does, but you probably don't see it before the window closes, as your "wait for user to press enter" is BEFORE the printf, and then the applicaiton just finishes immediately.
    move this the code to the end of the function (just before "return 0") and it will be much more what you want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    which code do i move all the way down?
    Code:
    while(getchar() != '\n') ;
    getchar();
    that one? i did, and if i do keep it like that i have the same problem as before,it closes as soon as i put in one of the four letters

  7. #22
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    actually i got it workin it was the }

    there shouldnt be the two close 2gethere it was suppose 2 be like this:
    Code:
                         break;
      
      printf("\n");
    }    
    while(getchar() != '\n') ;
    getchar();
        return 0;   
    
    }

    and not
    Code:
                         break;
      
      printf("\n");
        
    while(getchar() != '\n') ;
    getchar();
        return 0;   
    }
    }
    


    thanks every1 for ur help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  3. plz help me with simple string comparison.
    By MegaManZZ in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2008, 01:11 PM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM