Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,573 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,910 people online right now. Registration is fast and FREE... Join Now!




Finding a program for the largest number out of 15 numbers enters one

 
Reply to this topicStart new topic

Finding a program for the largest number out of 15 numbers enters one

graves2008
11 Apr, 2008 - 09:21 AM
Post #1

New D.I.C Head
*

Joined: 10 Apr, 2008
Posts: 4

Every time I run the program, it will give me the last number entered instead of the MAX I want it to give me, I dont know what I did wrong...



CODE

#include <iostream>

int main()
{
  int number;
  int max;
  int counter = 1;

  std::cout << "Enter 15 numbers:\n ";
  
  while (counter <= 15)

{
  
  std::cout << "Enter number: ";
  std::cin >> number ;
  max = number;
  counter = counter + 1;        
  
      if (number >= max)
          number = max;
      else
         max = max;      
    }
  
  std::cout << "The largest number is : " << max << std::endl;
  
return 0;

}

User is offlineProfile CardPM
+Quote Post

skaoth
RE: Finding A Program For The Largest Number Out Of 15 Numbers Enters One
11 Apr, 2008 - 09:24 AM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 343



Thanked: 10 times
Dream Kudos: 100
My Contributions
It is because of this
QUOTE

std::cin >> number ;
max = number;


you assign to the max variable right after you enter the number in.

User is offlineProfile CardPM
+Quote Post

graves2008
RE: Finding A Program For The Largest Number Out Of 15 Numbers Enters One
12 Apr, 2008 - 01:32 AM
Post #3

New D.I.C Head
*

Joined: 10 Apr, 2008
Posts: 4

Yes, my teacher told me to do that with the max. I believe he also told us to use a double larger function, however I didn't use it because I dont know how!
User is offlineProfile CardPM
+Quote Post

KYA
RE: Finding A Program For The Largest Number Out Of 15 Numbers Enters One
12 Apr, 2008 - 01:35 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
Try this:

cpp


//your code, blah blah etc...
//define a variable that keeps count of the number entered
if (numEntered == 1)
max = number;

//rest of yuor code


That way the first number will always be assigned to max and subsequent ones will be put to the number > max text


edit: I would use your already defined counter variable for this test

This post has been edited by KYA: 12 Apr, 2008 - 01:38 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Finding A Program For The Largest Number Out Of 15 Numbers Enters One
12 Apr, 2008 - 01:40 AM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
sorry for the double post here is a possible solution to your problem smile.gif :

cpp

#include <iostream>

int main()
{
int number;
int max;
int counter = 1;

std::cout << "Enter 15 numbers:\n ";

while (counter <= 15)

{

std::cout << "Enter number: ";
std::cin >> number ;
if (counter == 1) {
max = number;
}//end first # check
else if (number >= max){
max = number;
}//end if
counter++;
}//end while

std::cout << "The largest number is : " << max << std::endl;
int response;
std::cin >> response;
return 0;

}


Feel free to figure out how you want it to end, etc...

This post has been edited by KYA: 12 Apr, 2008 - 01:40 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:02AM

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