cin.getline( ) with larger size - c++

#include<iostream>
using namespace std;
int main()
{
char test[10];
char cont[10];
cin.getline(test,10);
cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}
When I input:
12345678901234567890
output is:
123456789
It seems cont is empty. Could someone explain it?

istream::getline sets the fail bit if the input is too long, and that prevents further input. Change your code to:
#include<iostream>
using namespace std;
int main()
{
char test[10];
char cont[10];
cin.getline(test,10);
cin.clear(); // add this
cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}

If the variable you read into isn't big enough to hold the entire line, the input operation fails and the stream will not read anything more until you call cin.clear().
You should use a std::string instead to hold the data. It will resize itself to match the input.
std::string test;
std::string cont;
getline(cin, test);
getline(cin, cont);

The standard says that you can get a "shorter" line than that you entered under the following conditions:
The C++ you use may not be confirming to the standard. - not possible.
You hit an EOF like character somewhere.
My guess would be to change the char[] to a string (STL) and then try it out. Also, when you say you input 12345678901234567890 in one go, all of it goes into test. And since test is only 10 bytes long, 123456789 would be output. Nothing is input into cont since the failbit is set for the istream class and further input is prevented. This code works for me with std::string.
#include<iostream>
#include <string>
using namespace std;
int main()
{
//char test[10];
//char cont[10];
string test;
string cont;
cin >> test;
cin >> cont;
//cin.getline(test,10);
//cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}

Copied from somewhere
cin.getline Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.
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.
I believe you pressed enter twice after entering 12345678901234567890

The getline() function has the following two syntaxes:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
s: Pointer to an array of characters where extracted characters are stored as a c-string.
n: Maximum number of characters to write to s (including the terminating null character).
delim: Explicit delimiting character
The return type of this function is istream object (*this).
In the above scenario, the data is read into the pointer to an array of character, test, which is converted at runtime and hence can store up to 50 characters as declared in the cin.getline(test, 50).
If you want to achieve your desired result kindly use n=10

Related

cin.getline() when input exceeds limit [duplicate]

#include<iostream>
using namespace std;
int main()
{
char test[10];
char cont[10];
cin.getline(test,10);
cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}
When I input:
12345678901234567890
output is:
123456789
It seems cont is empty. Could someone explain it?
istream::getline sets the fail bit if the input is too long, and that prevents further input. Change your code to:
#include<iostream>
using namespace std;
int main()
{
char test[10];
char cont[10];
cin.getline(test,10);
cin.clear(); // add this
cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}
If the variable you read into isn't big enough to hold the entire line, the input operation fails and the stream will not read anything more until you call cin.clear().
You should use a std::string instead to hold the data. It will resize itself to match the input.
std::string test;
std::string cont;
getline(cin, test);
getline(cin, cont);
The standard says that you can get a "shorter" line than that you entered under the following conditions:
The C++ you use may not be confirming to the standard. - not possible.
You hit an EOF like character somewhere.
My guess would be to change the char[] to a string (STL) and then try it out. Also, when you say you input 12345678901234567890 in one go, all of it goes into test. And since test is only 10 bytes long, 123456789 would be output. Nothing is input into cont since the failbit is set for the istream class and further input is prevented. This code works for me with std::string.
#include<iostream>
#include <string>
using namespace std;
int main()
{
//char test[10];
//char cont[10];
string test;
string cont;
cin >> test;
cin >> cont;
//cin.getline(test,10);
//cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}
Copied from somewhere
cin.getline Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.
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.
I believe you pressed enter twice after entering 12345678901234567890
The getline() function has the following two syntaxes:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
s: Pointer to an array of characters where extracted characters are stored as a c-string.
n: Maximum number of characters to write to s (including the terminating null character).
delim: Explicit delimiting character
The return type of this function is istream object (*this).
In the above scenario, the data is read into the pointer to an array of character, test, which is converted at runtime and hence can store up to 50 characters as declared in the cin.getline(test, 50).
If you want to achieve your desired result kindly use n=10

Strange things with c++ string input

#include <iostream>
#include <cstring>
using namespace std;
const int BUFFER_SIZE = 80;
void getstr(char* &str);
int main()
{
char* str;
while(true)
{
getstr(str);
if (!strlen(str))
break;
}
delete [] str;
return 0;
}
void getstr(char* &str)
{
char temp[BUFFER_SIZE];
cout<<"Enter a string(empty line to quit): ";
cin.get(temp, BUFFER_SIZE);
while(cin.get()!='\n')
continue;
str = new char [strlen(temp)+1];
strcpy(str, temp);
}
I have a string reading loop above and entering an empty line to terminate the loop doesn't work(after entering an empty line program stops responding to any input). But when I replace a loop in getstr with single cin.get() all works fine. What's wrong?
istream::get() sets failbit when empty string is read.
This makes cin.get() return EOF and this because you couldn't break the loop while(cin.get()!='\n').
You can use ios::clear() to clear failbit.
cin.get(temp, BUFFER_SIZE);
cin.clear(); // add this
while(cin.get()!='\n')
continue;
cin.get(char* s, size_t n) Extracts characters from the stream and stores them in s as a c-string, until either (n-1) characters have been extracted or the delimiting character is encountered: the delimiting character being either the newline character ('\n') or delim (if this argument is specified).
The delimiting character is not extracted from the input sequence if found and remains there as the next character to be extracted from the stream (see getline for an alternative that does discard the delimiting character).
A null character ('\0') is automatically appended to the written sequence if n is greater than zero, even if an empty string is extracted.
So here is the problem. cin.get() need to read at least 1 character. You can close stdin by pressing Ctrl+D and Enter, after that, your program will be finished.
And BTW, you are using new N times, but you have only 1 delete. You need to delete the previous buffer
If you are going to use C++, you should really use cin/cout in an objectively consistent manner. For example:
string name;
cout << "What is your name: ";
getline (cin, name);
cout << "Your name is: " << name;
What you are doing is kind of a C/C++ hybrid (char arrays in leu of string objects, but using std namespace).
Now, I know this isn't your question, but what you are doing right now is slightly unorthodox which makes answering your question a bit difficult without putting the code in to and editor and debugging it.
Given the c++11 tag, I assume that you really want C++ code. The great thing is that C++ simplifies this a lot.
#include <iostream>
#include <string>
int main()
{
std::string str;
while(std::getline(std::cin, str) && !str.empty())
{
// do stuff
}
return 0;
}

In c++ getline() is not reading all the letters of the string while accepting multiple strings one after the other

#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t; //Number of test cases
while(t--){
cin.ignore();
string s;
getline(cin,s);
cout<<s<<endl;
}
return 0;
}
Input:
2
AMbuj verma
Aaaa bBBB
Bm Chetan
Output:
AMbuj verma
aaa bBBB
m Chetan
The above program is not reading first character of the strings.
This is the output that I'm getting.
I have also used cin.ignore()
What you need to do is bring the cin.ignore() outside of your while loop since every time your loop works, it takes the first letter of your string.
cin>>t; //Number of test cases
cin.ignore();
while(t--){
string s,a;
getline(cin,s);
cout<<s<<endl;
}
Lastly why are you writing string s, a when there is no use of string a in your code.
You are calling cin.ignore(); in the loop so it will ignore one character in every iteration. Since you only use operator >> once you need to move the call to ignore out of the loop and have it just after the input.
cin>>t; //Number of test cases
cin.ignore(); // get rid of newline
while(t--){
string s,a;
getline(cin,s);
cout<<s<<endl;
}
Actually yes the cin.ignore() consumes your characters. Now to add something in detail the input is basically stored in buffer (not in case of redirection). Now when you use getline it does this.
(1) istream& getline (istream& is, string& str, char delim);
(2) istream& getline (istream& is, string& str);<--you used this
Extracts characters from is and stores them into str [getline(cin.str)]until the
delimitation character delim is found (or the newline character, '\n',
for (2)).
The extraction also stops if the end of file is reached in is or if
some other 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).
So when basically that \n is consumed and discarded by getline itself. Now you use cin.ignore()
istream& ignore (streamsize n = 1, int delim = EOF); Extract and
discard characters Extracts characters from the input sequence and
discards them, until either n characters have been extracted, or one
compares equal to delim.
So you didn't specify anything-- so it discards one character that is available in buffer which is the first character. (But you thought there would be the \n which will be consumed but it was not there as it was discarded by getline().
That's how you get the output like this.

C++ c_str() doesn't return complete string

I'm doing a C++ assignment that requires taking user input of an expression (eg: 2 * (6-1) + 2 ) and outputting the result. Everything works correctly unless a space is encountered in the user input.
It is a requirement to pass the user input to the following method;
double Calculate(char* expr);
I'm aware the issue is caused by c_str() where the space characters act as a terminating null byte, though I'm not sure how to overcome this problem.
Ideally I'd like to preserve the space characters but I'd settle for simply removing them, as a space serves no purpose in the expression. I get the same result when using string::data instead of c_str.
int main(int argc, char **argv)
{
string inputExpr;
Calc myCalc;
while(true) {
cin >> inputExpr;
if(inputExpr == "q") break;
cout << "You wrote:" << (char*)inputExpr.c_str() << endl; // debug
printf("Result: %.3f \n\n", myCalc.Calculate( (char*)temp.c_str() ) );
}
return 0;
}
c_str works just fine. Your problem is cin >> inputExpr. The >> operator only reads until the next space, so you do not read your equation fully.
What you want to use is std::getline:
std::getline (std::cin,inputExpression);
which will read until it reaches a newline character. See the function description if you need a specific delimiter.
Problem is not with inputExpr.c_str() and c_str as such, c_str() returns pointer to a character array that contains a null-terminated sequence. While reading through cin, you get space or tab etc separating as multiple strings. Check with the content of the string that way to solve the intended operation
First, I think your Calculate() method should take as input a const char* string, since expr should be an input (read-only) parameter:
double Calculate(const char* expr);
Note that if you use const char*, you can simply call std::string::c_str() without any ugly cast to remove const-ness.
And, since this is C++ and not C, using std::string would be nice:
double Calculate(const std::string& expr);
On the particular issue of reading also whitespaces, this is not a problem of terminating NUL byte: a space is not a NUL.
You should just change the way you read the string, using std::getline() instead of simple std::cin >> overload:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line;
getline(cin, line);
cout << "'" << line << "'" << endl;
}
If you compile and run this code, and enter something like Hello World, you get the whole string as output (including the space separating the two words).

String.size() returns incorrect number if there is space in the string

I'm trying to write a program that returns the number of characters in a string. As I was writing my program, I've noticed that there's a bug in the string class.
Say my program is this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout << "Input string: ";
cin >> input
cout << "Number of characters: " << input.size() << endl;
return 0;
}
If my input is Test String, I should see the number 11 as the output.
However, the output I get is this:
Number of characters: 4
It seems like the size() method does not work when there is space in the string.
My question is, is there another way to get the number of characters in a string? I tried length() method but the result was the same.
That's because your
cin >> input;
only reads up to the first whitespace character.
If you want to get the a whole line, use the following code:
std::string s;
std::getline(std::cin, s);
This is not a bug, and more particularly, actually has nothing to do with the string class.
It has to do with the istream class (cin). cin's operator>> performs "formatted input," which is to say, input delimited by whitespace. After you hit enter, you read out "Test" into a string, leaving "String" in the input buffer. "Test" is, in fact, four characters long.
Consider using std::getline or istream::getline to read entire lines of input with more control. Be sure to read the documentation for these methods carefully, as they have different behavior with respect to what is left in the input stream which can then cause results you may not expect if mixed together with oeprator>> usage.
This is a result of the meaning of cin >> input, which stops reading when any whitespace is found. If you want to keep reading until the end of a line, try getline.
After taking input correctly, you can get the length of the string or char pointer(char*)(including whitespaces) by using strlen(string_name), this will return the length.