I want to take infinite number of strings using string class.

Why string operator is not working in the following program?

it is showing...
error: no match for 'operator!=' in 'str != -0x000000001'|

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
    vector<string> s_v;
    string str;

    for( ; ;)
    {
        getline(cin, str, '\n');
        if(str !=  EOF) s_v.push_back(str);
        else break;
    }


    return 0;
}

one more question, will

getline(cin, str, '\n');

this line will include '\n' at the end of each string?

it is showing...
error: no match for 'operator!=' in 'str != -0x000000001'|

That's because you're comparing a std::string with EOF. I suggest you change your loop to get rid of the EOF macro all together:

while (getline(cin, str)) 
{
     s_v.push_back(str);
}

Works better, look cleaner :)

one more question, will

getline(cin, str, '\n');

this line will include '\n' at the end of each string?

No. 'str' will contain the input upto, but not including the '\n'

Edited 6 Years Ago by Nick Evan: n/a

Comments
(N)Curses. Foiled again. Still a wannabe sig spammer lol.

EOF is not a string it's normally a negative integer. Use your getline function to drive a while loop. I would suggest the following though someone probably has a better solution which doesn't require the user to press enter twice (as it's just not clicking with me today).

while(getline(cin,str,'\n')) //getline defaults to '\n' anyway
	{
		if(str != "")
		  s_v.push_back(str);
		else
		   break;
	}

getline reads in the '\n' but subsequently gets rid of it, it doesn't end up in the string.

EDIT: Curses foiled by niek_e. I tried that solution initially but it just sits there once you are done...

Edited 6 Years Ago by jonsca: n/a

EDIT: Curses foiled by niek_e. I tried that solution initially but it just sits there once you are done...

Yes it probably will, but so will the original code :)

Yes it probably will, but so will the original code :)

:D thanks jonsca & niek_e

But what actually i was looking is for infinite number lines of infinite length string input.Input termination will occur only when EOF will be encountered.

Where, all the line will be terminated by '\n'...if it is both carriage and line feed...isn't this what happens at the end of every input line of ACM problem?

what is the difference between endl and '\n'?

isn't this what happens at the end of every input line of ACM problem?

I know you like to do the contests but I am not at all familiar with the intricacies of each one.

what is the difference between endl and '\n'?

See this thread. Basically endl forces a flush the output buffer (since it is flushed under certain circumstances anyway) and inserts a newline but '\n' just gives you a newline.

The problem is almost solved...but a little problem has appeared.

The following code is supposed to print three lines from "in.txt" file(which contains three lines).But it is printing the last line twice.The size method of vector is also showing one extra count of the total line amount.

s_v[3] and s_v[4] is showing the same result.where s_v[4] dont even exists in "in.txt"

How am i supposed to get ride of this problem?

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
    freopen("in.txt", "r", stdin);

    vector<string> s_v;
    string str;

    while(cin)
    {
        getline(cin, str);
        s_v.push_back(str);
    }

    for(int i=0; i<s_v.size(); i++)
    {
        cout << s_v[i] << endl;
    }

    return 0;
}

also, i wanna get ride of this line

freopen("in.txt", "r", stdin)

;

I used it when i coded in C.What should i use in C++?

But it is printing the last line twice.

while(cin)
    {
        getline(cin, str);
        s_v.push_back(str);
    }

The condition you have in the while() is not working. Instead you have to ..

while(getline(cin, str))
{
    s_v.push_back(str);
}

>> also, i wanna get ride of this line freopen("in.txt", "r", stdin) ;

I think you can just delete that line, your program will work without it. I.e. a command such as

yourprog.exe < input.txt

will work.

The condition you have in the while() is not working.

But why not? Maybe an explanation is in order so the error does not happen again...

while(cin)
    {
        getline(cin, str);
        s_v.push_back(str);
    }

In this code, the loop exits after cin is in an error condition. After reading the last line, there is no error.

You now attempt to read the 4th line, which causes an error. But you don't check the error. str still has line 3 in it because nothing was read this time. So you now push back line 3 again. cin has an error now and the loop exits, but the damage is done. It's simply a case of not testing for the error at the proper time.

Comments
"Why not" ... A good point there, kudos

You now attempt to read the 4th line, which causes an error. But you don't check the error. str still has line 3 in it because nothing was read this time. So you now push back line 3 again.

one things i dont understand is, while, cin encounters error(4th line), no push_back will take place, so, the last item in the vector, will be the third line, which was only once push backed while cin was true.

How, does another push back happens while cin is false.

But i have found an alternate,

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
    freopen("in.txt", "r", stdin);

    vector<string> s_v;
    string str;

    while(cin.good())
    {
        getline(cin, str);
        s_v.push_back(str);
    }

    for(unsigned int i=0; i<s_v.size(); i++)
    {
        cout << s_v[i] << endl;
    }

    return 0;
}

This works perfectly. :D

But im still wondering how the previous one didn't work, or what else i have to do to make it work.

I think you can just delete that line, your program will work without it. I.e. a command such as...

I was asking how to rewrite this code in C++.

I was asking how to rewrite this code in C++.

I see, since that specific line was included in the code you were having problems with, I thought that it was there as part of the program (unnecessarily so). But anyhow, I think you are after the following construct

ifstream ifs("some file here");
// redirect ...
streambuf * cin_rdbuf = cin.rdbuf(ifs.rdbuf());

// do something with cin ...

// then restore cin
cin.rdbuf(cin_rdbuf);

As to the loop problem, either use your own (workaround) solution or accept the commonly used one, choice is yours. Since you did not understand WaltP's explanation, maybe step through the original code with your debugger and keep an eye on cin 's state (hint: cin.eof() )

I was trying to solve ACM 10082 problem....here is the final code.

Problem Link:
http://online-judge.uva.es/p/v100/10082.html

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    ifstream file("in.txt");
    cin.rdbuf(file.rdbuf());

    string str, final_str;
    string data_str("`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./");

    while(cin.good())
    {
        getline(cin, str);
        for(unsigned int i=0; i<str.size(); i++)
            final_str += data_str[data_str.find(str[i]) - 1];

        cout << final_str << endl;
        final_str.clear();
    }
    return 0;
}

I have checked character by character...things look fine..but stilll they are not accepting the code...grrrr...

any idea, what might go wrong...except my eye... :(

Edited 6 Years Ago by iamcreasy: n/a

What is this ACM 10082 problem exactly. You might provide a link to it in case anyone is interested in having a look at it.

one things i dont understand is, while, cin encounters error(4th line), no push_back will take place,

Why not? If getline() returns an error, does the push back somehow just know there was an error and decide not to run? Is it linked to the getline() somehow I'm unaware?

so, the last item in the vector, will be the third line, which was only once push backed while cin was true.

But cin is TRUE after reading the 3rd line. It's only false after reading the 4th non-existent line -- which is immediately followed by the push back, error or not.

As for your "working" code, if you say so. It still looks like the same problem to me --

The IF status of cin is good
Attempt to read line -- error
Ignore error and continue with code anyway.

Whatever... Glad it works -- or appears to.

This article has been dead for over six months. Start a new discussion instead.