This shouldn't be this hard.
You want to read from a text file of the format :
Firstname;Lastname;178
where the ';' means the word has ended. And you know you will
always read firstname, then lastname, then the value. Now first
you need to open a file. Of course you know how to do that :
ifstream getFromFile("text.txt");
That opens a file to read. Now you have to read.
First lets say this is the file :
Firstname;Lastname;000
Tony;Montana;100;
Best;Forever;99;
Richard;Jones;98;
World;Hello;97;
You now need to read the file. What you can do is read 1 line into a string. Then have a function find all occurrence of ';' and replace it with a space.
Here is a complete example :
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
//find wherever ';' is and replace it with ' '
void wordReplace(string& source,char find = ';', char replaceWith = ' ')
{
size_t pos = 0;
while(true)
{
pos = source.find_first_of(find,pos); //find the position of ';'
if(pos == string::npos)
break;
source[pos] = replaceWith; //replace ';' with a space
}
}
int main()
{
ifstream iFile("test.txt"); //open file to read
if(!iFile) return -1; //check if file is opened
string word = "";
vector<string> fullNameWithValue;
while( getline(iFile,word)) { //get a line into a string
wordReplace(word); //replace ';' with ' '
fullNameWithValue.push_back(word); //add word to out list
}
for(size_t i = 0; i < fullNameWithValue.size(); i++) //print out word
cout<<fullNameWithValue[i]<<endl;
return 0; …