Make an infinite loop and break out of it when number equals -1. What you are doing there is not quite right:
1 2 3 4 5 6 7 8 9
int sum = 0; // contains the summation of the input
while(true) // infinite loop
{
cout << "Input your data: ";
cin >> number;
if (number == -1) // Don't use = in the expression check, use == instead.
break;
sum += number; // Execution will not get to this point if number equals -1 (since it would have broken out of the loop)
}
Have you learned about the getline() command? I don't fully understand it yet, but your question sound like it might apply. I think after you get the line, you cycle through all the elements of the line, the numbers in your case, in a loop. There might be a tutorial lesson on it on this site.
Have you learned about the getline() command? I don't fully understand it yet
getline( istream&, string );
takes an input stream object reference (a file or standard (keyboard) input), and the desination string to store the contents, up to and not including the '\n' character from the input stream (assuming there is one). This is useful for reading in an entire line (NOT whitespace delimited), instead of using cin >> string, since the latter will stop at the first whitespace it sees and not read it in.
@OP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
int number;
int sentinel = 5;
while ( (cin >> number) && number != sentinel ) //this makes sure the input stream is intact, //and that something was read into number
{
//...
cout << number << " was entered...\n";
}
return 0;
}
Edit: The reason for the initial condition check: ( cin >> number ) is to check to make sure the input stream is not corrupted (this can happen if say, you enter a char when it expects an int or some other datatype).
If I type a 'a' when this program is run, it will exit out of the loop. cin is an input stream object that returns bool (true or false), depending on whether or not the input stream is intact, and when used with >> var, it also checks to make sure something was actually read in (and end of file was not encountered). Note that technically, the keyboard is a file. At least as far as your program is concerned.
If you want to do anything to the inputted numbers ourside the loop they were inputted in, you will need to store them somewhere like ne555 said. Alternative to using a container (the best way IMP), you could do dynamically allocated arrays:
#include <iostream>
usingnamespace std;
int main()
{
int number;
int sentinel = 5;
int* dynamicArray;
int initSize = 10;
dynamicArray = newint[initSize];
int i = 0;
while ( (cin >> number) && number != sentinel )
{
if ( i > initSize-1 )
{
//copy old array into temp array so we can resize
int temp[i];
for ( int j = 0; j < i; j++ )
{
temp[j] = dynamicArray[j];
}
delete[] dynamicArray;
initSize += 10; //double the size
dynamicArray = newint[initSize];
//now copy it back
for ( int j = 0; j < i; j++ )
{
dynamicArray[j] = temp[j];
}
}
dynamicArray[i] = number;
i++;
}
for ( int j = 0; j < i; j++ )
{
cout << "Saved int " << j << " " << dynamicArray[j] << "\n";
}
//edit: deallocate memory...
delete[] dynamicArray;
return 0;
}
Maybe now you can see why using a collection makes more sense... (And btw, does anyone know if this solution can be made more efficient/is worth doing at any time over using a collection?)
You are basically emulating a vector (and you are doing it wrong), so it wouldn't be more efficient.
1 2 3 4 5 6 7 8 9 10
//The part you copy the array can be improved
//int temp[i]; //i is not const. This goes against the standart
int *temp = dynamicArray;
int tempsize = initSize;
//initSize += 10; //double the size
initSize *= 2; //this will double the size
dynamicArray = newint[initSize];
for(int K=0; K<tempSize; K++)
dynamicArray[K] = temp[K];
delete [] temp;
You can initialize a variable (holding a negative number as a value at the start) as your maximum, and also initialize a variable (holding the largest possible value for an integer) for your minimum. Per loop, if a comparison between a variable and your current number returns that your current number is bigger than the maximum or smaller than the minimum, rewrite that variable.