End loop with Ctrl+Z

In this program I have a vector to save new inputs from user into. I need to test for three conditions(if it's an integer, if int is between 0 & 100), and if correct add input to vector, and if not valid input then display error message. Then I'm trying to end loop with Ctrl+Z input from user.

Problem: I can't end loop with Ctrl+Z

Everything in my program works fine except being able to end the loop. When user types Ctrl+Z it displays as "^Z", so that is what I've been testing for. However, when user types Ctrl+Z, the loop doesn't end, but it doesn't function properly either. It leaves an empty command line, where if you enter anything then it starts the loop over. I've scoured every resource to find this answer, but no luck. Here's my code, the portion I am having trouble with has asteriks in the note line:

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
57
58
59
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

//declaring constants used
const int ZERO = 0;
const int ONE_HUNDRED = 100;

//declaring class
void main()
{
	//declaring variable
	int homework;

	//declaring vector
	vector <int> scores;

	//initiating loop
	while (true)
	{
		//prompt user for input
		cout << "\nEnter score for homework assignment (press Ctrl+Z to quit): ";

		//save input
		cin >> homework;

		//testing if input is valid integer
		if (!cin.good())
			//****testing if user typed Ctrl+Z, then breaking from loop****
			if (homework == '^Z')
				break;
			//testing if user typed any other variation of letter & integers, etc., then displaying error message
			else
			{
				cout << "\nInvalid Input. Entry must be an integer." << endl;
				cin.clear() ;
				cin.ignore(std::numeric_limits<streamsize> :: max(), '\n') ;
			}
		//if input is valid integer then..
		else
		{
			//testing if input is between 0-100, then save input to vector
			if(homework >= ZERO && homework <= ONE_HUNDRED)
			{
				scores.push_back(homework);
			}
			//if input is <0 or >100, display error message
			else
			{
				cout << "\nInvalid Input. Grade must be between 0-100." << endl;
				cin.clear() ;
				cin.ignore(std::numeric_limits<streamsize> :: max(), '\n') ;
			}
		}
	}

	system("PAUSE");
}



'homework' is a number. Think for a moment how much sense it makes to check if a number is the same as a word.

Ctrl+Z appears on the screen as ^Z, but is actually EOF. You need to check if your input scream has reached the EOF.
I have tried it that way, testing for EOF, but my problem with that is any letter character entered qualifies as an EOF. So what my program does when I use code testing for an EOF, any letter character entered ends the loop. I need to be able to test if any other characters besides '^Z' are entered.

Should I declare homework a string and see if it can convert to an integer, and if not test if it's '^Z'?
I have tried it that way, testing for EOF, but my problem with that is any letter character entered qualifies as an EOF.
What exactly did you do?

Should I declare homework a string and see if it can convert to an integer, and if not test if it's '^Z'?
Again, ^Z is just the way Ctrl+Z appears on the screen. If you test for that, the condition will not be met when the EOF is actually reached, but when the user literally enters a circumflex follower by a capital Z.
I declared homework = EOF in loop and then tested. And any letter character entered ends the loop since EOF is used as the value to represent an invalid character. Here's the code:

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
57
58
59
60
61
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

//declaring constants used
const int ZERO = 0;
const int ONE_HUNDRED = 100;

//declaring class
void main()
{
	//declaring variable
	int homework;

	//declaring vector
	vector <int> scores;

	//initiating loop
	while (true)
	{
		//*****declare homework as EOF******
		homework = EOF;
		//prompt user for input
		cout << "\nEnter score for homework assignment (press Ctrl+Z to quit): ";

		//save input
		cin >> homework;

		//testing if input is valid integer
		if (!cin.good())
			//****testing if user typed Ctrl+Z, then breaking from loop****
			if (homework == EOF)
				break;
			//testing if user typed any other variation of letter & integers, etc., then displaying error message
			else
			{
				cout << "\nInvalid Input. Entry must be an integer." << endl;
				cin.clear() ;
				cin.ignore(std::numeric_limits<streamsize> :: max(), '\n') ;
			}
		//if input is valid integer then..
		else
		{
			//testing if input is between 0-100, then save input to vector
			if(homework >= ZERO && homework <= ONE_HUNDRED)
			{
				scores.push_back(homework);
			}
			//if input is <0 or >100, display error message
			else
			{
				cout << "\nInvalid Input. Grade must be between 0-100." << endl;
				cin.clear() ;
				cin.ignore(std::numeric_limits<streamsize> :: max(), '\n') ;
			}
		}
	}

		system("PAUSE");
}


I also tried loop without declaring homework = EOF, but loop doesn't function properly. What it does when I don't declare homework = EOF; the display message "Invalid input" is displayed, then an empty command line until a key is pressed, then the loop starts all over again. The loop basically cannot end.
Once again, homework is a number. Is EOF a number? Technically, yes it is, but conceptually? Can you mathematically deduce which number denotes the end of a file? I apologize if this sounds condescending. I'm just trying to get you to think about what you're doing and see how little sense it makes.
Additionally, homework will equal an integer value representable by the string of numerals the user entered. But the EOF is not reachable by entering any string of numerals.
Because of the above points, there's no value homework can take such that homework == EOF if and only if the EOF was reached.

std::cin has the method eof() that returns true when the EOF has been reached.
I don't feel like your response is condescending :) I understand homework is a number. I super, duper appreciate your help! It's just been frustrating trying to use the info taught in class for this project and the classmates I've been talking with have been struggling, too. I will reply to post when I figure out a solution.
just put while( !cin.eof() ) instead of while( true ). basically you read while it does not equal the end of file. The EOF you were using was a macro but you should be using the method as helios mentioned.

http://www.cplusplus.com/reference/ios/ios/eof/
http://www.cplusplus.com/reference/istream/istream/


*On a side note I would suggest you try to steer away from system("PAUSE"); There are better methods to keep the window open.
http://www.cplusplus.com/forum/beginner/1988/
Last edited on
Topic archived. No new replies allowed.