How do you recognize multiple user inputs and extract a sentinel value

Pages: 12
User puts in as many numbers as they want and then use -1 to say they would like to stop inputting numbers.

IE:

Input your numbers:
4 20 28 299 65 83 -1

or i can do on separate lines

Input your numbers:
6
4
3
49
-1

i can't figure out how to recognize each number separately (i assume its %10, /10)

and i can't figure out how to remove the -1 from the numbers once they numbers are input.

So basically:

How do i separate X amount of user inputted numbers and how do i remove a -1 from them.

If this isn't clear sorry i can clear it up more.
Last edited on
to read a number std::cin>>number;
So you make a loop, and when you find a -1 just break.
When i do that

and i try to find the sum of all the numbers entered by using %10 and /10 it includes the -1 value

and is there an easier way of extracting all the numbers entered? I need to use the input for multiple different things?
Could you show me what that loop might look like?

while ( numbers != -1 )
{
cout << "input your data: ";
cin >> numbers

if ( number = -1)
break;
}


is that close to right?
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.
i think i need to use a function to derive each number given by the user

then perform multiple arithmetic operations on the numbers taken out

so in english the code should go

Input your data
read data

function to derive all numbers

case switch to allow user to choose what they want done with the numbers
Sum
Average
high and low numbers
a few others


so: Once the numbers are inputted how do i separate each single one IE what might the function look like?



Thank you guys very much for your help.
closed account (Lv0f92yv)
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>
using namespace 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.
Last edited on
To desh:

After i do the initial input loop which allows the user to enter numbers until they don't want to.

IE the loop you provided.

then i do another loop to find each number of the function correct?

in code/english hybrid

while(number still has digits)
number & 10
number / 10


now how do i save each number that it extracts this way?
I don't understand you. Do you want numbers or digits?
For save the numbers put them in a container (queue, stack, list, vector, multiset, etc)
closed account (Lv0f92yv)
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:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

int main()
{
	int number;
	int sentinel = 5;

	int* dynamicArray;
	int initSize = 10;
	dynamicArray = new int[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 = new int[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?)
Last edited on
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 = new int[initSize];
for(int K=0; K<tempSize; K++)
  dynamicArray[K] = temp[K];
delete [] temp;
Last edited on
closed account (Lv0f92yv)
I actually did not know that was what I was doing... Interesting.

Edit: Not sure why I put double there when I was adding.........
Last edited on
i figured out how to extract the -1

how would i figure out the highest and lowest numbers inputted by the user?
Last edited on
i'm assuming it would involve the

max(a,b)
and
min(a,b)

functions but i don't know how to separate each number within the previous loop.

This is the loop i am using

int sum = 0, number, average = 0, count = 0;
while(true) // infinite loop
{
cout << "Input your data: ";
cin >> number;
if (number == -1)
break;

sum = sum + number;
count++;
average = sum / count;

}

this allows me to get the average, the sum, and the number of numbers, but i need the highest and lowest numbers also. how do i do that?
Please?! Anybody?!
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.

Does this give you an idea of what to do?

-Albatross
i forgot to mention i changed everything to doubles but that wouldn't change anything except the maximum would be much higher right?

i am still fairly confused as to what to do

double x = -5, y = (maximum double);

if ( maxdouble - number > number )

.....how do i compare numbers i think i haven't learned that yet
Sorry I'm late.

Forget the sentinel value. When the user enters nothing he is done.
http://www.cplusplus.com/forum/beginner/2409/#msg9254

1
2
3
#include <iostream>
#include <sstream>
#include <string> 
1
2
3
4
5
6
7
8
string s;
while (getline( cin, s ) && !s.empty())
  {
  istringstream ss( s );
  double x;
  ss >> x;
  /* do something with x here */
  }

Hope this helps.
can't use getline

only iostream and cmath

i think what albratross said would be the answer i just don't know how to translate it into code.

another quicker question:

why would this loop run even when choice = 'S', 'Q', 'N', or 'A'

while (choice != 'S' || choice != 'A' || choice != 'Q' || choice != 'N')
{
cout << "Invalid please enter choice: ";
cin >> choice;
cout << endl;
}
Pages: 12