C loop question

Discussion in 'Programmer's Corner' started by justtrying, Oct 1, 2011.

  1. justtrying

    Thread Starter Active Member

    Mar 9, 2011
    this is a simple program that lets us practice loops. I am having a problem with loops because they are going against every core of my mathematical being... Anyway, the user can input any integer, once -99 is entered the loop is exited and the output is the greatest integer that was entered. I can see that the program would have to loop to keep asking for the input and somehow keep comparing previous input to the new one to determine the largest number. So far I have this:

    #include <stdio.h>

    int main(void)
    int i, a, largest;

    printf("please enter integer:");

    while (a!=-99){
    printf("please enter integer:");
    scanf("%d", &a);
    printf("largest integer is %d", a);

    return 0;


    with the last part being my attempt to get the largest number as the output but it does not work. It exits the loop at -99 and returns a nonsense value stored somewhere at comp. memory. I think that if function is the one two use to compare the numbers, but am not sure how to get them to store and retreive. Any advise would be appreciated.
  2. nerdegutta


    Dec 15, 2009

    Just a thought:

    Two variables. userNumber and largestNumber.

    User inputs a number. This is stored in largestNumber. Now the user inputs another number, this is stored in userNumber and compared with largestNumber. If the number is bigger, then userNumber = largestNumber. If not, then ask for next input.

    If I read your code correct. (This board has code-tags, which make source code easier to read.)

    The a variable is given values before you see what number is the biggest. You cannot compare a with a, because a will have the last number...

    At least that's what I think....
  3. stahta01


    Jun 9, 2011
    Code ( (Unknown Language)):
    2. if(a>"%d",a)
    The above line is code that I hope your compiler complains about.
    What do you think it does?

    Tim S.
  4. justtrying

    Thread Starter Active Member

    Mar 9, 2011
    in regards to the if statement, I originally thought that it would get the input stored previously and compared to the next input and the greater one to be stored. I can see that it does not make any sense. I triedediting it, but still cannot make sense of how to use IF correctly....

    Code ( (Unknown Language)):
    1. int main(void)
    2. {
    3. int user_number, largest_number;
    5. printf("please enter integer:");
    6. scanf("%d",&user_number);
    8. while (user_number!=-99, largest_number!=-99){
    9. printf("please enter integer:");
    10. scanf("%d", &largest_number);
    11. }
    12. if(user_number>largest_number)
    13. user_number=largest_number;
    14. printf("largest integer is %d", largest_number);
    16. return 0;
    18. }
  5. RockyBlackburn

    New Member

    Sep 11, 2006
    Try this simple Win32 console application.

    Code ( (Unknown Language)):
    1. ////////////////////////////////////////////////////////////////////////////////////////////////
    2. // MY CODE STARTS HERE  //////////////////////////////////////////////
    3. /////////////////////////////////////////////
    5. // Note: Console programs that are run in debug mode will exit without user input.
    6. // To avoid this, run without debugging.
    8. // Win32 Console Application.
    9. // DESCRIPTION: This program demonstrates loops. The loop will continue until “-99”
    10. // is entered. Also it will keep track of the largest integer entered and display it
    11. // at program termination.
    13. // Compiler is VS2008 C++, using an empty Win32 Console Application.
    15. #include <iostream>
    17. using std::cout;
    18. using std::cin;
    19. using std::endl;
    21. int main()
    22. {
    23.     // Declare the variables.
    24.     int iValue = 0;         // The integer entered by the user.
    25.     int iLargest = 0;       // The largest integer entered by the user.
    27.     // The loop
    28.     while (iValue != -99)
    29.     {
    30.         cout << endl << "Please enter an integer (-99 to end): ";
    31.         cin >> iValue;
    33.         if (iValue > iLargest)
    34.         {
    35.             iLargest = iValue;
    36.         }
    37.     }
    39.     // Give the user the largest value entered.
    40.     cout << endl << endl;
    41.     cout << "The largest value entered was " << iLargest << "." << endl;
    43.     return 0;
    44. }
    45. /////////////////////////////////////////////
    46. // MY CODE ENDS HERE   //////////////////////////////////////////////
    47. ////////////////////////////////////////////////////////////////////////////////////////////////
    Looks like your if statement should have been

    Code ( (Unknown Language)):
    1. if(user_number>largest_number)
    2. largest_number=user_number;
    Just a little reversal of the variables.

    Screen shot of program in action.
    Last edited: Oct 1, 2011
    justtrying likes this.
  6. kubeek

    AAC Fanatic!

    Sep 20, 2005
    it should be like this
  7. PNeil

    New Member

    Oct 27, 2010
    Loops in C/C++ are created by assigning an integer variable a value; the value is then incremented or decremented. A test is then made to the value in the form of an expression. If the expression evaluates to be false the loop breaks, otherwise it continues.

    An expression is something like this if(a<5) {continue loop} else {stop loop}

    Note: The expression can be the other way around…if true, break loop, and otherwise continue.

    When using loops like while, the increment or decrement part is already built in so all you have to worry about is the expression part. Also note that to execute several statements you must surround them with curly brace.

    You are on the right track and you did well as a beginner. You do not need to ask for input twice, you can just ask in the loop…execution will eventually get there.

    Here are the errors.

    The if line is incorrect, and is placed outside of the loop. You must make this test and assignment inside the loop. The last printf line is good thou.

    int main(void)
    Int a=0, largest=0;

    while(a != -99) {
    printf(“Enter Integer: “);
    scanf(“%d”, &a);

    // must be done here
    if(a > largest) largest=a; // Compare current user input with the one stored, if greater, overwrite/store it.

    } // end loop brace

    // print output only after loop exits
    printf(“Output = %d”);

    Return 0;