-4

I am having memory trouble with my code, and figured out that my code was being read wrong. For example the last value is adding numbers, not sure why. Also the names aren't coming out right.

This is what the output is looking like:

4101,BRAEBURN02.07682e-3172.07691e-317
4021,DELICIOUS02.07682e-3172.07691e-317
4020,DELICIOUS02.07682e-3172.07691e-317
4015,DELICIOUS02.07682e-3172.07691e-317
4016,DELICIOUS02.07682e-3172.07691e-317
4167,DELICIOUS02.07682e-3172.07691e-317
4124,EMPIRE,1,1.14,145.202.07682e-3172.07691e-317
4129,FUJI02.07682e-3172.07691e-317
4131,FUJI02.07682e-3172.07691e-317

As you can see the Empire was separated properly with the exception of the the last value.

csv file

Here's my code: the cout part was just for my personal use to see if the values were being inputted properly.

int main()
{
    string name;
    double price;
    int by_weight;
    double inventory;
    int plu_code;
    ifstream infile;
    infile.open("inventory.csv"); 

    while(!infile.eof())
    {    
        stringstream ss;
        string line = "";
        getline(infile,line);

        Tokenizer tok(line, ",");
        ss << line;

        ss >> plu_code >> name >> by_weight >> price >>inventory;
        cout << plu_code<<"" <<name<<"" << by_weight<<"" << price <<""<<inventory<<"\n";
        table[plu_code] = new Product(plu_code, name,by_weight, price,inventory);
        numProducts++;
    }

    return 0;
}
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
user2644360
  • 71
  • 1
  • 2
  • 9
  • It's not clear from your data sample how it relates to the table that you've posted. Please can you edit the data into a more readable form? –  Aug 12 '13 at 03:49
  • The data sample and data table are not the same. For example, the DELICIOUS types are different. There are strange scientific notation numbers in the data sample, possibly a machine minimum float value. It is not CSV format data corresponding to the table beneath it. Could you provide matching data? – Paul Aug 12 '13 at 03:52
  • I know that is what my cout is outputting, it is ending data at the spaces for the words and turning my a last value to some type of floating point. The purpose of the data sample was to show how my output was coming out – user2644360 Aug 12 '13 at 04:00
  • possible duplicate of [How can I read and manipulate CSV file data in C++?](http://stackoverflow.com/questions/415515/how-can-i-read-and-manipulate-csv-file-data-in-c) – Paul Aug 12 '13 at 04:07

1 Answers1

0

The Empire line works because it's the only one whose name contains no whitespace. When you read strings from a stream, they are delimited by whitespace, so you only get the first word. If there are more words after that, then reading a double or other numeric type will fail because the stream is still pointing at non-numeric characters. You are not testing that your input operations succeeded, but it should have been obvious from your output.

I'm not sure what effect your Tokeniser class is supposed to have here. But perhaps have a look at this SO question for tokenising commas out of the stream. You can use a single getline call with a comma delimiter to read the name, and then normal << operator for the others.

[Edit] In fact, after cleaning up your question layout I notice that the Empire line doesn't work. It's reading the rest of the line as the name, and then still outputting uninitialised values. Which suggests to me your Tokeniser doesn't do anything at all.

Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103
  • Okay so why does the last value of Empire still come out as a floating point instead of how it is given – user2644360 Aug 12 '13 at 04:21
  • @user2644360 It doesn't. The name is read in as `,EMPIRE,1,1.14,145.2` and the remaining values are the same uninitialised values as in every other case. Because you don't output anything between your fields, you are having trouble seeing what's going on. Try outputting a character like `#` in between and you'll see. Currently you're using `""` which is an empty string. Once you can see this, you can then try to take the advice I gave you in my answer. – paddy Aug 12 '13 at 04:25