check for specific integer while accepting strings in standard input - c++

cout << "\nEnter the filename for rainfall data (or -1 to quit): ";
How do I allow for both int and string input?
I have an object with the a string type and have accepted it from the user to be in that form. However, I must also test whether -1 was entered. I am a bit confused because how would I assign an integer value to a string?
We have not yet been taught how to use sstream so the conversion of the string to an int is not a solution.
I have tried the following:
cin >> externalFile;
if (externalFile = -1) {
// evaluate condition...
}
I have also tried concatenation and google.
Just need some guidance on what tool to use and where I went wrong
Thanks for any help.

Do a string comparison of the string representing a hyphen and a 1 "-1" like this:
If( "-1" == externalFile){
}

What you can do is read the input as a string, and then try to convert it to an integer with e.g. std::stoi and if it succeeds you have an integer.
You can then go on to check the value of the parsed integer for your control flow/logic.
Please let me know if you have any questions!

Your mistake is you are reinitializing the value of externalFile.
What is happening in your code is you are getting a value for externalFile, then saying if externalFile is now -1 do this. The problem is that this changes to boolean expressions,
and If I remember correctly, this statement will always be false because -1 is in the category 0 or lower, and if it was 1 or higher it would always evaluate true.
cin >> externalFile;
if (externalFile == -1) {
// evaluate condition...
}
Your also trying to compare externalFire with an integer.
You simply cannot do this.
To fix this, you can simply create externalFile as a string, like this:
Corrected Version:
cin >> externalFile;
if (externalFile == "-1") {
// evaluate condition...
}

Related

while loop running for every digit/character from input

Hey guys beginner in C++ and coding in general. I am currently making a tictactoe program. For the part of the program I am validating user input. Since it is a 3x3 table, I want to make sure their input is an integer and that they choose a number between 1~9.
To do this I wrote
//Validating user input
void move() {
std::cout << "It's Player" << player << "'s turn!\n";
while(!(std::cin >> position)){
std::cout << "Please choose a NUMBER between 1~9!\n";
std::cin.clear();
std::cin.ignore();
}
while(position < 1 || position > 9){
std::cout << "Please choose a number BETWEEN 1~9!\n";
std::cin.clear();
std::cin.ignore();
}
while(board[position - 1] != " ") {
std::cout << "Already filled please choose another number between 1~9!\n";
std::cin >> position;
}
}
It works but for some reason when I put in an input like 10, it would print Please choose a number BETWEEN 1~9! twice (for each digit) and if I input in for example "apple" it would print Please choose a NUMBER between 1~9! four times (for each character). How do i make it just print out the statement once?
Thank you!
Let me try to explain to you the problem. It is a little bit subtle and not that easy to understand. Both other answers adress only the obvious part.
Then, let us first recap that:
The boolean condition in the while statement is loop invariant. Meaning, it will not be modified within the loop. Whatever it was before the loop, will be the same after the loop body has been executed. The condition will never change.
So, for the case where you enter a wrong number:
If the input number is correct (1..9) and the while statement starts to evaluate the boolean expression, it will be false in this case and the loop will not be entered.
If the number is out of your selected bounds (<1 or >9), then the boolean condition is true. The while loop starts, but the condition relevant variable will not be changed in the loop boody and hence, the boolean expression is always true. The loop will run forever.
Additionally, and now comes the answer to your first question, the following will happen:
The text "Please choose a number BETWEEN 1~9!\n" will be shown (first time)
clear will be called for std::cin. The failbit was not set, but anyway. This does not harm
The ignore function is an unformatted input function. It will actively read the next character from the input buffer, which is the end of line `'\n' character.
We enter again the while statement. The condition is still true (position was not modified in the loop body), and we enter the loop again.
The text "Please choose a number BETWEEN 1~9!\n" will be shown (second time)
clear will be called for std::cin. The failbit was not set, but anyway. This does not harm
The ignore function is a formatted input function. It will actively read the next character from the input buffer. But there is none. So it will wait until a key is pressed. For example "enter". After that, it would go back to number 5.
By the way. If you would now enter "abc" then you would see the text 4 times for a,b,c and enter.
So, please remember: ignore is an input function!
Next. It is important to understand, that if you enter an unexpected value, like "apple" instead of "3", the formatted input function >> can do no conversion and sets the failbit. It will also not extract further wrong characters from the input stream (std::cinis a buffered stream). The characters that could not be converted are still in the buffer and wil be read next time.
Please read here about formatted/unformatted input. And especially read about the extraction operatpr >> here..
There you can read the following:
If extraction fails (e.g. if a letter was entered where a digit is expected), zero is written to value and failbit is set.
OK, understood. Then, what is going on here, if you enter "abc". Basically, the same as above.
Enter abc
The boolean condition !(std::cin >> position)will be evaluated to true, because an 'a' was read and cannot be converted to a number.
The std::cin's failbit will be set. The variable positionwill be set to 0.
"Please choose a NUMBER between 1~9!\n" will be shown
The failbit will be reset
Ignore will extract exactly the one wrong character and discard it
std::cin >> position`` will be called again and extract the next wrong character 'b'. 3., 4., 5., 6. will be done again. Until the last charcter in the buffer, the newline '\n' will be extracted. Then you may enter the next number.
The fix for that problem is simple:
ignore has a parameter, where you can specify, how many characters shall be ignored. So, not only one, but all until the end of line.
You should write:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
This will ignore all bad input.
And to fix your whole program, you could write at the top:
while (!(std::cin >> position) or (position < 1 ) or ( position > 9)) {
std::cout << "Please choose a NUMBER between 1~9!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while(position < 1 || position > 9){
This while loop will continue running as long as position is less than 1 or greater than 9, that's what this says.
But there's nothing in the while loop itself that changes the value of position. For that simple reason, if position's value at this point is outside of the range of 1-9, this while loop will execute forever.
You always need to keep in mind The Golden Rule Of Computer Programming: your computer always does exactly what you tell it to do instead of what you want it to do. Here, you told your computer to execute this while loop as long as position is less than 1 or greater than 9, so this is what your computer will do until this is no longer the case.
You can change your code as this. With this you run your while loop ones for every input. Until you get the value as you like. If the value is as you wish, you get out of the loop with break
std::cout << "Please choose a NUMBER between 1~9!\n";
while(std::cin >> position){
std::cin.clear();
std::cin.ignore();
if(position < 1 || position > 9){
std::cout << "Please choose a number BETWEEN 1~9!\n";
}else{
break;
}
}

End array input with a newline?

Not sure if the title is properly worded, but what I am trying to ask is how would you signify the end of input for an array using newline. Take the following code for example. Not matter how many numbers(more or less) you type during the input for score[6], it must take 6 before you can proceed. Is there a method to change it so that an array can store 6 or 100 variables, but you can decide how many variables actually contain values. The only way I can think of doing this is to somehow incorporate '\n', so that pressing enter once creates a newline and pressing enter again signifies that you don't want to set any more values. Or is something like this not possible?
#include <iostream>
using namespace std;
int main()
{
int i,score[6],max;
cout<<"Enter the scores:"<<endl;
cin>>score[0];
max = score[0];
for(i = 1;i<6;i++)
{
cin>>score[i];
if(score[i]>max)
max = score[i];
}
return 0;
}
To detect "no input was given", you will need to read the input as a input line (string), rather than using cin >> x; - no matter what the type is of x, cin >> x; will skip over "whitespace", such as newlines and spaces.
The trouble with reading the input as lines is that you then have to "parse" the input into numbers. You can use std::stringstream or similar to do this, but it's quite a bit of extra code compared to what you have now.
The typical way to solve this kind of problem, however, is to use a "sentry" value - for example, if your input is always going to be greater or equal to zero, you can use -1 as the sentry. So you enter
1 2 3 4 5 -1
This would reduce the amount of extra code is relatively small - just check if the input is -1, such as
while(cin >> score[i] && score[i] >= 0)
{
...
}
(This will also detect end-of-file, so you could end the input with CTRL-Z or CTRL-D as appropriate for your platform)

c++ need help to understand

So I have this piece of code.
I understand all the things beside the fact that when does the loop actually take place again. I mean what is meant by the e(!valid) statement. Does it refer to its numeric value or what? Can somebody please explain this to me. Consider all required variable declared. And ignore uppercase.
The code is:
do
{
valid=1;
gotoxy(22,7);
gets(emailid);
int flag=0;
for (int i = 0; emailid[i] != '\0'; i++)
if (emailid[i] == '#')
flag++;
If(!flag)
{
valid = 0;
cout << "not a valid id. Try again";
getch();
}
} while(!valid);
So mainly I want to know that it is working, with emphasis on what does !valid and !fail mean.
From what I could get, it has to do with its numeric values but I am still confused.
To answer the question:
} while (!valid);
means: treat the integer behind valid as a boolean. (assuming it is an integer, as it was given a value of 1)
i == 0 -> false
i != 0 -> true
!valid:
valid == 0 -> true
valid != 0 -> false
In C++ the value 0 is considered "false" and any other integer is considered "true". In this case while valid is equal to 0 the loop runs.
Numeric value can be promoted to bool type this way:
Zero means false and other values mean true. So !valid will return true only if valid == 0.
What it means is your do while() loop will repeat itself until valid equals value other than 0.

C++ Do - While loop until a string meets certain criteria

I'm asking the user for an input, but I want the question to stay on screen until the input meets one of the allowed inputs. Here's my code
string input = "";
string departure = "";
cout << "Please enter an airport code: ";
do
{
getline(cin,input);
stringstream(input) >> departure;
} while(departure.compare("MAN") != 0 || departure.compare("EMA") != 0 || departure.compare("LHR") != 0 );
}
I want it to loop until the user enters MAN or EMA or LHR; also if they are lowercase I would like for it to be accepted aswell.
Every time I run this, even if I enter a correct input, it just keeps taking words in and doesn't do anything else.
The condition
departure.compare("MAN") != 0 || departure.compare("EMA") != 0 || departure.compare("LHR") != 0
is always true, regardless of what departure is.
compare returns 0 on equality. So what you're basically telling the compiler is
Run the loop while departure is different than "MAN" OR different than "EMA" OR different than "LHR".
You need && instead of || in your condition.
This condition always returns true since it can't not be all 3 at once.
The && will return false as soon as the input is one of the 3 accepted.
Consider using boost::to_upper to convert the input into upper case before you perform the comparison in the while(...) statment. This will resolve the lowercase/uppercase issue.
http://www.boost.org/doc/libs/1_41_0/doc/html/boost/algorithm/to_upper.html
Also, when dealing with C++ strings, I recommend you simply do
departure == "MAN" || departure == "EMA" || departure == "LHR"
You don't need to do string.compare in C++, unlike some other languages (for example Java), as the == operator is overloaded to compare the /content/ of the string, rather than the string object itself.
Also somebody else beat me to it about the compare method returning 0 when equal.
First Your conditional for the while loop is incorrect. Right now it reads, while departure is not 'MAN' or is not "EMA" or is not "LHR", continue looping. Because departure cannot be all three of them simultaneously, the loop never ends. I would suggest replacing your OR's (||) with AND's (&&)
As well, each execution of the loop you need to clear the value in departure, otherwise the previously entered lines persist and your comparison will fail even when a correct airport code is entered.
our main problem is that the string is being compared incorrectly. Let's say we type in "MAN".
The departure.comare("MAN") != 0 will be true if the string is not "MAN". Fine, we typed in "MAN", so it's false. Now we OR that with departure.compare("EMA") != 0 - which is true, because "MAN" is not equal to "EMA". So you need to combhine your condition with &&, not ||.
To fix for "owercase", there are two choices. Either convert the input string to uppercase, or compare with all different combinations of lower and upper case (Man, MaN, mAn, etc) - the latter gets very tedious very quickly.
Have a look at this one for some options of comparing strings in a case-insensitive way:
Case insensitive string comparison C++

Exercise Self Study Help

I've started learning C++ and am working through some exercises in the C++ Primer Plus book.
In chapter 5 one of the exercises is:
Write a program that uses an array of
char and a loop to read one word at a
time until the word done is entered.
The program should then report the
number of words entered (not counting
done). A sample run could look like this:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
You should include the cstring header
file and use the strcmp() function to
make the comparison test.
Having a hard time figuring this out. It would be much easier if I could use if statements and logical operators but I'm restricted to using only:
Loops
Relational Expressions
char arrays
Branching statments (ie if, case/switch ) and logical operators are not allowed.
Can anyone give me hints to push me in the right direction?
Edit: Clarification. The input must be one string. So, several words for one input.
Edit: oops, spec says to read into an array of char… I'm not going to bother editing, this is really stupid. std::string contains an array of char too!
cin.exceptions( ios::badbit ); // avoid using if or && to check error state
int n;
string word;
for ( n = 0; cin >> word, strcmp( word.c_str(), "done" ) != 0; ++ n ) ;
I prefer
string word;
int n;
for ( n = 0; cin && ( cin >> word, word != "done" ); ++n ) ;
Use this pseudo-code:
while (input != done)
do things
end-while
HINT: A loop could also act as a conditional...
integer count
char array input
count = 0
read input
while(input notequal "done")
count++
read input
done
print count
The input notequal "done" part can
be done as strcmp(input,"done"). If
the return value is 0 is means
input is same as "done"
reading input can be done using cin
You should define a max len for char arrays first. Then a do while loop would be sufficient. You chould check the string equality with the strcmp function. That should be ok.