Like Tree2Likes
  • 1 Post By hk_mp5kpdw
  • 1 Post By hk_mp5kpdw

Printf issue I've never encountered before...

This is a discussion on Printf issue I've never encountered before... within the C Programming forums, part of the General Programming Boards category; So I'm adding an extra menu to my program and one of the printf statements decides to double print its ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    Printf issue I've never encountered before...

    So I'm adding an extra menu to my program and one of the printf statements decides to double print its output, something I have never seen before. I tried changing around some of the variable names, but none of that helped. If I changed it to some random output i.e. "asdghagkgklshklgh" it did not double print. Everything is in the string quotes so I'm not sure whats going on. Any help would be awesome.

    Code:
    void SetupMenu(){
    
    
    int DISTMOON, FUEL, NEWFFALL;
    char READY;
    float VELOCITY;
    
    
    printf("* * * * * * * Setup Menu  * * * * * * * * *\n\n");
    printf("Allows user to change Moon lander initial\n");
    printf("values for a whole new experience !!!\n\n");
    printf("* * * * * * * * * * * * * * * * * * * * * *\n\n");
    
    
    READY = 'N';
    
    
       while (!(READY == 'Y')){
          do {
             printf("Enter new distance to Moon:  >>");
             scanf("%d", &DISTMOON);
          } while (DISTMOON <= 0);
    
    
          do {
             printf("Enter new amount of fuel:    >>");
             scanf("%d", &FUEL);
          } while (FUEL <= 0);
    
    
          printf("Enter new velocity of ship:  >>");
          scanf("%f", &VELOCITY);
    
    
          do {
             printf("Enter new Moon gravity:      >>");
             scanf("%d", &NEWFFALL);
          } while (FFALL <= 0);
    
    
          do {
             printf("Ready (Y/N)?      >>");
             scanf("%c", &READY);
          } while ((READY != 'Y') && (READY != 'N'));
       }
    
    
    return;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > one of the printf statements decides to double print its output
    Which one?

    > I tried changing around some of the variable names, but none of that helped
    That will never fix the problem.

    Edit: where's FFALL defined? You're checking it with NEWFFALL instead, so it does this infinitely.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    So what output are you getting?

    I suggest remving the redundant return at the end of the function and making your variables non-capital.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    scanf("%c", &READY);
    That should be modified with a space in front of the % so that prior newline characters in the input buffer do not cause problems.
    shruthi likes this.
    "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

  5. #5
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    I didn't get any garbage value printed,the only problem is the scanf part.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Thanks for the advice, haven't tested any of your guys implementations yet though. The double print I was getting was on the ready printf statement.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Got it working it was definitely the space before the % as was mentioned... although I'm not sure the reason why exactly that was needed. Care to elaborate?

    Thanks. Appreciate the help.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    %c will read the next character, even if it's just a space or a newline.
    Putting a space before %c causes printf to consume all whitespace before reading the next character.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    But it wasn't in printf it was in scanf. Printf doubled its output because of something linked to scanf?

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    The scanf in the old code read the leftover newline character and took that as valid input as if you had simply pressed the enter key at that point. The program then proceeded past the scanf call and subsequent code, the printf for example, was executed. By changing the scanf call in the new code, you effectively forced the program to halt and wait for user input instead of skipping through as it did before.

    Trust me, it was an I/O issue, specifically the way the scanf was processing input and not the output by the printf function that was the issue.
    greeziakattack likes this.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. interview Q and A i encountered - BOKIBOAZ
    By bokiboaz in forum C Programming
    Replies: 2
    Last Post: 05-12-2010, 07:10 AM
  2. Program has encountered a problem and needs to close
    By TrippyJing in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2010, 12:17 PM
  3. Your program has encountered a problem and needs to close error
    By taka209 in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2008, 09:42 PM
  4. Weirdest Problem i've ever encountered!Please help me
    By chottachatri in forum C++ Programming
    Replies: 12
    Last Post: 05-25-2008, 11:14 AM
  5. printf() issue on Windows
    By Cactus_Hugger in forum C Programming
    Replies: 10
    Last Post: 09-22-2005, 09:03 PM

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