Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,399 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,107 people online right now. Registration is fast and FREE... Join Now!




While Statement to Determine Largest Integer

 
Reply to this topicStart new topic

While Statement to Determine Largest Integer, Using C++ While Statement to find Largest Int Entered by User

UncleScruffy
28 Sep, 2006 - 07:59 PM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 7


My Contributions
Hello, everyone.

I am having a little trouble coding while statements to get results from the rest of the code. At present I am attempting to determine the largest of 10 integers entered by a user from the keyboard.

I have constructed the While Statement, using an if statement in the body of the while, to determine the largest integer. However, I get numerous errors.

The code I have thus far constructed is reproduced below.

CODE

// Program 4.17_b.cpp: This program uses a while statement to determine and print the largest of 10 numbers entered by a user from the keyboard.

#include <iostream>
// Declare using declarations
using std::cout;
using std::cin;
using std::endl;

int main() // Begin program execution
{
// Initialize variables

int counter = 1;
int num;
int largest = 0;

while (counter <= 10) // Begin while loop

{
     cout << "Please enter a number from the keyboard.\n" << endl; // Prompt user to enter a number from the keyboard
     cin >> num; // Read in number entered by user
     int largest = num;
     if (int num > largest) // Begin loop to determine the largest of 10 numbers entered by user from the keyboard
          cout << "\n" << largest << "\n" << endl; // Determine if first number entered by user from keyboard is largest, then output that number

counter++; // Advance counter by 1 for each loop

} // End while loop
     cout << "\nThe largest of the numbers entered is " << num << "." << endl;

return 0; // Terminate program successfully

} // End main and program execution


With the code constructed like the above, I get a syntax error #2059 '>' for the if statement.

I have rearranged the code several different ways, and each time I get an error, like I need a semi-colon or a comma before the if statement. Therefore, I must be constructing the if statement wrong. I also tried an if/else statement, but I get the error next to the else that it is illegal and missing an if.

I understand what I need to do in this problem, but my mind is not picking up on how to construct the code to find the largest number.

A little help from anyone would be greatly appreciated.

Thanks.

Uncle Scruffy sleepy.gif
User is offlineProfile CardPM
+Quote Post

Vextor
RE: While Statement To Determine Largest Integer
28 Sep, 2006 - 08:53 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 May, 2002
Posts: 288



Thanked: 1 times
Dream Kudos: 25
My Contributions
A couple of things.

First you have num declared twice within the same scope. Basically this means when your program runs it's going from the top of main to the bottom where each of the curly braces are. With in that block you have num declared twice. First you declare it at the top, then you declare it again WITHIN the if statement. This is likely your syntax error. To correct this you can simply take the 'int' out of the if statement in front of num. The same will go for 'largest'.


Just remeber this for now. When you've already declared a variable with a keyword (int, char, etc...) you don't have to use that keyword to assign values to that variable. Look below...
CODE

#include <iostream>

int main()
{
   int x, y;    // variable declartions

    y = 5; // this assigns y the value of 5 notice you don't need the 'int' in front
    cout<<"Enter a number\n";
    cin>> x;  // same thing here, x is assigned a number from the user but no 'int' is needed.

return 0;
}

User is offlineProfile CardPM
+Quote Post

browngod2002
RE: While Statement To Determine Largest Integer
29 Sep, 2006 - 07:27 PM
Post #3

New D.I.C Head
Group Icon

Joined: 16 Sep, 2006
Posts: 27



Thanked: 1 times
Dream Kudos: 275
My Contributions
I found a few things wrong with your program.
One is just as Vextor said about your declaring num twice
Second you need to move your statement
CODE
int largest = num;
inside the if statement without the int.
Third you need to look at what you are returning. the way it is now all you are returning is the last number entered
Change these things and your code will work.

This post has been edited by Dark_Nexus: 1 Oct, 2006 - 02:32 PM
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: While Statement To Determine Largest Integer
29 Sep, 2006 - 07:39 PM
Post #4

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,974



Thanked: 48 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Thanks Vextor and browngod2002 for helping out Uncle Scruffy!
User is offlineProfile CardPM
+Quote Post

UncleScruffy
RE: While Statement To Determine Largest Integer
1 Oct, 2006 - 01:08 PM
Post #5

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 7


My Contributions
Indeed, I echo shyhawk133. Thanks, Vextor and browngod2002. I do apprrciate your help
and will include your suggestiong in my code. tongue.gif

Uncle Scruffy
User is offlineProfile CardPM
+Quote Post

UncleScruffy
RE: While Statement To Determine Largest Integer
1 Oct, 2006 - 04:42 PM
Post #6

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 7


My Contributions
biggrin.gif Thanks to Vextor and browngod2002, again, I finally got the above code to work
after several more blunders and trials.

The original and the way it finally worked are reproduced below.
original:
[quote]
CODE

while (counter <= 10) // Begin while loop

{
     cout << "Please enter a number from the keyboard.\n" << endl; // Prompt user to enter
                   a number from the keyboard
     cin >> num; // Read in number entered by user
     int largest = num;
     if (int num > largest) // Begin loop to determine the largest of 10 numbers entered by user from the keyboard
          cout << "\n" << largest << "\n" << endl; // Determine if first number entered by user from keyboard is largest, then output that number

counter++; // Advance counter by 1 for each loop

} // End while loop
     cout << "\nThe largest of the numbers entered is " << num << "." << endl;[/quote]


Final version:
[quote]
CODE

while (counter <= 10) // Begin while loop
{
       cout << "Please enter a number from the keyboard.\n" << endl; // Prompt user to enter a number from the keyboard
       cin >> num; // Read in number entered by user
       if (num > largest) // Begin loop to determine the largest of 10 numbers entered by user from the keyboard
      {
             largest = num;
      } End if
      counter++; // Advance counter by 1 for each loop
      cout << "\nThe largest of the numbers entered is " << largest << "." << endl;
} // End while
[/quote

I appreciate everyone's interest in my problem, including you, skyhawk133.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:06AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month