This question already has answers here:
How to read from std::cin until the end of the stream?
(2 answers)
How to break loop by Enter (c++)?
(3 answers)
End of File(EOF) of Standard input stream (stdin)
(6 answers)
Closed 5 years ago.
I was trying to take input variable number of strings and numbers.Found this solution link:
I tried this for numbers:
#include<iostream>
using namespace std;
int main(){
int np;
while (cin>>np){
cout << np<<endl;
}
return 0;
}
for strings:
#include<iostream>
#include<string>
using namespace std;
int main(){
string line;
while (getline(cin, line)){
cout << line <<endl;
}
return 0;
}
But,when I run the code,even if I just press enter it doesn't come out of the loop.The loop should terminate if just an enter key is pressed,but that does not happen.
Please provide suggestions how to achieve this functionality.
You can write
while (std::getline(std::cin, line) && !line.empty()) {
// ...
}
Only keep looping when the fetched string is nonempty.
Related
This question already has answers here:
C++: Why does space always terminate a string when read?
(4 answers)
Closed 2 years ago.
I wrote a code so that it removes everything(like spaces and other things) other than the alphabats using isalpha() function and converts it to lower case using tolower() function. It is working fine if i don't put a space in the string but if there is any space in the string then it go beyond the space. I dont understand why this is happening. This is the code i wrote.
#include<bits/stdc++.h>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
int i;
string A,b="";
cin>>A;
for(i=0;i<A.size();i++)
{
if(isalpha(A[i]))
b+= tolower(A[i]);
else
continue;
}
cout<<b;
}
Please help me.
Thankyou
The cin >> A; considers the space to terminate the input.
To get the whole line, use getline(cin, A);
cin reads the string till the first space it encounters, if your input string is "Hello World", then cin will only read "Hello".
You can use getline function to read a complete line.
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 4 years ago.
In this C++ code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string S;
getline(cin,S);
cout<<S;
return 0;
}
It prints the string with spaces, eg:
Input:
abc def
Output:
abc def
However, in this code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string S;
getline(cin,S);
cout<<S;
return 0;
}
It does not print any output, eg:
Input:
1
abc def
Output:
How is that even possible? Please help. Where am I going wrong?
You read a number and then a line. Think carefully about what is going on, a newline is not part of a number. So if the input is 123 newline, you read the number 123 and then getline reads the newline. Whatever is on the next line hasn't been read at all.
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 7 years ago.
I've read the answers to the "The Questions that may already have your answer" but it didn't help me.
I suppose my problem has a quick solution.
I want to write a program in which
The user inputs the name of the file they want to create.
then
In the same line they input an integer amount=the number of lines of the text of the newly created txt file.
In a new line the user input the first line of the text, in a new line the next line and so on.
And all those lines are saved to the created file.
For example file.txt 3
bhjudshsu 565 jdd
hcxjs
jdckisa jsdjs
And after opening the file named file.txt I should see the above three lines of the text.
Here is my code sample:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int amount;
string fileName, line;
ofstream start;
cin>>fileName>>amount;
ofstream out(fileName.c_str());
for(int i=0; i<amount; i++)
{
getline(cin, line);
start<<line<<endl;
}
start.flush();
start.close();
return 0;
}
The problem is that the file is created but it is empty.
Moreover, if I input file.txt 3 the program is closed after I input only two lines.
Could you tell me what I am doing wrong?
You have two ostreams, start and out. You attach out to the file (and write nothing to it), and write to start (which is attached to nothing).
In addition to Beta's answer, cin>> will leave newlines in the buffer when the user presses enter. getline() reads this as the user having pressed enter to "skip" the input.
You can use cin.ignore() to get rid of those extra characters before using getline().
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int amount;
string fileName, line;
cin >> fileName >> amount;
ofstream start(fileName.c_str());
for(int i = 0; i < amount; i++)
{
cin.ignore();
getline(cin, line);
start << line << endl;
}
start.flush();
start.close();
return 0;
}
This question already has answers here:
How to cin Space in c++?
(8 answers)
Closed 7 years ago.
i have a cpp code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string s,s1;
ofstream f("new.txt");
cin>>s;
f<<s;
f.close();
ifstream f1("new.txt");
while(!f1.eof())
{
f1>>s1;
cout<<s1;
}
return 0;
}
if i give the input string as "MAGICAL STRING WITH SPACES" . The ofstream object only writes "MAGICAL" into the textfile, what do i do to write and read whitespaces from string variables into an output file stream ?
This is because you use cin.
Try getline() instead!
string newArgument = "";
getline(cin, newArgument);
This question already has answers here:
tell cin to stop reading at newline
(6 answers)
equivalent of Console.ReadLine() in c++
(3 answers)
Closed 7 years ago.
I want to store "What is wrong with you?" in a string str in cpp but when I run this code it only stores "What" in ·str·.
How can I store the complete sentence in the string.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str;
}
Input:
What is wrong with you?
Output:
What
was already answered. But here is the solution:
std::string str;
std::getline( std::cin, str);