Outputting spliced version of input - c++

#include <iostream>
#include <string>
using namespace std;
int main()
{
char name[256];
cout << "Enter your name: ";
cin.getline (name,256);
for(int i = 0; i < 50; i++)
{
cout << name[i];
if (name[i] == '\n')
break;
}
cout << endl;
return(0);
}
Enter your name: My name is Sean
once I do that it outputs "My name is Sean" but then a bunch of nonsense crazy symbols after that.
How come it doesn't stop after 'n' in Sean? Since there's a '\n' character there. Or at least I would think.
All, I want is to output every character in name one symbol at a time, and stop when it gets to the end of the line so that it doesn't have to go to 256 when the array is actually much smaller
What's wrong with my code?

cin.getline reads the newline (or whatever you specify as the delmiter) out of the input buffer, but does NOT include it in the string that's produced, so you're reading past the end of the data that was read until you encounter the next byte that happens to contain the value that corresponds to a new-line (typically 10).
It's a lot easier to just write it all together: cout << name;

From documentation:
Characters are extracted until either
(n - 1) characters have been extracted
or the delimiting character is found
(which is delim if this parameter is
specified, or '\n' otherwise). The
extraction also stops if the end of
file is reached in the input sequence
or if an error occurs during the input
operation.
If the delimiter is found, it is
extracted and discarded, i.e. it is
not stored and the next input
operation will begin after it. If you
don't want this character to be
extracted, you can use member get
instead.

Related

Cin in function [duplicate]

I wrote a very basic program in C++ which asked the user to input a number and then a string. To my surprise, when running the program it never stopped to ask for the string. It just skipped over it. After doing some reading on StackOverflow, I found out that I needed to add a line that said:
cin.ignore(256, '\n');
before the line that gets the string input. Adding that fixed the problem and made the program work. My question is why does C++ need this cin.ignore() line and how can I predict when I will need to use cin.ignore()?
Here is the program I wrote:
#include <iostream>
#include <string>
using namespace std;
int main()
{
double num;
string mystr;
cout << "Please enter a number: " << "\n";
cin >> num;
cout << "Your number is: " << num << "\n";
cin.ignore(256, '\n'); // Why do I need this line?
cout << "Please enter your name: \n";
getline (cin, mystr);
cout << "So your name is " << mystr << "?\n";
cout << "Have a nice day. \n";
}
ignore does exactly what the name implies.
It doesn't "throw away" something you don't need. Instead, it ignores the number of characters you specify when you call it, up to the char you specify as a delimiter.
It works with both input and output buffers.
Essentially, for std::cin statements you use ignore before you do a getline call, because when a user inputs something with std::cin, they hit enter and a '\n' char gets into the cin buffer. Then if you use getline, it gets the newline char instead of the string you want. So you do a std::cin.ignore(1000,'\n') and that should clear the buffer up to the string that you want. (The 1000 is put there to skip over a specific number of chars before the specified delimiter, in this case, the '\n' newline character.)
You're thinking about this the wrong way. You're thinking in logical steps each time cin or getline is used. Ex. First ask for a number, then ask for a name. That is the wrong way to think about cin. So you run into a race condition because you assume the stream is clear each time you ask for a input.
If you write your program purely for input you'll find the problem:
int main()
{
double num;
string mystr;
cin >> num;
getline(cin, mystr);
cout << "num=" << num << ",mystr=\'" << mystr << "\'" << endl;
}
In the above, you are thinking, "first get a number." So you type in 123 press enter, and your output will be num=123,mystr=''. Why is that? It's because in the stream you have 123\n and the 123 is parsed into the num variable while \n is still in the stream. Reading the doc for getline function by default it will look in the istream until a \n is encountered. In this example, since \n is in the stream, it looks like it "skipped" it but it worked properly.
For the above to work, you'll have to enter 123Hello World which will properly output num=123,mystr='Hello World'. That, or you put a cin.ignore between the cin and getline so that it'll break into logical steps that you expect.
This is why you need the ignore command. Because you are thinking of it in logical steps rather than in a stream form so you run into a race condition.
Take another code example that is commonly found in schools:
int main()
{
int age;
string firstName;
string lastName;
cout << "First name: ";
cin >> firstName;
cout << "Last name: ";
cin >> lastName;
cout << "Age: ";
cin >> age;
cout << "Hello " << firstName << " " << lastName << "! You are " << age << " years old!" << endl;
}
The above seems to be in logical steps. First ask for first name, last name, then age. So if you did John enter, then Doe enter, then 19 enter, the application works each logic step. If you think of it in "streams" you can simply enter John Doe 19 on the "First name:" question and it would work as well and appear to skip the remaining questions. For the above to work in logical steps, you would need to ignore the remaining stream for each logical break in questions.
Just remember to think of your program input as it is reading from a "stream" and not in logical steps. Each time you call cin it is being read from a stream. This creates a rather buggy application if the user enters the wrong input. For example, if you entered a character where a cin >> double is expected, the application will produce a seemingly bizarre output.
Short answer
Why? Because there is still whitespace (carriage returns, tabs, spaces, newline) left in the input stream.
When? When you are using some function which does not on their own ignores the leading whitespaces. Cin by default ignores and removes the leading whitespace but getline does not ignore the leading whitespace on its own.
Now a detailed answer.
Everything you input in the console is read from the standard stream stdin. When you enter something, let's say 256 in your case and press enter, the contents of the stream become 256\n. Now cin picks up 256 and removes it from the stream and \n still remaining in the stream.
Now next when you enter your name, let's say Raddicus, the new contents of the stream is \nRaddicus.
Now here comes the catch.
When you try to read a line using getline, if not provided any delimiter as the third argument, getline by default reads till the newline character and removes the newline character from the stream.
So on calling new line, getline reads and discards \n from the stream and resulting in an empty string read in mystr which appears like getline is skipped (but it's not) because there was already an newline in the stream, getline will not prompt for input as it has already read what it was supposed to read.
Now, how does cin.ignore help here?
According to the ignore documentation extract from cplusplus.com-
istream& ignore (streamsize n = 1, int delim = EOF);
Extracts characters from the input sequence and discards them, until
either n characters have been extracted, or one compares equal to
delim.
The function also stops extracting characters if the end-of-file is
reached. If this is reached prematurely (before either extracting n
characters or finding delim), the function sets the eofbit flag.
So, cin.ignore(256, '\n');, ignores first 256 characters or all the character untill it encounters delimeter (here \n in your case), whichever comes first (here \n is the first character, so it ignores until \n is encountered).
Just for your reference, If you don't exactly know how many characters to skip and your sole purpose is to clear the stream to prepare for reading a string using getline or cin you should use cin.ignore(numeric_limits<streamsize>::max(),'\n').
Quick explanation: It ignores the characters equal to maximum size of stream or until a '\n' is encountered, whichever case happens first.
When you want to throw away a specific number of characters from the input stream manually.
A very common use case is using this to safely ignore newline characters since cin will sometimes leave newline characters that you will have to go over to get to the next line of input.
Long story short it gives you flexibility when handling stream input.
Ignore function is used to skip(discard/throw away) characters in the input stream. Ignore file is associated with the file istream.
Consider the function below
ex: cin.ignore(120,'/n');
the particular function skips the next 120 input character or to skip the characters until a newline character is read.
As pointed right by many other users. It's because there may be whitespace or a newline character.
Consider the following code, it removes all the duplicate characters from a given string.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
cin.ignore(); //Notice that this cin.ignore() is really crucial for any extra whitespace or newline character
while(t--){
vector<int> v(256,0);
string s;
getline(cin,s);
string s2;
for(int i=0;i<s.size();i++){
if (v[s[i]]) continue;
else{
s2.push_back(s[i]);
v[s[i]]++;
}
}
cout<<s2<<endl;
}
return 0;
}
So, You get the point that it will ignore those unwanted inputs and will get the job done.
It is better to use scanf(" %[^\n]",str) in c++ than cin.ignore() after cin>> statement.To do that first you have to include < cstdio > header.

C++ getline is not empty when no input is entered

I am new to c++ and still trying to wrap my head around how input/output streams work.
I am currently trying to write a function to make sure the user enters an int, and tell them if the input is empty or not a valid int.
I am using getline and have tried using cin.clear and cin.ignore but i cannot seem to get this to work and have no idea where i am going wrong.
It works if I input a letter however if i just press enter with nothing input it doesn't say no input detected.
void testStuff()
{
string number;
ws(cin);//skips Whitespaces
if (getline(cin, number) && number.end() !=
find_if_not(number.begin(), number.end(), &isdigit))
{
if (number.empty())
{
cout << "No input detected" << endl;
testStuff();
}
cout << "Please input a Valid number" << endl;
testStuff();
}
}
Assuming your ws works as specified (skips whitespace in the input), by the time you call getline, something other than whitespace has to have been entered. Thus, when getline gets called, that non-whitespace character has to be waiting in the input buffer, and getline must return a non-empty sequence of characters (i.e., everything from that first non-whitespace character up to the next new-line).
For example, let's write our own ws that shows what character(s) it's skipping over:
void ws(std::istream &is) {
while (std::isspace(is.peek())) {
char ch;
is.get(ch);
std::cout << "Read: " << (int)ch << '\n';
}
}
Now, when we call testStuff() and just press enter, we get Read: 10 as our output--i.e., ws has read and skipped the new-line we entered.
So, to get to the call to getline, the user has to enter something other than whitespace, and a new-line is whitespace. So, but the time getline is called at all, we know there's some non-whitespace character waiting in the input buffer, so when getline is called, it must produce a non-empty result.
I have assumed that every of function(s) that i don't know implementation are written correctly. Then, I have such code (simplified):
#include <iostream>
#include <string>
using namespace std;
int main() {
string number;
if (getline(cin, number))
{
if (number.empty())
{
cout << "No input detected" << endl;
main();
}
cout << "Please input a Valid number" << endl;
main();
}
}
I don't know find_if_not(number.begin(), number.end(), &isdigit) implementation so I skipped it. I've put source code on Ideone.com, you can view it HERE. After passing "just enter", program behaves vaildly. This means, one of function implementations that you didn't show us is working incorrectly. To help you we need full source code (if not, just only needed parts). Also, you should skip "using namespace std;". I think, number.end() != find_if_not(number.begin(), number.end(), &isdigit)) is implemented incorrectly. You should think about what someone told you in comments - "If the string is empty the only thing find_if_not can return is number.end(). number.end() == number.end() and body is not entered."

Is it possible to read an empty string from cin and still get true from cin.good()?

My question is based on this simple code:
#include <string>
using namespace std;
int main() {
string buf;
while (cin >> buf && !buf.empty()) {
cout << "input is " << buf << "\n";
}
return 0;
}
The operator>> of cin (which is an object of type basic_istream) reads and discards any leading whitespace (e.g. spaces, newlines, tabs). Then operator>> reads characters until the next whitespace character is encountered. The operators returns finally the stream itself, cin.
It shouldn't be possible to enter an empty string without also setting at least one of the iostates eof, fail or bad? And therefore the streams converts with the operator bool to false. I think !buf.empty() is here superfluous, but a good habit. Is there a way to leave the iostate of cin in good and leaving the string empty?
Example usage:
1. type in a word of your choice
2. press enter
3. press Ctrl+d (EOF on UNIX) or Ctrl+d (EOF on Windows)
Thank you
No, it is not possible. If no characters could be extracted std::ios::failbit is set. [string.io]/3:
If the function extracts no characters, it calls
is.setstate(ios::failbit), which may throw ios_base::failure.
And if characters could be extracted they are subsequently appended to the string and thereby make its size non-zero.

When and why do I need to use cin.ignore() in C++?

I wrote a very basic program in C++ which asked the user to input a number and then a string. To my surprise, when running the program it never stopped to ask for the string. It just skipped over it. After doing some reading on StackOverflow, I found out that I needed to add a line that said:
cin.ignore(256, '\n');
before the line that gets the string input. Adding that fixed the problem and made the program work. My question is why does C++ need this cin.ignore() line and how can I predict when I will need to use cin.ignore()?
Here is the program I wrote:
#include <iostream>
#include <string>
using namespace std;
int main()
{
double num;
string mystr;
cout << "Please enter a number: " << "\n";
cin >> num;
cout << "Your number is: " << num << "\n";
cin.ignore(256, '\n'); // Why do I need this line?
cout << "Please enter your name: \n";
getline (cin, mystr);
cout << "So your name is " << mystr << "?\n";
cout << "Have a nice day. \n";
}
ignore does exactly what the name implies.
It doesn't "throw away" something you don't need. Instead, it ignores the number of characters you specify when you call it, up to the char you specify as a delimiter.
It works with both input and output buffers.
Essentially, for std::cin statements you use ignore before you do a getline call, because when a user inputs something with std::cin, they hit enter and a '\n' char gets into the cin buffer. Then if you use getline, it gets the newline char instead of the string you want. So you do a std::cin.ignore(1000,'\n') and that should clear the buffer up to the string that you want. (The 1000 is put there to skip over a specific number of chars before the specified delimiter, in this case, the '\n' newline character.)
You're thinking about this the wrong way. You're thinking in logical steps each time cin or getline is used. Ex. First ask for a number, then ask for a name. That is the wrong way to think about cin. So you run into a race condition because you assume the stream is clear each time you ask for a input.
If you write your program purely for input you'll find the problem:
int main()
{
double num;
string mystr;
cin >> num;
getline(cin, mystr);
cout << "num=" << num << ",mystr=\'" << mystr << "\'" << endl;
}
In the above, you are thinking, "first get a number." So you type in 123 press enter, and your output will be num=123,mystr=''. Why is that? It's because in the stream you have 123\n and the 123 is parsed into the num variable while \n is still in the stream. Reading the doc for getline function by default it will look in the istream until a \n is encountered. In this example, since \n is in the stream, it looks like it "skipped" it but it worked properly.
For the above to work, you'll have to enter 123Hello World which will properly output num=123,mystr='Hello World'. That, or you put a cin.ignore between the cin and getline so that it'll break into logical steps that you expect.
This is why you need the ignore command. Because you are thinking of it in logical steps rather than in a stream form so you run into a race condition.
Take another code example that is commonly found in schools:
int main()
{
int age;
string firstName;
string lastName;
cout << "First name: ";
cin >> firstName;
cout << "Last name: ";
cin >> lastName;
cout << "Age: ";
cin >> age;
cout << "Hello " << firstName << " " << lastName << "! You are " << age << " years old!" << endl;
}
The above seems to be in logical steps. First ask for first name, last name, then age. So if you did John enter, then Doe enter, then 19 enter, the application works each logic step. If you think of it in "streams" you can simply enter John Doe 19 on the "First name:" question and it would work as well and appear to skip the remaining questions. For the above to work in logical steps, you would need to ignore the remaining stream for each logical break in questions.
Just remember to think of your program input as it is reading from a "stream" and not in logical steps. Each time you call cin it is being read from a stream. This creates a rather buggy application if the user enters the wrong input. For example, if you entered a character where a cin >> double is expected, the application will produce a seemingly bizarre output.
Short answer
Why? Because there is still whitespace (carriage returns, tabs, spaces, newline) left in the input stream.
When? When you are using some function which does not on their own ignores the leading whitespaces. Cin by default ignores and removes the leading whitespace but getline does not ignore the leading whitespace on its own.
Now a detailed answer.
Everything you input in the console is read from the standard stream stdin. When you enter something, let's say 256 in your case and press enter, the contents of the stream become 256\n. Now cin picks up 256 and removes it from the stream and \n still remaining in the stream.
Now next when you enter your name, let's say Raddicus, the new contents of the stream is \nRaddicus.
Now here comes the catch.
When you try to read a line using getline, if not provided any delimiter as the third argument, getline by default reads till the newline character and removes the newline character from the stream.
So on calling new line, getline reads and discards \n from the stream and resulting in an empty string read in mystr which appears like getline is skipped (but it's not) because there was already an newline in the stream, getline will not prompt for input as it has already read what it was supposed to read.
Now, how does cin.ignore help here?
According to the ignore documentation extract from cplusplus.com-
istream& ignore (streamsize n = 1, int delim = EOF);
Extracts characters from the input sequence and discards them, until
either n characters have been extracted, or one compares equal to
delim.
The function also stops extracting characters if the end-of-file is
reached. If this is reached prematurely (before either extracting n
characters or finding delim), the function sets the eofbit flag.
So, cin.ignore(256, '\n');, ignores first 256 characters or all the character untill it encounters delimeter (here \n in your case), whichever comes first (here \n is the first character, so it ignores until \n is encountered).
Just for your reference, If you don't exactly know how many characters to skip and your sole purpose is to clear the stream to prepare for reading a string using getline or cin you should use cin.ignore(numeric_limits<streamsize>::max(),'\n').
Quick explanation: It ignores the characters equal to maximum size of stream or until a '\n' is encountered, whichever case happens first.
When you want to throw away a specific number of characters from the input stream manually.
A very common use case is using this to safely ignore newline characters since cin will sometimes leave newline characters that you will have to go over to get to the next line of input.
Long story short it gives you flexibility when handling stream input.
Ignore function is used to skip(discard/throw away) characters in the input stream. Ignore file is associated with the file istream.
Consider the function below
ex: cin.ignore(120,'/n');
the particular function skips the next 120 input character or to skip the characters until a newline character is read.
As pointed right by many other users. It's because there may be whitespace or a newline character.
Consider the following code, it removes all the duplicate characters from a given string.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
cin.ignore(); //Notice that this cin.ignore() is really crucial for any extra whitespace or newline character
while(t--){
vector<int> v(256,0);
string s;
getline(cin,s);
string s2;
for(int i=0;i<s.size();i++){
if (v[s[i]]) continue;
else{
s2.push_back(s[i]);
v[s[i]]++;
}
}
cout<<s2<<endl;
}
return 0;
}
So, You get the point that it will ignore those unwanted inputs and will get the job done.
It is better to use scanf(" %[^\n]",str) in c++ than cin.ignore() after cin>> statement.To do that first you have to include < cstdio > header.

Knowing initial user input after getline cstring assignment

Here's a C++ function of mine:
void SetUserName(char username[])
{
cout << "\nPlease enter a username.\n"
<< "Must not be longer than 12 characters.\n>> ";
cin.getline(username, MAX) // MAX is globally defined
while(strlen(username) > MAX)
{
cout << "\nUsername too long, try again.\n>> ";
cin.getline(username, MAX);
}
}
Obviously, the while loop never works because the user input is truncated to 12 characters everytime.
How can I effectively determine if the user input was too long, and continue to loop until the conditions are met?
Edit: Using cstring here is a requirement. I already know how easy it is with strings.
Edit #2: This was definitely a fruitful question for me, as it taught me a lot. Final code: http://pastie.org/3537894
C-style terminated strings are rather tricky to work with, and in almost every case I'd recommend the C++ std::string instead. However, since you say you specifically want to read a terminated string into an array, here is one way to do it.
Remember that the array size must be MAX+1, so there's space for MAX characters followed by the terminator.
istream::getline sets the streams failbit flag if the line is too long; you can test this after reading. If only a partial line was extracted, and you want to move on to the next line, then you'll need to clear the error state, and ignore the rest of the line.
while (!std::cin.getline(buffer, MAX+1)) {
std::cout << "Too long\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Use proper C++ (in particular, strings and the free getline function):
#include <string>
#include <iostream>
std::string line;
while (std::getline(std::cin, line))
{
if (line.length() > 12)
{
// error, line too long
continue;
}
// process line
}
If you want to find out if std::istream::getline() read an array full of characters as demanded but not an end of line character you need to figure out whether the number of stored characters (minus the terminating null) is identical to the extracted characters. That is, the following determines if there are more then 12 characters on the line (the 13th character is needed for the terminating null):
#include <iostream>
#include <string.h>
int main()
{
char array[13];
if (std::cin.getline(array, 13).gcount() == strlen(array)) {
std::cout << "excess characters on the line\n";
}
}
If you next also want to remove the excess characters from the stream you'd use something like std::cin.ignore(std::numeric_limits<std::streamsize>::max());. Since this is tagged as C, too, I don't know off-hand how to do this with C but I'm pretty sure that there is something similar to gcount().
Actually, looking more closely at the spec std::istream:getline() actually sets std::ios_base::failbit if it doesn't encounter a newline while reading the character (it also sets std::ios_base:failbit when no character is read but it doesn't set std::ios_base::failbit if at least one character is read before end of file is reached). This mean, you also want to clear the stream before ignoring excess characters and you can work off std::ios_base::failbit and std::ios_base::eof():
#include <iostream>
#include <limits>
#include <string.h>
int main()
{
char array[13];
std::cout << "enter password: ";
while (!std::cin.getline(array, 13) && !std::cin.eof())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "password is too long: enter max 12 chars: ";
}
std::cout << "the password is '" << array << "'\n";
}
Since std::ios_base::failbit is set you need to call clear() before you can use the stream for anything.
For the user to enter more characters than you allow, he must go at least one character over the limit. Since it does not matter to you by how many characters the user has "overstepped" your limit, you can pass MAX+1 as your limit, and see if the length is greater than MAX.
Of course you need to make enough space in the buffer to hold the 13-th character and a zero terminator. EDIT You also need to call ignore to skip to the end of the line on each failed attempt.