How do I read an input of an arbitrary amount of numbers instead of a specific amount?

I'm trying to make a program that allows the user to input an arbitrary amount of numbers and finding the largest of all the inputs but I keep having problems with the output.

Also I can't figure out the format to insert my code so that it shows lines and such; I'm new to this site. Apologies if its a pain to read.

javascript:tx('
#include <iostream>
using namespace std;

//******************************************
//CLASS COMPARATOR
//******************************************

class comparator
{
public:
comparator();
void getMaxNum();
void displayMaxNum() const;
private:
int maxNum;
};

//******************************************
//CLASS DEFINITION
//******************************************

comparator::comparator()
{
cout << "Enter numbers you want to compare: ";
return;
}

void comparator::getMaxNum()
{
cout << "\n\nDid we get to this part?" << endl << endl;
int num;
while ( (num = cin.get() ) != EOF) // THIS LINE HERE I THINK IS MY TROUBLE
{
cin >> num;
if (num > maxNum)
{
maxNum = num;
} // end if
} // end while
return;
} // end function getMaxNum

void comparator::displayMaxNum() const
{
cout << "The highest number you entered was " << maxNum << endl;
return;
}


//**********************************************************
// MAIN
//**********************************************************



int main()
{
comparator compareNums; //Initializes object to compare numbers, calling constructor to print message
compareNums.getMaxNum();
cout << endl << endl;
compareNums.displayMaxNum();

return 0;
}')

and regardless of what numbers I enter, I always get the output of 10 and I have no idea why.


Also I got the EOF idea from my textbook so if there is a better way of doing this I'd like to hear it. I don't know any clear ways that looks nice to end the while loop when the user doesn't have any more numbers to enter. Thanks.
Last edited on
First: cin doesn't have EOF because it actually is not a file.
Second: get() returns a character (like '0' = 48, not 0). See:

http://www.cplusplus.com/reference/istream/istream/get/

See what happens:
1
2
3
4
5
6
7
8
9
10
11
12
// istream::get example
#include <iostream>     // std::cin, std::cout
#include <fstream>      // std::ifstream

int main () {

  int c;
  while ((c = std::cin.get()) != EOF)          // loop getting single characters
    std::cout << c;

  return 0;
}



Since get() removed the first character of the numeric string only the remaining digits are used to form a number. Check this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// istream::get example
#include <iostream>     // std::cin, std::cout
#include <fstream>      // std::ifstream

int main () {

  int c;
  while ((c = std::cin.get()) != EOF)          // loop getting single characters
  {
      std::cin >> c;
    std::cout << c;
  }

  return 0;
}


Better ask for the amount of numbers and loop according to that.
cin doesn't have EOF because it actually is not a file.
Technically you can send End Of Stream yourself, Ctrl+D on Linux, Ctrl+Z on Windows.

There are several ways to do what you want depending on needed input structure (how input should look)

If you want to read all numbers until stream end, this is most common choice:
1
2
3
int c;
while (std::cin >> c) //While we successfully read numbers
    std::cout << c;
Hope this helps

#include <iostream>

cout << "*********************************************" << endl;
cout << "* Created by Zamuxolo Gomo *" << endl;
cout << "*********************************************" << endl << endl;

using namespace std;

const int size =100;

class Comparator {
public:
Comparator();
void getNumbers();
int compareNumbers();
private:
int count;
int num;
int arr_num[size];
int max;
};

Comparator::Comparator() {
max = 0;
}
void Comparator::getNumbers() {
count = 0;
while(num != 0) {
cout << "Please enter number:(0 to stop):";
cin >> num;

arr_num[count++] = num;
}
}
int Comparator::compareNumbers() {
for(int i = 0; i <= count; i++) {
if(arr_num[i] > max)
max = arr_num[i];
}
return max;
}
int main()
{
Comparator c;

c.getNumbers();
cout << "The biggest number is " << c.compareNumbers() << endl;

return 0;
}

Last edited on
Topic archived. No new replies allowed.