Reading two Files and comparing in C++ - c++

I have been trying to compare the user input to a file, I was able to open it. let me just show it
cout<< "Please Enter your Student ID: ";
cin >> stdID;
cout<< "Please Enter your Course ID: ";
cin >> courseID;
if the courseID is similar to the prerequisite file, eligible, if not, not eligible..
this is the file
course Prereq
cs111 NA
cs112 cs111
cs240 cs112
cs241 cs240
cs214 cs112
cs215 cs112
cs218 cs111
cs310 cs215
cs311 cs112
cs317 cs215
cs318 cs218
cs324 cs240
cs341 cs241

For something simple like this, I would read the input in a simple while loop, using the input operator >> to read into two strings (and relying on the fact that the operator>> function returns the stream, and that streams can be used in boolean conditions).
Then in the loop compare the user-input with the first string, and if it's a match then output the second string (if it's not e.g. "NA").

Related

How to detect a single Enter key in C++?

I am trying to get an input such as 'Country name'.
If you just press Enter, Country name should be set to default country name (ex)USA). Otherwise, input string would be set to country name. I am confused to how to detect input as a single Enter key or normal string.
Use std::getline() to read a whole line of user input up to the ENTER key (which will be read and discarded for you). If the returned string is empty, replace it with a default value as needed.
std::cout << "Country name: ";
std::countryName;
std::getline(std::cin, countryName);
if (countryName.empty())
countryName = "USA";
Try this
std::cout << "Country name: ";
std::getline(std::cin, countryName);
if(countryName==""){
countryName="USA"
}
also use
cin.ignore(); after every use of cin>> if you have to use getline() in the next line.
you can also use
cin.sync();
cin.get();

Can I use enter as an input?

I am trying to allow the user to either enter a string or just hit enter, and in that case I would use a default string.
cout << "Where should I save the exam (default (./)exam.txt): " ;
cin >> exam_filename;
But right now you can enter a string and it works fine, but if you hit enter it just keeps waiting for the user to type something. Any suggestions??
Okay so when I do this:
string exam_filename;
getline(cin, exam_filename);
if (exam_filename.empty())
// set to default string
now it always sets the string to the default string. It never gives me a chance to enter anything it just moves on the next part of the program autmoatically.
You really want to read a line. Just do it:
string exam_filename;
getline(cin, exam_filename);
if (exam_filename.empty())
// set to default string

Check if "cin" is a string

I have a simple little script I am coding, and I am trying to not allow people to enter a string, or if they do make it revert to the beginning of the function again. Here's the input code I have:
int main()
{
cout << "Input your first number" << endl;
cin >> a;
cout << "Input your second number" << endl;
cin >> b;
}
The rest of the code beyond this part works just fine for what's going on, although if a string is entered here it obviously doesn't work.
Any help would be appreciated.
You may find this post useful,
How to check if input is numeric in C++
Basically you can check the input, whether it is numeric value or not. After checking whether the given input is numbers, then you can add a while loop in main to ask user to repeat if input is not a valid number.
Every input is a string. If you want to know if a entered string can convert to a number, you have to read in a string and try to convert it yourself (eg with strol).
An alternative would be to check if the reading from cin failed, but personally i don't like it because cin.fail() covers more error situations than just a failed type conversion.
There's a library function may help,you can check it after input:
int isdigit(char c);
Tips:
1.You should include such files :
# include <ctype.h>
2.If c in 0 ~ 9 ,return 1 ; else return 0.

Code Snippet Works in Certain Cases but not as Expected, Why?

I have this code snippet that is supposed to test whether the user enters an integer or not. This works if the user enters letters, but not decimals and I'm left wondering why that is. Here's my code snippet:
Student student;
int id;
while(!(cin >> id))
{
cout << "\nERROR: Please enter a Positive Whole Number" << endl;
cin.clear();
cin.ignore ();
cout << "Enter Student ID: ";
}
Entering A will make it iterate through the while loop, but if I enter 12.5 it drops out of the while loop and keeps going. Isn't it testing whether it will parse to integer or not? Why is it accepting 12.5 but not characters?
cin>>id will succeed as long as it finds something it can convert to an int ("12", in this case). When it reaches something it can't convert, it stops, but if it's read an int already, that counts as success.
To check that everything it read was digits, you might want to do something like using std::getline to read a line of input into a string, then use std::isdigit to test whether those are all digits. Testing a conversion to int (by itself) will only tell you that it found something that could be read as an integer, but won't tell you if that was followed by other things that couldn't be converted to an int.

C++: how do I check if the cin buffer is empty?

How do you check to see if the user didn't input anything at a cin command and simply pressed enter?
When reading from std::cin, it's preferable not to use the stream extraction operator >> as this can have all sorts of nasty side effects. For example, if you have this code:
std::string name;
std::cin >> name;
And I enter John Doe, then the line to read from cin will just hold the value John, leaving Doe behind to be read by some future read operation. Similarly, if I were to write:
int myInteger;
std::cin >> myInteger;
And I then type in John Doe, then cin will enter an error state and will refuse to do any future read operations until you explicitly clear its error state and flush the characters that caused the error.
A better way to do user input is to use std::getline to read characters from the keyboard until the user hits enter. For example:
std::string name;
getline(std::cin, name); // getline doesn't need the std:: prefix here because C++ has ADL.
ADL stands for argument-dependent lookup. Now, if I enter John Doe, the value of name will be John Doe and there won't be any data left around in cin. Moreover, this also lets you test if the user just hit enter:
std::string name;
getline(std::cin, name);
if (name.empty()) {
/* ... nothing entered ... */
}
The drawback of using this approach is that if you want to read in a formatted data line, an int or a double you'll have to parse the representation out of the string. I personally think this is worth it because it gives you a more fine-grained control of what to do if the user enters something invalid and "guards" cin from ever entering a fail state.
I teach a C++ programming course, and have some lecture notes about the streams library that goes into a fair amount of detail about how to read formatted data from cin in a safe way (mostly at the end of the chapter). I'm not sure how useful you'll find this, but in case it's helpful I thought I'd post the link.
Hope this helps!
cin will not continue with the program unless the user enters at least 1 character (enter doesn't count). If the user doesn't give ANY input, cin will just keep waiting for the user to give input and then press enter.
The Simple way >>
{
char X=0; // ASCII( 0 ) means a NULL value
cin>>X;
if(X==0 || X==10) // ASCII( 10 ) means ENTER
cout<<"User din't enter ANYTHING !! ";
}
But a simple problem is....
cin just won't allow you to move further without entering a character
by character here i mean a DIGIT or alphabet or special symbol , not space, enter null etc
Hope this solves your problem, if it doesn't, I'll be glad to help just let me know.
int main(){
string str[100];
std::cout<<"Hello how are you ? \n";
std::cin>>str;
if(str.length() > 0){
// If input is seen
}
else{
// If input is not seen
}
}
Any problem let me know.