By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,482 Members | 1,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,482 IT Pros & Developers. It's quick & easy.

How to input?

P: n/a
i have a file which contains many words as a list.. like this:

Midhun
wonderful
excellent
handsome
interesting

how can i initialise each of these words into a string? I'd like to use
a vector of strings to do few operations on these words.

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> how can i initialise each of these words into a string? I'd like to use
a vector of strings to do few operations on these words.


std::ifstream
std::string
std::vector
std::getline

regards,
ben
Jul 23 '05 #2

P: n/a
here's what i tried.. it is giving me error.. I'm a novice programmer..
any help with the getline?

#include <iostream>
#include <fstream>
#include <vector>
#include <String>
using namespace std;
void main(){
string rhyme_line;
vector <string> rhymefact;
ifstream database("base.rfy");
cout<<"Database is loading...\n";
while (database){
database.getline(rhyme_line,'\n');
if (rhyme_line=="STOPSTOP")
break;
rhymefact.push_back(rhyme_line);
}
system("cls");
cout<<"Database loaded\n";
/* i've to write more codes here once the above part is done correctly
*/
}

the file name is base.rfy and the delimiter of words is '\n'....

Jul 23 '05 #3

P: n/a
Midhun K Menon wrote:
here's what i tried.. it is giving me error.. I'm a novice programmer..
any help with the getline?

#include <iostream>
#include <fstream>
#include <vector>
#include <String>
Make this

#include <string>

with a lowercase 's'.
using namespace std;
Not recommended. At least, put it in function scope.
void main(){
main() returns an int, always.

int main()
{
using namespace std;
string rhyme_line;
vector <string> rhymefact;
ifstream database("base.rfy");
You should check whether the file was correctly opened:

if (!database)
// error..
cout<<"Database is loading...\n";
while (database){
database.getline(rhyme_line,'\n');
std::istream::getline() works on c-style strings, not on std::string.
You should use std::getline():

getline(database, rhyme_line);

and in this case, the delimiter is redundant.
if (rhyme_line=="STOPSTOP")
break;
rhymefact.push_back(rhyme_line);
}
system("cls");
Non portable. On my system, "cls" is the command to reboot the
computer, not to clear the screen. Hopefully I didn't run your program.
cout<<"Database loaded\n";
/* i've to write more codes here once the above part is done correctly
*/
}

the file name is base.rfy and the delimiter of words is '\n'....

Jonathan

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.