Great number help

I am going to be upfront this is for a homework assignment. I have been online and found I could've made this so much easier just making an array but I am stubborn and want this to work with the original code.

Basically I am tasked with making a series of numbers then have the greatest number being the output after I cancel the program. The professor requires that the variables are clear for grading purposes that is why I am using variables max and number.

My issue is I cannot figure out the code to find that largest number or how to force it to recognize the that max>number. If you have an article for me to read I would love to find it because my book doesn't give much guidance and the things I have found online are in reference to arrays or a series of like 3-5numbers. If it is a simple fix that is awesome too but would still like to read about the logic.



#include <iostream>
using namespace std;
int main()


{

int number, max=0;
cout << "Enter a series of numbers and this program will find the largest. \n"
<< "Enter 0 will terminate the sequence of input values \n";


cout << "enter a number: ";
cin >> number;


while (number !=0)
{
cout << "enter a number: ";
cin >> number;
if (number < 0)
{
cout << "that is invalid enter a positive number \n";
}
}
if (number >= max=0)
number=max;
cout << "the largest number is: \n" << max;
return 0;
}
Use code tags when posting code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

int max;
int number;

cout<<"Enter number: ";
cin>>number;
max = number;

while(number != 0)
{
   cin>>number;
   if(number > max)
         max = number;
}
cout<<"The max is "<<max<<endl;
I can do that, also the adjustments above did have the "this max is" statement but keeps equating it to 0
Maybe coz you start input with 0.

This is the complete code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{
    int number = 0;
    int max = 0;

    cout<<"Enter 0 to stop\n\n";

    cout<<"enter number: ";
    cin>>number;
    max = number;

    while(number != 0)
    {
        cout<<"Enter number: ";
        cin>>number;
        if(number > max)
            max = number;
    }
    cout<<"The max is: "<<max<<endl;
    cin.ignore();
    return 0;

}
oh so I have to input the greatest value statement inside the while statement brackets? along with the condition that if it is less than 0 to input a new response? lol last question (I sadly cannot compute this because I am leaving the lab and about to head to class) does this allow for an infinite number of integers to be input and it'll pick the largest?
By "the greatest value statement" you mean "The max is", then NO.
Just follow as i wrote it.

The algorithm will get the largest number of infinite number of inputs.
1
2
3
4
5
6
7
8
cout << "enter a number: ";
cin >> number;


while (number !=0)
{ 
cout << "enter a number: ";
cin >> number; 
@OP something like that you should use a do/while

1
2
3
4
5
do
{
    cout << "Enter a number: ";
    cin >> number;
} while(number);
Topic archived. No new replies allowed.