i am doing a task where i am inputting ist no of times you want to have input and then i am inputting a full line by the use of getline but sime how i am not able to do that.
here when i try to input '1' as my no. of test cases it instantly gives '0' as answer and out of the loop why is not this cin>> test; here compatible with getline (cin,s);
#include<iostream>
#include<string>
using namespace std;
int main()
{
int test,j;
cin>>test;
int len;
string s;
for(j=1;j<=test;j++)
{
getline( cin,s);
len = s.length();
cout<<len;
}
return 0;
}
the difficulty
input :
1
output:
0
expected
input:
1
Hello World
output
11
Well I mixed a little C to get My answer
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
int test,j;
scanf("%d\n",&test);
int len;
string s;
for(j=1;j<=test;j++)
{
getline( cin,s);
len = s.length();
cout<<len;
}
return 0;
}
You are mixing formatted input and unformatted input functions.
operator>> leaves the trailing newline after the number you extracted in the input buffer, but this isn't a problem for subsequent reads done with operator>> because it skips all the whitespace it finds before reading. On the other hand, getline reads until it finds the delimiter (\n by default) without skipping anything.
Long story short: if you want to use getline you have to clean the buffer from the \n after your cin>>test;:
#include <limits>
// ...
cin>>test;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for(j=1;j<=test;j++)
{
getline( cin,s);
len = s.length();
cout<<len;
}
This is reading in the int and the string with the one carridge return.
It reads 1 as the total and then nothing as the string (hence the length of 0).
Try 1TEST and this should give you a length of 4.
I believe your problem is you are giving input stream as a parameter where you should give buffer.
Try this.
{
char szInput[256];
cin.getline( szInput,256);
len = strlen(szInput);
cout<<len;
}
Related
#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
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 6 years ago.
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
getline(cin,s);
cout<<s.at(0);
}
return 0;
}
I tried to run this code but it says out_of_range error. This code is not even taking in the string input.
You are mixing line-based input with non-line-based input.
int main()
{
int t;
cin>>t;
When the user enters "123" and then presses the enter key, the "123" part of the input is parsed and ends up as the number 123 in t, but the linebreak resulting from the enter key will remain in the input buffer.
That's non-line-based input.
while(t--) {
string s;
getline(cin,s);
The std::getline function reads everything until a linebreak is encountered. The linebreak from above is still there, so a linebreak is encountered immediately and s remains empty.
That's line-based input.
std::getline also consumes the linebreak it has encountered, but this doesn't help you much anymore:
cout<<s.at(0);
s has size 0 and at(0) tries to access the 1st element. at is required to throw std::out_of_range_error when you try to access a non-existing element.
A good solution would be to switch exclusively to line-based input on the top input layer. Read everything as lines and parse the individual lines as required. Use std::stoi to convert a string to an integer number.
Here is an example:
#include <iostream>
#include <string>
int main()
{
std::string input;
std::getline(std::cin, input);
int t = std::stoi(input);
while(t--)
{
std::string s;
getline(std::cin, s);
std::cout << s.at(0);
}
}
Note that you should consider adding more error handling to your code. What happens when the user enters a negative number?
You can use cin.sync() function to clear extra input buffer.
The code like this:
int main()
{
int t;
cin >> t;
cin.sync(); // add this line
while(t--)
{
string s;
getline(cin, s);
cout<<s.at(0);
}
return 0;
}
The original code to make the string that is empty and you're trying to access the first element. So it says out_of_range error.
I am a beginner in c++ and I want to enter a string as character by character into an array , so that I can implement a reverse function .. However unlike C when the enter is hit a '\n' is not insterted in the stream.. how can I stop data from being entered ?
my code is :
#include<iostream>
#include<array>
#define SIZE 100
using namespace std;
char *reverse(char *s)
{
array<char, SIZE>b;
int c=0;
for(int i =(SIZE-1);i>=0;i--){
b[i] = s[c];
c++;
}
return s;
}
int main()
{
cout<<"Please insert a string"<<endl;
char a[SIZE];
int i=0;
do{
cin>>a[i];
i++;
}while(a[i-1]!= '\0');
reverse(a);
return 0;
}
When you read character by character, it really reads characters, and newline is considered a white-space character.
Also the array will never be terminated as a C-style string, that's not how reading characters work. That means your loop condition is wrong.
To begin with I suggest you start using std::string for your strings. You can still read character by character. To continue you need to actually check what characters you read, and end reading once you read a newline.
Lastly, your reverse function does not work. First of all the loop itself is wrong, secondly you return the pointer to the original string, not the "reversed" array.
To help you with the reading it could be done something like
std::string str;
while (true)
{
char ch;
std::cin >> ch;
if (ch == '\n')
{
break; // End loop
}
str += ch; // Append character to string
}
Do note that not much of this is really needed as shown in the answer by Stack Danny. Even my code above could be simplified while still reading one character at a time.
Since you tagged your question as C++ (and not C) why not actually solve it with the modern C++ headers (that do exactly what you want, are tested, save and work really fast (rather than own functions))?
#include <string>
#include <algorithm>
#include <iostream>
int main(){
std::string str;
std::cout << "Enter a string: ";
std::getline(std::cin, str);
std::reverse(str.begin(), str.end());
std::cout << str << std::endl;
return 0;
}
output:
Enter a string: Hello Test 4321
1234 tseT olleH
Why I can't take inputs of a string using gets,getline and cin.getline.when I debugg it seems that compiler skips those lines.here's my code-
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1,s2;
char *p;
int n,m,i;
cin>>n;
for(i=1;i<=n;i++)
{
int j=0;
getline (cin,s1);
getline (cin,s2);
cout<<s1<<"\n";
while(s1[j]!='\0')
{
if(s1[j]==' ')
{
s1.erase(s1[j]);
}
j++;
}
}
cout<<s1<<S2<<endl;
return 0;
}
What about j variable, it isn't set to zero when next for-loop iteration begin, so in second iteration you work with garbage.
Every time you use cin, it stores every character entered in memory until it encounters a newline character. This block of memory is called the input buffer.
when you take the input for 'n' the return key is in the cin buffer.
You should use cin.ignore to get rid of this newline.
Before getline (cin,s1); add cin.ignore statement
#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