I am trying to output a string that I have previously read using cin>>input until EOF. The problem that I am facing is that after I output the string there is no new line when asking for the next string. I already tried using getline(), but the problem was that it was not skipping whitespaces. Any clues ?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
while (cin>>input)
{
cout<<input;
}
return 0;
}
Here is an example :
Input:test test
Output: test test
Input:test test... (Previous input remains in stdin)
Sorry in advance if this is a duplicate, but I could not find anything similar.
Related
I've been doing programming challenges on coderbyte and while doing one, ran into an issue. I want to isolate a word from a string, do some checks on it and then move to another word. The code I'm going to post is supposed to take only the first word and print it out on the screen. When I run it, it doesn't print anything. I thought that maybe I did something wrong in the while loop so I did a simple test. Let's say my input is "This is a test sentence" and instead of word (in cout), I type word[0]. Then it prints "T" just fine. Can you find what the problem is?
#include <iostream>
#include <string>
using namespace std;
int Letters(string str) {
int i=0;
int len=str.length();
string word;
while(i<len){
if(isspace(str[i])){word[i]='\0'; break;}
word[i]=str[i];
i++;
}
cout<<word;
return 0;
}
int main() {
int test;
string str;
getline(cin, str);
test=Letters(str);
return 0;
}
string word;
is default constructed, which is empty initially. Inside while loop, you tried to do:
word[i] = str[i];
It means you tried to access memory that has not been allocated,resulting in undefined behavior.
Try:
word.append(str[i]);
You can use simpler way to get words from input in C++. It will help you to avoid errors in the future.
#include <iostream>
using namespace std;
int main()
{
string word;
while(cin >> word)
{
// "word" contains one word of input each time loop loops
cout << word << endl;
}
return 0;
}
So this is a fairly simple example of a program where I'm trying to output the first two lines of an input text file. The ifstream should be a global variable, and the testGetFile() function is necessary (I have not done the actual text processing needed in this code.) I'm trying to figure out why this is cout-ing only the SECOND line of the input file. Any help will be appreciated!
Thanks in advance!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
ifstream input;
string testGetFile(){
string result;
getline(input,result);
return result;
}
int main(){
input.open("testInput.txt");
cout<< testGetFile();
cout<< testGetFile();
return 0;
}
So I am trying to read from a file named input_file till it reaches the end. I've tried using while (!input_file.eof()) but it goes on for an infinite loop. I looked around on the forum and tried using while (getline(input_file, line)) but that just returns an empty line. I'm not using both getline() and the >> operator like other questions were.
How do I get around this? Here's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Main program
void CalfFlac(ifstream& input_file) {
string text;
string line;
while (getline(input_file, line)) {
text += line;
}
cout << text << endl;
}
int main() {
ifstream input_file;
input_file.open("calfflac.in");
CalfFlac(input_file);
}
input_file contains a single line Confucius say: Madam, I'm Adam. followed by a carriage return.
Thanks for the help!
PS: I'd prefer if the solution remained simple, as this appears to be a pretty simple problem.
EDIT: Make sure you have the right file name! I tried a bogus file name and it printed a blank line as your describe.
I tried your code with a correct file name and it worked for me
In main add this line:
int main() {
ifstream input_file;
input_file.open("calfflac.in");
if(!input_file)
cout << "File path is wrong!";
CalfFlac(input_file);
}
I wanted write a program which allows users to write some random stuff, but i got an
error saying no matching call to which I am not able to figure it out. please Help me.
while you are trying to answer to this question, try to be more noob specific.
here is my code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string story;
ofstream theFile;
theFile.open("Random.txt");
while(cin.get(story,5000)!=EOF)
{
theFile<< story;
}
return 0;
}
cin.get with 2 arguments expects char* as first argument and you are trying to pass string as the first argument.
If you want to read std::string instead of C-string until the end of line use getline(cin, story)
If you want to read string until the next space or newline or another blank symbol use cin >> story;
You seem to be trying to write the content of cin to a file. You could just use stream operators:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string story;
ofstream theFile;
theFile.open("Random.txt");
if(cin >> story)
{
theFile << story.substr(0, 5000);
}
return 0;
}
I am assuming that you only want the first 5000 characters in Random.txt...
I have a script which takes a word from input file and outputs customizated word in output file.
For example, in input file I have word ccccccaaaaaannnnndlllllleeeeeand I need to edit the word it to look like candle and output in other file. So could you please give me a example of it?
So far I have:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string vards;
std::ifstream input_file("virkne3.in");
input_file >> vards;
std::ofstream output_file("virkne3.out");
output_file << vards.erase(std::unique(vards.begin(), vards.end()), vards.end());
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
Assuming you mean consecutive repeated letters, std::unique does exactly that.
vards.erase(std::unique(vards.begin(), vards.end()), vards.end());