file input of mixed types

I am writing a program where I need to handle an inventory file of mixed types integers and strings. An example of my data file would be as follows:

55555 38 12 Paint Brushes

The first 5 digit number is the item number, next is quantity on hand, then minimum reorder number, and finally an item description.

This is a beginner to intermediate course, so please keep that in mind with the help. I will also post what I have tried, which is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int main()
{
    string line;
    ifstream infile;
    
    string itemNum = "";
    string quantity = "";
    string reorderNum = "";
    string description = "";
    int strLength;
    int checker = 0;
    
    infile.open("inventorytest.data");
    
    getline(infile, line);
    
    while(infile)
    {       
       strLength = line.length();
       cout << line << endl;
       for(int i=0; i<line.length(); i++)
       {
           if(line[i] == " ")
             checker++;
           if(checker == 0 && line[i] != " ")
             itemNum = itemNum + line[i];
           if(checker == 1 && line[i] != " ")
             quantity = quantity + line[i];
           if(checker == 2 && line[i] != " ")
             reorderNum = reorderNum + line[i];
           if(checker >= 3 && line[i] != " ")
             description = description + line[i];
        }
       getline(infile, line);
    }


I know that even if this were to have worked, and it didnt, I get a c++ forbids comparison between pointer and integer error, that this would be terribly inefficient. Basically, once I use getline, I do not know how to split up the data on the line into different variable types that are seperated by spaces especially with the description being multiple words and the whole line seperated only by spaces. Thanks a lot for any help.
What have you learned so far? Have you learned about streams?
I have learned about ifstream and ofstream to handle input and output to files. Not sure if there is more to it than that. To be honest I missed some class due to my girlfriend getting into a car accident. She is around 7 months pregnant with our first boy. I am a bit behind the class, but I searched through the lecture notes and have not found anything to help thus far.
I feel for you. Use stringstreams.
Last edited on
We definately have not covered vectors yet. I am not sure what an unsigned variable type is. We have basically covered input output of string, int, float, double, we have covered structs, so I am good there. I am not sure what #include<sstream> does or iterator for that matter. I do appreciate you taking your time and I realize it may not be easy to think backwards so to speak and come up with a dumbed down answer, but I just do not follow exactly what you have submitted. We start learning about classes next week.
Simplified:
Vectors are just like arrays, they occupy a contiguous block of memory and can adjust their size as you add more elements to the end using push_back.

Unsigned variables hold a decimal value just like an integer, but they don't have the bit that allows them to store negative values, only positive ones.

<sstream> is the header file for stringstream. You can read values into a string (istringstream) or read from a string (ostringstream) or both (stringstream). You'll find that there's equivalent classes for standard streams (ostream and istream) which I have overloaded operators for input and output of the struct to cin and cout.

Iterators are used just like an index to an array in a for loop, except are custom tailored for use with a vector and the standard template library.

What you specifically need to solve your problem is right there in the istream operator>>. Put the line into a stream, extract the three numeric values and then get the description with another call to getline.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    string line;
    ifstream infile;
    
    int num, quant, min;
    
    infile.open("inventorytest.data");
    
    infile >> num >> quant >> min;
    getline(infile, line);
    
    while(infile)
    {       
       cout << num << " " << quant << " " << min << " "
            << line << endl;
       infile >> num >> quant >> min;
       getline(infile, line);
    }
    
    cin.get();
    cin.get();
    
    return 0;


This worked except line gets an empty space before the string I want, but I want to thank you for your responses because they did help me figure it out. Thanks again.
Topic archived. No new replies allowed.