Thread: question ab an infinite loop...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    question ab an infinite loop...

    how could i possibly write an infinite loop that have to be terminated when the number = 0 (or below)?... & also have the program allow multiple number entries to find the highest # out of the bunch... grrr, this prog of chap 6 exercises def sux!!!

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Code:
    while (1) {
      if (number <= 0) break;
    
    
    }
    simple.

    i don't know what you mean for the other one.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    & also have the program allow multiple number entries to find the highest # out of the bunch..
    create an array of integers and have inputs stored in the array. Write a function that steps through the integer array looking for the highest number.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273

    Talking

    if it's an infinite loop then it can't be terminated now can it!

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    this is what i have *so* far~

    /* SEC6.1 EX 1 */
    /* DATE: 07-25-02 */

    #include <stdio.h>

    int main()
    {
    float n, highest;

    printf("Enter a number: ");
    scanf("%f", &n);

    while (n > 0) {
    printf("Enter a number: ");
    scanf("%f", &n);
    }
    if ( n<= 0)
    printf("The highest number of the entries was %f", highest);

    printf("\n");
    return 0;
    }

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Code:
    printf("Enter a number: "); 
    scanf("%f", &n); 
    
    while (n > 0) { 
    printf("Enter a number: "); 
    scanf("%f", &n); 
    } 
    if ( n<= 0) 
    printf("The highest number of the entries was %f", highest);
    The last if statement is not needed.
    You're missing a step and also a do/while loop would better fit:

    highest = 0;
    do
    {
    printf("Enter a number: ");
    scanf("%f", &n);
    if(n > highest)
    highest = n;
    }while(n > 0);

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    awwww, thanx Cshot (that did the trick [do]~!!! ... now i have another Q... this program can have either integer or non-integer values... i can't use either %d or %f... what should i use?~
    thanx a bunch everyone~!!!!!!!!!!!

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Why can't you use %f? Do you mean you're supposed to check for invalid inputs also such as characters?

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    this is what the exercise in the book says:
    1. Write a program that finds the largest in a series of numbers entered by the user. The program must prompt the user to enter numbers one by one. When the user enters 0 or a negative number, the program must display the largest nonnegative number entered:

    Enter a number: 60
    Enter a number: 38.3
    Enter a number: 4.89
    Enter a number: 100.62
    Enter a number: 75.2295
    Enter a number: 0

    The largest number entered was 100.62

    ***NOTICE that the numbers aren't necessarily integers***

    here's my code so far (I need to fix the part about the entries not necessarily being integers [but not floating 5000decimal points~

    /* SEC6.1 EX 1 */
    /* DATE: 07-25-02 */

    #include <stdio.h>

    int main()
    {
    float n, highest;

    highest = 0;

    printf("Enter a number: ");
    scanf("%f", &n);

    do
    {
    printf("Enter a number: ");
    scanf("%f", &n);
    if(n > highest)
    highest = n;
    }while(n > 0);


    return 0;
    }

  10. #10
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Well, if you want to be able to enter both integers and decimals, just keep it a float.
    This is my signature. Remind me to change it.

  11. #11
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Code:
    printf("Enter a number: "); 
    scanf("%f", &n); 
    
    do 
    { 
    printf("Enter a number: "); 
    scanf("%f", &n); 
    if(n > highest) 
    highest = n; 
    }while(n > 0);
    Note that you do not need the first printf/scanf statements. The do/while loop takes care of it. Before you were using a while loop so the statements were needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  2. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  3. question about for loop
    By panfilero in forum C Programming
    Replies: 3
    Last Post: 09-27-2005, 05:59 AM
  4. Infinite loop
    By osal in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-08-2004, 04:18 PM
  5. Newbie Loop Question.
    By SlyMaelstrom in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2004, 03:47 PM