Program to display the occurrence of max numbers and print the maximum The C++ p
ID: 3763983 • Letter: P
Question
Program to display the occurrence of max numbers and print the maximum
The C++ programming code is below:
#include<iostream>
using namespace std;
int main()
{
int max = 0, count = 0;
int num;
cout << "Enter the numbers (program exits if the value is zero)" << endl;
cin >> num;
while (num!=0)
{
if (num > max)
{
count = 1;
max = num;
}
else if (num == max)
{
count++;
}
cin >> num; // why I need this line ??
}
cout << "The largest number is" << " "<<max << endl;
cout <<"The occurrence count of the largest number is "<<" "<< count<<endl;
system("pause");
return 0;
}
The question is why I need"cin >> num;" this line, if I delete this line , the program won't run as I expect.
Cause I can't understand the usage of this line, can I rewrite this program in another way without using the array? or you have better suggestion of writing this program.
Explanation / Answer
That's where the newline is coming from. You can't read your number from cin before this newline appears in the console, because the newline is what causes the console to hand your input over to the program in the first place
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.