5 Replies - 4443 Views - Last Post: 28 September 2010 - 07:20 PM Rate Topic: -----

#1 afmack21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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++;
    printf("Please enter a number: ");
    scanf("%d\n"), &number;
    largest = number;
    
    
    while (counter <= 9){
          counter++;
          printf("Please enter a number: ");
          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  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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/
Was This Post Helpful? 1
  • +
  • -

#3 afmack21  Icon User is offline

  • New D.I.C Head

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

Re: Find the largest number

Posted 28 September 2010 - 06:37 PM

View Postjanotte, 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++;
    printf("Please enter a number: ");
    scanf("%d\n"), &number;
    largest = number;
    
    
    while (counter <= 9){
          counter++;
          printf("Please enter a number: ");
          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;
    
}



Was This Post Helpful? 0
  • +
  • -

#4 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 afmack21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1