944,156 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2174
  • C++ RSS
Sep 28th, 2004
1

a little help

Expand Post »
I've written the program out but I'm not sure on how I can get the largest and smallest # to display.. can anyone help? thanks.

Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered.


#include <iostream>
using namespace std;

int main()

{
int number = 0;

cout << "This program will let you enter number after\n";
cout << "number. Enter 99 when you want to quit the ";
cout << "program.\n";

while (number !=99)
cin >> number;
cout << "Done\n";
return 0;

}
Reputation Points: 11
Solved Threads: 0
Newbie Poster
wangstarr is offline Offline
18 posts
since Sep 2004
Sep 28th, 2004
0

Re: a little help

I think this would work:

#include <iostream>
using namespace std;

int main()
{

///////////////////////////////Creating variables/////////////////////////////////////

int number = 0;
int min, max;

///////////////////////////////Blah!Blah!Blah!Blah!///////////////////////////////////

cout << "This program will let you enter number after\n";
cout << "number. Enter 99 when you want to quit the ";
cout << "loop.\n";

///////////////////////For initialising values of min and max////////////////////////

cin>>number;

if(number==99) return(0); //if the number is 99 the program will quit

min = max = number; //initialize the values of min and max to compare with later inputs

///////////////////////////Loop to get the numbers/////////////////////////////////

while(number!=99) //loop
{
cin>>number;

/////////////////////////////Evaluating the numbers/////////////////////////////////

if(number ==99) break; //break the loop or else max will become 99 if this line is not there
else if(number>max) max = number; //if the no. is greater than max then it becomes the max
else if(number<min) min = number; // if the no. is smaller than min then it becomes min

} //end of loop

////////////////////Printing the smallest and biggest numbers/////////////////////

cout<<"The smallest number is: "<<min<<endl; //displays smallest no.
cout<<"The biggest number is: "<<max<<endl; //displays biggest no.
return 0;
}
Reputation Points: 15
Solved Threads: 1
Junior Poster
chound is offline Offline
143 posts
since Aug 2004
Oct 9th, 2004
0

Re: a little help

Little help please:

cout << "loop.\n"; does this play any role or its just an output -> "n"

I didn’t understand this part too:

min = max = number;

Why did we have to equal the min and max to number...? Which is 0?

Are max and min are reserved word for C++.
far
Reputation Points: 10
Solved Threads: 0
Newbie Poster
far is offline Offline
2 posts
since Oct 2004
Oct 9th, 2004
0

Re: a little help

>cout << "loop.\n"; does this play any role or its just an output -> "n"
I assume you mean \n. It's an escape character that tells cout to print a newline.

>Why did we have to equal the min and max to number...? Which is 0?
min and max denote the smallest and largest value encountered, respectively. Because at this point the only value encountered was number, both min and max must be that value.

>Are max and min are reserved word for C++.
Yes and no. The rules are a bit tricky, but in this case there's no problem.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ development on Linux/Unix?
Next Thread in C++ Forum Timeline: printing a linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC