# Find the largest number

Page 1 of 1

## 5 Replies - 6000 Views - Last Post: 28 September 2010 - 07:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192526&amp;s=89977e8151399322293f169cfc7bb3aa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 afmack21

Reputation: 0
• Posts: 5
• Joined: 28-September 10

# Find the largest number

Posted 28 September 2010 - 06:12 PM

Hello, I am trying to solve a problem from C How To Program 5th ed. and I'm currently stumbling around the answer. The problem is:

Write a program that inputs a series of 10 numbers and determines and prints the largest of the numbers. [Hint: Your program should use three variables as follows]:

counter: A counter to 10
number: The current number input to the program
largest: The largest number found so far

When I run my code it runs but after it runs the code then shuts down so I don't know if it did what it was supposed to or not. Here's the code I'm currently running. Thanks everyone!
```
#include<stdio.h>

int main ( void )
{
int counter = 0;
int number;
int largest;
int theend;

counter++;
scanf("%d\n"), &number;
largest = number;

while (counter <= 9){
counter++;
scanf("%d\n", &number);

if ( number > largest ){
largest = number;
}
}

printf("The largest number is %d!", largest)
scanf("%d", &theend);

return 0;

}

```

Is This A Good Question/Topic? 0

## Replies To: Find the largest number

### #2 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Find the largest number

Posted 28 September 2010 - 06:17 PM

Try this:
Holding the execution window open
http://www.dreaminco...on-window-open/

### #3 afmack21

Reputation: 0
• Posts: 5
• Joined: 28-September 10

## Re: Find the largest number

Posted 28 September 2010 - 06:37 PM

janotte, on 28 September 2010 - 05:17 PM, said:

Try this:
Holding the execution window open
http://www.dreaminco...on-window-open/

Please excuse my ignorance but can you mix c and c++ code. I believe I am already do essentially the same thing by having scanf("%d\n"), &theend; in the code above by making the program wait for a numeric input to end.

I went ahead and tried it using the headers and c++ syntax but I would keep getting a compiler error saying that cin.get() was was an undeclared variable. Here is the codefrom that:

```
#include<stdio.h>
#include<iostream>
#include <limits>

int main ( void )
{
int counter = 0;
int number;
int largest;
int theend;

counter++;
scanf("%d\n"), &number;
largest = number;

while (counter <= 9){
counter++;
scanf("%d\n", &number);

if ( number > largest ){
largest = number;
}
}

printf("The largest number is %d!", largest);
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
cin.get();

return 0;

}

```

### #4 ericr2427

• D.I.C Regular

Reputation: 39
• Posts: 378
• Joined: 01-December 08

## Re: Find the largest number

Posted 28 September 2010 - 06:57 PM

You need to use std::cin.get();. If you don't type using namespace std; at the beginning of your program, you have to show that the methods are from the std namespace by preceding it with std::, as you have done in other places in your program.

### #5 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Find the largest number

Posted 28 September 2010 - 07:02 PM

Fair enough.

Try this
make theend a C-string.
```char* theend;
```

then at the end
```scanf("%s", &theend);
scanf("%s", &theend);
```

The double scanf() will ensure any junk, if there is any, is consumed by the first instance.

### #6 afmack21

Reputation: 0
• Posts: 5
• Joined: 28-September 10

## Re: Find the largest number

Posted 28 September 2010 - 07:20 PM

Alright, thanks janotte and eric. I went ahead and used Eric's fix and that fixed the disappearing screen. Now that I can see my output again I have another issue. When it gets to printing the largest number at the end, it gives me a random number 2293576. I believe that this is the memory location. I've had this problem before but the answer was quite simply that I was telling it to print &variable instead of just variable. I don't know why it's doing it this time.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }