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:
#include <iostream>
#include <vector>
#include <cstdio>
usingnamespace std;
//declaring constants used
constint ZERO = 0;
constint 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");
}
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:
#include <iostream>
#include <vector>
#include <cstdio>
usingnamespace std;
//declaring constants used
constint ZERO = 0;
constint 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.