0
int a, b;
while (infile >> a >> b)
{
    // process pair (a,b)
}

So this is the code i've been watching but i ran into a problem because my strings doesn't have whitespaces between them, they have ";"
My code:

void load(string filename){ // [LOAD]
    string line;
    ifstream myfile(filename);
    string thename;
    string thenumber;

    if (myfile.is_open())
    {
        while (myfile >> thename >> thenumber)
        {
        cout << thename << thenumber << endl;

        //map_name.insert(make_pair(thename,thenumber));

        }
        myfile.close();
    }
   else cout << "Unable to open file";
}

[Inside the txt.file]

123;peter
789;oskar
456;jon

What i get right now is "thename" as 123;peter and "thenumber" as 789;oskar. I want "thename" as peter and "thenumber" as 123 so i can then insert it back into my map correctly, How?

Widdin
  • 47
  • 1
  • 4
  • 2
    Is you text file `123;peter` or `peter;123`? Your code says it should be `peter;123` but you say your text files is `123;peter` – NathanOliver Nov 19 '15 at 18:39
  • @Widdin In case you have `123;peter`, you can extract `123` to an integer type, then `myfile.get()`, then `myfile >> thename`. If it's the other way around, use `std::getline` with `;` as a delimiter. – LogicStuff Nov 19 '15 at 18:41
  • The issue with my while-loop is that the string thename gets the value "123;peter" and thenumber gets "789;oskar", its supposed to be separated so the thename gets peter and thenumber 123 for each line so i can then insert it back into my map correctly, as insert(name,number) – Widdin Nov 19 '15 at 18:41
  • 1
    you should try [changing your delimiter](http://stackoverflow.com/questions/10376199/how-can-i-use-non-default-delimiters-when-reading-a-text-file-with-stdfstream) – R Nar Nov 19 '15 at 18:42
  • But then i need like another 10 rows of code >.>, isnt there a more simple way as shown above? – Widdin Nov 19 '15 at 18:47

3 Answers3

1

The infile >> a read from infile the eligible type for a. In your case a is int so '>>' expect to find an int. In your code myfile >> thename >> thenumber both are string type so they expect string type in your file. The problem is that string include ';' so the variable name will take all the row until it find \n(new line).

in your code

std::string thename, thenumber; char delimeter(';'); //It is always '-' is it? std::getline(std::cin, thename, delimeter); std::getline(std::cin, thenumber);

also thenumber will be string type. To convert your thenumber into int:

std::istringstream ss(thenumber);
int i;
ss >> i;
if (ss.fail())
{
    // Error
}
else
{
    std::cout << "The integer value is: " << i;
}
return 0;
MSD561
  • 512
  • 5
  • 16
  • The map is a so don't worry about converting or something like that, just save the numbers in the string – Widdin Nov 19 '15 at 18:50
0

You have to input a single string and then split it to get the name and number

....

#include <string>
#include <sstream>
#include <vector>

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

....

void load(string filename){ 

..........


    if (myfile.is_open())
    {
        while (myfile >>whole)
        {
             std::vector<std::string> parts = split(whole, ';');
             name = parts[0];
             number = parts[1];
        }
    }
Sumsuddin Shojib
  • 3,583
  • 3
  • 26
  • 45
0

It is fairly simple to read in a file in the format. You can use std::getline with a different delimiter to tell it where to stop reading the input.

while(getline(myfile, thenumber, ';')) // reads until ';' or end of file
{
    getline(myfile, thename); // reads until newline or end of file
    map_name.insert(make_pair(thename,thenumber));
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402