looping until 'x' is entered

I tried this and it is not working since input is an integer and 'x' is a char
how can i make this work. I want to make the loop exit when the user presses x,
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

#include<iostream>



int main()
{
int* numbers = new int [size];
int input;
int i=0;

std::cout<<"Enter some integers or x to exit;
std::cin>>input;
while(input != 'x') 
{

numbers[i]=input;

i++;

std::cin>>input;
}



}
 
One thing to point out is something you already said yourself:

input is an integer and 'x' is a char

This won't work because the condition of the while statement will keep looking for something that can never be true in this integer/character case, thus the loop keeps going on. Another way to look at it is the condition is looking for an apple when it can only see oranges, so to speak.

If you want the user to use numbers and then be able to exit a loop, then another number that is usually not used (such as -1) can suffice in this instance.
Line 8: size must be set before it.

Line 13: input expects an integer as you declared in line 9.

If you will enter more than size integers the program will crash (overload)
Last edited on
yeah I know but I was wondering if there is a way. because i have a project where the teacher made the same project and it exits with 'x' can't figure out how he did it.
Indeed, if enter 'x' the program will stop with crash(!), but the size must be set before line 8.

Also at the end put:

delete[] numbers;
Last edited on
I have nothing to do so I put this together. I hope the comments are enough of an explanation.
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
47
48
49
50
51
52
53
54
55
56
#include <vector>
#include <iostream>

int main()
{
	// std::vector resizes itself if we input more objects that it can contain.
	// Something that plain arrays can't do on their own
	std::vector<int> numbers;
	int input;

        // ios::good() checks if a stream is able to operate properly.
	while(std::cin.good())
	{
		// istream::operator>> returns the stream itself (cin).
		// The stream can be used as a bool to check if the last input operation
		// succeeded. The operation fails if the input can't be interpreted as a
		// value of the type of the variable ('x' can't be read as a number so it fails).
		if( !(std::cin >> input) )
		{
			// Reset cin to a good state to make it able to read again.
			std::cin.clear();

			// Read the non-number that was entered.
			char c;
			std::cin >> c;

			// tolower() converts a character to lowercase, so 'X' can be used
			// to exit the loop too.
			if(std::tolower(c) == 'x')
			{
				// Inform that the program is continuing properly.
				std::cout << c << " found. Quitting." << std::endl;
				break;
			}
			// Inform of the error.
			std::cout << "Invalid input: " << c << std::endl;

			// If c was garbage we want to continue reading
			// but we don't want to get to the point where the invalid
			// input is stored (that is, vector::push_back()).
			// So we restart the loop.
			continue;
		}
		
		// If all is well, we store the input.
		numbers.push_back(input);
	}
	
	// Print back the input sequence to see if it correct.
	std::cout << std::endl << "Valid numbers entered:" << std::endl;
	for(int i = 0; i < numbers.size(); ++i)
		std::cout << numbers[i] << ' ';
	std::cout << std::endl;
	
	return 0;
}


It has two "bugs":
1) It doesn't read strings. If you input "roxanne" it will read and discard 'r' and 'o', then read 'x' and terminate.
2) It doesn't work with decimal numbers. If you write "12.34" it reads "12", then '.' as a character and discards it, then "34".
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main ()
{
    std::cout << "enter the single character 'x' to quit the program\n" ;

    while( std::cout << "number (or x)? " && std::cin.peek() != 'x' )
    {
        int n ;

        if( std::cin >> n ) std::cout << "you entered " << n << '\n' ;
        else std::cout << "you did not enter a number\n" ;

        std::cin.clear() ; // clear the error state if any
        std::cin.ignore( 1000, '\n' ) ; // throw away everything else on this line
    }

    std::cout << "you entered an 'x': quitting\n" ;
}
Topic archived. No new replies allowed.