Thread: while loop

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    54

    while loop

    Hi, I am trying to do a while loop that terminates the program when 99999 is entered but otherwise allows infinite integers to be entered. My problem is that when a negative number is entered it crashes and also when I enter a number it displays it twice. Any help would be much appreciated.
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main()
    {
    int number;
    
    
       printf("Input an integer.\n");
    
    
       do {
          scanf("%d",&number);   /* read a single integer value in */
          printf("%d\n",number);
       } while (number != 99999);
    
    
    
    
    }

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by andyouf View Post
    Hi, I am trying to do a while loop that terminates the program when 99999 is entered but otherwise allows infinite integers to be entered. My problem is that when a negative number is entered it crashes and also when I enter a number it displays it twice. Any help would be much appreciated.
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main()
    {
    int number;
    
    
       printf("Input an integer.\n");
    
    
       do {
          scanf("%d",&number);   /* read a single integer value in */
          printf("%d\n",number);
       } while (number != 99999);
    
    
    
    
    }
    It does not display a number twice what you see is an echo of what you entered and a display from the program making it seem like its displaying the number twice.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Aslaville is right, one is the echo from the terminal, the other is the print statement. As for crashing on negative numbers, I can't replicate your problem. Perhaps if you posted a sample run showing the input that causes the crash, we could help.

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    I see, thank you. Any idea on how to get it to display "Enter next integer" repeatedly in the output? Right now it just says that first line then only numbers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
    int number;
    
    
    
    printf("Enter next integer:\n");
    
    do {
    scanf("%d",&number); /* read a single integer value in */
    
    } while (number != 99999);
    
    
    }
    

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    As Aslaville said, the first value printed is the input being echoed to the terminal (or console window).
    Normally, for short prompt string lengths and short input values, you would leave the cursor on the same line as the input prompt, by not adding a newline character. I use a colon plus a couple spaces:

    Code:
    printf("Input an integer:  ");
    You may also want to print some message along with the output value:
    Code:
    printf("You entered:  %d\n",number);
    Also, an int may be only 16 bits on some systems, with a maximum value of 32767.
    99999 would be too large a value in that case.

    -
    Last edited by megafiddle; 10-21-2014 at 10:32 PM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by andyouf View Post
    I see, thank you. Any idea on how to get it to display "Enter next integer" repeatedly in the output? Right now it just says that first line then only numbers.
    Place your input prompt, "Enter next integer", inside the while loop.

    -

  7. #7
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Quote Originally Posted by anduril462 View Post
    Aslaville is right, one is the echo from the terminal, the other is the print statement. As for crashing on negative numbers, I can't replicate your problem. Perhaps if you posted a sample run showing the input that causes the crash, we could help.
    i see the difference now, thank you. Yes it was not crashig on negatives i thing i may have put in a really long one need to change it to long long int

  8. #8
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Quote Originally Posted by megafiddle View Post
    Place your input prompt, "Enter next integer", inside the while loop.

    -
    Freaking brilliant! Thank you. I love when this stuff works, and as you can see I am very beginner. Onto getting a sum calc into that loop, which is beginning to get maddening. Any hints...I wouldn't mind. I get putting a sum calc in but putting one in that will accept any random set of numbers...

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    The normal approach to that is to declare a variable "sum" and initialize it to zero.
    Then each time a new value is entered, add the new value to "sum", like this:

    sum = sum + value;

    This accumulates the values and keeps a running total of the values entered.

    -

  10. #10
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    What I'm asking is is there a way to sum a random set of numbers where we do not know how many #'s will be input until the program is terminated with 99999 and then reads an output message on sum.

  11. #11
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Quote Originally Posted by megafiddle View Post
    The normal approach to that is to declare a variable "sum" and initialize it to zero.
    Then each time a new value is entered, add the new value to "sum", like this:

    sum = sum + value;

    This accumulates the values and keeps a running total of the values entered.

    -
    Even when I just put "sum=0" under int main it crashes the prog...where do i declare sum?

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Yes, just set up a variable "sum" and initialize it to zero.

    Then each time a value is input, add that value to "sum":

    Code:
    scanf("%d",&number);
    sum = sum + number;
    This occors inside the loop, each time a new value is read in.

    -

  13. #13
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    ugh....wont compile
    Code:
    /* Prompts user and gets integer numbers from keyboard, one number at a time. Ends program when 99999 entered. and displays various results.
            Written by: 
            Date:       10/20/14
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    int number;
    sum=0;
    
    
        do {
          printf("Enter next integer:");
          scanf("%d", sum + &number);   /* read a single integer value input */
    
    
        } while (number != 99999);
    
    
    }

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Sorry, we are cross posting -

    Declareing "sum" at the beginning of main should be fine.

    What is your code now?

    -

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    sum has no type:

    sum = 0;

    you need:

    int sum = 0;

    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM