0

I'm new to this community.

I got a program which get info from a .txt file then store it into a array for future use.However,when i try to get line from file with cities name,it didn't work that well.

Sameple for the line:

8175133 New York, New York, United States
7871900 Taipei, Taiwan
7785965 Kinshasa, Democratic Republic of the Congo

the point here is to get the weight(which are those long int) and the name of the cities(include the Country name .etc) then store them into a class named "Term" which has two variables "weight" and "query".Then store the "Term" class in a list.

My problem is,how can i do that?(get info from the file) The way i did it was to get whole line (such as "7871900 Taipei, Taiwan")then try to use "(getline(iss,get_line,' ')" but it didn't work and give me "0" for most of the lines.

  Autocomplete Search_SL(10),Result_SL(k+1);
    while (!in_file.eof())
    {
        string weight;
        string query_LS;
        long weight_LS;
        string name;
        getline(in_file,name);
        istringstream iss(name);
        for (int i = 0;i < 10;i++)
        {
            if (getline(iss,get_line,' '))
            {
                weight = get_line;
                if (i == 1)
                {
                    weight_LS = atoi(weight.c_str());
                    cout<< weight_LS<<endl;
                }
                else
                {
                    query_LS += weight;
                }
            }
        }
        if (query_LS != "")
        {    
            Term input_que(query_LS,weight_LS);
            Search_SL.insert(input_que);
            last++;
        }    
    }    

Let me know if you guys have any questions on my question! :P Thank you!

PS: Here's a sample output(correct one):

Please input the search query(type "exit" to quit): 
Chic↵↵ 

 1. 2695598 Chicago, Illinois, United States 
 2. 577375 Chiclayo, Peru
 3. 86187 Chico, California, United States 

I'm sure the rest of my program are right coz when i use other txt file which has line like "

 1. 5627187200  the 
 2. 509184100   at

it will works

  • 1
    Do `while(getline(in_file, buffer)) {...}` where `buffer` is a `std::string` which stores the contents of each line in the file. Parse each line around the comma and you're good to go – sjrowlinson Apr 27 '16 at 19:57
  • Thank you! But that's not really what i need in this case,my problems is how to divide the "weight" and the "name" into two different variables for each line i get from the file. – Simon Tang Apr 27 '16 at 20:02
  • Is each weight and query in the file on separate lines? – Biruk Abebe Apr 27 '16 at 20:06
  • no,one weight for one city,and one weight with one name makes one line,sorry for the confusion – Simon Tang Apr 27 '16 at 20:09
  • 2
    You probably don't need to parse the integer value for `weight` since you can just use a formatted input provided by `operator>>` – Biruk Abebe Apr 27 '16 at 20:48
  • Well,i did used >> but it didn't work,that's why i try a different approach – Simon Tang Apr 27 '16 at 22:05

4 Answers4

1

You can use the extraction operator>> of stringstream,which use a space as an input delimiter, to read the formatted long int value for the weight(no need to read this as string and convert to int) and you can then use getline to read the rest the line from the stringstream.

string query_LS;
long weight_LS;
string name;
while (in_file.peek() != EOF)
{
    getline(in_file,name);//read a line from the file
    istringstream iss(name);

    query_LS="";
    iss>>weight_LS;//read a formatted long integer,reads upto the space
    getline(iss,query_LS);//read the rest of the string     

    if (query_LS != "")
    {    
        Term input_que(query_LS,weight_LS);
        Search_SL.insert(input_que);
        last++;
    } 
}   
Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
0

Here is the outline of a potential solution, untested so it may need some tweaking.

std::string buffer = "";
while(getline(in_file, buffer)) {
    // parse up to the first space (by checking characters in buffer)
    size_t count1 = 0;
    while (buffer.at(count1) != ' ' && count1 < buffer.size()) {
        ++count1;
    }
    // get substring of buffer up to first space occurrence, and convert to 
    // integral type to store in long variable
    long weight = atoi(buffer.substr(0,count1).c_str());

    // do similar parsing around the commas to get the city/country data...

}
sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
0

Maybe the one immediate problem with your code is?

if (i == 1)    ==>   if (i == 0)  

But I like to reuse what we have from the std::, specially in simple cases.

This is my "I too..." (untested):

struct Term {std::string query; long weight;};
... 
Term input{};
while(in_file>>input.weight && std::getline(in_file, input.query))
    if (!input.query.empty())
        Search_SL.insert(input);
qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
-1

After you get the line, try iterating through the istringstream elements (example taken from here):

istringstream iss("2.832 1.3067 nana 1.678");
double num = 0;
while(iss >> num || !iss.eof()) {
    if(iss.fail()) {
        iss.clear();
        string dummy;
        iss >> dummy;
        continue;
    }
    cout << num << endl;
}

This will only return the numbers in the stringstream.

EDIT: Code to do this the other way around, i.e. get the strings instead of the numbers:

#include <sstream>
#include <iostream>

using namespace std;

int main(){

    istringstream iss("2.832 mike 1.3067 nana 1.678");
    double num = 0;
    while(iss >> num  || !iss.eof()) {
        if(iss.fail()) {
            iss.clear();
            string dummy;
            iss >> dummy;
            cout << dummy << endl;
            continue;
        }
    }
}
Community
  • 1
  • 1
Nikos Kazazakis
  • 792
  • 5
  • 19
  • But it wont delete the numbers right?It works but it didn't help with my question! :3 Thank you though – Simon Tang Apr 27 '16 at 22:13
  • ah sorry, i read that the other way around. Just swap double and string in the code above and it should do the trick, I'll update the answer – Nikos Kazazakis Apr 27 '16 at 22:16
  • The second one didn't work,from my test,it would return all the stuff one by one whatever it's a string or long int :( – Simon Tang Apr 27 '16 at 22:38
  • You're right, that's what I get for not compiling it haha. I updated the edit, it should work fine now :) – Nikos Kazazakis Apr 27 '16 at 22:49
  • your solution worked ! :) but my program still has problem,i guess that's my work there.However,it has been an honer to discuss this problem with you! Thank you! – Simon Tang Apr 27 '16 at 23:10