Thread: I need help with one line!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    33

    I need help with one line!

    Here is my code it executes fine it just won't redo the loop when I press "y"

    Code:
     #include <stdio.h>
    
    int main()
    {
    
    int c; /* c is get char */
    char d;   
    float t;  /* t is how far away the lighting is */
        
    
    
        printf ("Enter the amount of seconds between the flash and the sounds.\n");
        
    
    do { 
        scanf ("%d.00",&c); /* is the amount of time between the flash and the sounds */
        t= c*.2; /* multiply the time it takes for sounds to travel by the differance of the time of flash and sounds */ 
        printf ("The lighting is %f mile(s) away\n",t); /* diplay how far away the lighting is */ 
        printf ("Would you like to continue (Y/N)\n");
        scanf ("%c",&d);
        }
        while (d =='y',d=='Y');
    
        
    
    
    
    
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Use || (aka logical OR) operator, not the comma operator in the condition of the while loop.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    while (d =='y' || d =='Y');

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    it still ends the "press any key to continue line comes up then when i hit "y" it ends.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Changing the while statement fixes on problem... but you apparently have another.

    What they don't teach you in school is that scanf() leaves the <enter> key in the buffer and if the next scanf() is looking for a single character or a string, it will see the enter key and keep right on cruising... The fix is to insert a space between the opening quotation mark and the percent sign, like this...
    Code:
    scanf(" %c",&d);
    The space tells it to ignore invisible characters and take the first visible one... the one you type.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    awesome that works but it won't redo the loop with a lower case y? only when i use Y

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you change your while loop?

    You need to fix BOTH problems.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    yes this is what i have now
    Code:
    scanf(" %c",&d);
        }
        while (d =='y' || d =='Y');

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That should work...

    You can try bracketing but I wouldn't think it will matter...
    Code:
    while ((d == 'y') || (d == 'Y'));

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    no still doesn't work that is very odd

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... repost your current code.

    LOL... I just compiled it here and it does work... however since your intial prompt is before the do-while() loop it doesn't show up the second time around...

    So...
    Code:
     #include <stdio.h>
    
    int main(void)
    {
      int c;   // delay
      char d;  // go again 
      float t; // distance
      
      printf("Lightning calculator...\n\n");
    
      do 
        { 
          printf ("Seconds between flash and boom : ");
          scanf ("%d",&c); 
    
          t= c*.2;  
    
          printf ("The lighting was %.2f mile(s) away\n\n",t);
    
          printf ("Would you like to continue (Y/N) : ");
          scanf (" %c",&d);
          printf("\n\n");
        }
      while (d =='y'|| d=='Y');
    
      return 0;
    }
    Last edited by CommonTater; 10-13-2011 at 11:37 PM.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    Code:
        
    
        #include <stdio.h>
    
    int main()
    {
    
    int c; /* c is get char */
    char d;   
    float t;  /* t is how far away the lighting is */
        
    
    
        printf ("Enter the amount of seconds between the flash and the sounds.\n");
        
    
    do { 
        scanf ("%d.00",&c); /* is the amount of time between the flash and the sounds */
        t= c*.2; /* multiply the time it takes for sounds to travel by the differance of the time of flash and sounds */ 
        printf ("The lighting is %f mile(s) away\n",t); /* diplay how far away the lighting is */ 
        printf ("Would you like to continue (y/n)\n");
        scanf(" %c",&d);
        }
        while ((d == 'y') || (d == 'Y'));
    
        
    
    
    
    
    
    return 0;
    
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Scroll back to message #11 ... I was editing while you posted...

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Repost the whole thing.
    Something is different between what we think you now have and what you actually now have, otherwise it would be working. That is, assuming it is actually recompiling the thing.
    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"

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Repost the whole thing.
    Something is different between what we think you now have and what you actually now have, otherwise it would be working. That is, assuming it is actually recompiling the thing.
    Check post #12...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. Read strings line by line until Ctrl+C is pressed
    By r00t in forum C Programming
    Replies: 25
    Last Post: 11-17-2009, 04:18 AM
  5. Replies: 1
    Last Post: 05-20-2006, 11:17 PM