# C loop question

#### justtrying

Joined Mar 9, 2011
429
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;

scanf("%d",&a);

while (a!=-99){
scanf("%d", &a);
}
if(a>"%d",a)
a=largest;
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.

#### nerdegutta

Joined Dec 15, 2009
2,676
Hi.

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....

#### stahta01

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

Tim S.

#### justtrying

Joined Mar 9, 2011
429
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....

Rich (BB code):
int main(void)
{
int user_number, largest_number;

scanf("%d",&user_number);

while (user_number!=-99, largest_number!=-99){
scanf("%d", &largest_number);
}
if(user_number>largest_number)
user_number=largest_number;
printf("largest integer is %d", largest_number);

return 0;

}

#### RockyBlackburn

Joined Sep 11, 2006
7
Try this simple Win32 console application.

Rich (BB code):
////////////////////////////////////////////////////////////////////////////////////////////////
// MY CODE STARTS HERE  //////////////////////////////////////////////
/////////////////////////////////////////////

// Note: Console programs that are run in debug mode will exit without user input.
// To avoid this, run without debugging.

// Win32 Console Application.
// DESCRIPTION: This program demonstrates loops. The loop will continue until -99
// is entered. Also it will keep track of the largest integer entered and display it
// at program termination.

// Compiler is VS2008 C++, using an empty Win32 Console Application.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
// Declare the variables.
int iValue = 0;			// The integer entered by the user.
int iLargest = 0;		// The largest integer entered by the user.

// The loop
while (iValue != -99)
{
cout << endl << "Please enter an integer (-99 to end): ";
cin >> iValue;

if (iValue > iLargest)
{
iLargest = iValue;
}
}

// Give the user the largest value entered.
cout << endl << endl;
cout << "The largest value entered was " << iLargest << "." << endl;

return 0;
}
/////////////////////////////////////////////
// MY CODE ENDS HERE   //////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
Looks like your if statement should have been

Rich (BB code):
if(user_number>largest_number)
largest_number=user_number;
Just a little reversal of the variables.

Screen shot of program in action.

#### Attachments

• 53.5 KB Views: 57
Last edited:

#### kubeek

Joined Sep 20, 2005
5,716
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....

Rich (BB code):
int main(void)
{
int user_number, largest_number=-99;

scanf("%d",&user_number);

while (user_number!=-99){
scanf("%d", &user_number);
//} not here
if(user_number>largest_number)
user_number=largest_number;
} //but here
printf("largest integer is %d", largest_number);

return 0;

}
it should be like this

#### PNeil

Joined Oct 27, 2010
11
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;
}