multiple integer output

Oct 21, 2008 at 1:00am
Alright(I've asked before, didn't get a clear answer), this program uses a sentinal controlled loop(-99) and calculates the greatest and least number of integers. Question is how I would output the series of integers.

#include <iostream>
using namespace std;

int main()
{

int num, greatest, least;
char doAgain;

do
{ cout << "This program lets you input a series of numbers and finds\nthe greatest and least values.\n";
cout << "Type in a value. -99 will finish the series.\n";
cin >> num;

if (num == -99)
cout << "\nNo values were entered, so there is no smallest and greatest value.\n";
greatest = num, least = num;

while (num != -99)
{
{if (num > greatest)
greatest = num;
else if (num < least)
least = num;

cout << "Enter another value.\n";
cin >> num;}
}

if (greatest != -99 && least != -99)
{ cout << "\nSmallest value: " << least << endl;
cout << "Greatest value: " << greatest << endl;}

cout << "\nWould you like to do this again?(Y/N)\n";
cin >> doAgain;
cout << endl;}
while (doAgain == 'y' || doAgain == 'Y');

return 0;

}

The integer num needs to be outputted before the greatest and least values are outputted. Having an unlimted number of integers, I can't use separate variables and so I'm have some trouble as to how to output the values.

I learned about using a loop to read data from a file like this

while (!inputfile.eof())
{ cout << numer << " ";
inputfile >> number; }

and was wondering if something like this could be used for the user input cin that I'm using. Or perhaps I need to rewrite the code in a different way?
Oct 21, 2008 at 1:07am
As I said in PM :)

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
27
28
29
30
31
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  vector<int> vNumbers;

  int input = 0;
  char cInput[100] = {0};

  while (input > -99) {
    cin.getline(cInput, 100);
    input = atoi(cInput);

    //if (input > greatest)
    //if (input < lowest)
    // etc

    vNumbers.push_back(input);
  }

  vector<int>::iterator vPtr = vNumbers.begin();
  while (vPtr != vNumbers.end()) {
    cout << "Number Entered Was: " << (*vPtr) << endl;
    vPtr++;
  }

  return 0;
}
Last edited on Oct 21, 2008 at 1:32am
Oct 21, 2008 at 1:30am
Anything in a more simple form? (My compiler is having a problem with line 11 btw) My programs aren't going to be that advanced right now and I haven't learned the vector headline either : /
Oct 21, 2008 at 1:33am
Ok fixed so it'll compile atleast.

But that is only a guideline. Take it, use it, enhance it and play with it.

Not going to give you a complete solution, as they you won't learn. But it's fairly simple.

atoi() - convert char[] to int
Oct 21, 2008 at 2:03am
I'm trying to use it as a guide line but there's too many new words that I don't know and that the class hasn;t gone over, I don't think something this "off track" would work. I was hoping someone could explain how I could use the data statement I mentioned above and use it with cin. My instructor said its possible and I'm not finding anything in the book on how to do that.
Oct 21, 2008 at 7:36pm
Without imposing hard-limits in your application you have to go through dynamic memory and allocation. The best way to do this is to use a vector.

Unless you want hard limits?
Topic archived. No new replies allowed.