URGENT! Outputting data from txt file using getline

hi
Last edited on
What this program is doing is to read the first seven values from the file.
It then stores them into a particular location in the array. The value which has just been read is also displayed.

The error is that depending upon the number chosen by the user, the same data will be stored in a different place.

What it should be doing is to first read all of the data from the file, and place it into the corresponding rows and columns.

Only after that has been completed should the user be asked which row they wish to see displayed.

I don't know whether I explained that very well. The main point is that the number selected by the user should not take any part in the process of reading the data. It should only affect the output.

Sample code:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

    using namespace std;

int main()
{
    //---------- create the array and open the input file ------------

    const int cols = 7;
    const int rows = 12;
    string str[cols][rows];
    ifstream myfile("TextFile1.txt");
    int a = 0;
    int b = 0;
    if (!myfile)       //Always test the file opens
    {
        cout<<"Error opening input file"<<endl;
        system("pause");
        return 0;
    }

    //---------- read the data from the file into the array ----------

    int row = 0;
    string line;
    while (getline(myfile, line) && row < rows)
    {
        istringstream myline(line);
        string word;
        int col = 0;
        while (getline(myline, word, ' ') && col < cols)
        {
            str[col][row] = word;
            col++;
        }
        row++;
    }

    //---------- get the user input  and output the results ----------

    int number = -1;
    cout<<"Please select number between 1 and 12: ";
    cin>>number;
    while (!cin || number < 1 || number > 12)
    {
        cin.clear();
        cin.ignore(1000,'\n');
        cout << "number between 1 and 12: ";
        cin>>number;
    }
    int ix = number - 1;

    cout << "value one is:   " << str[0][ix] << endl;
    cout << "value two is:   " << str[1][ix] << endl;
    cout << "value three is: " << str[2][ix] << endl;
    cout << "value four is:  " << str[3][ix] << endl;
    cout << "value five is:  " << str[4][ix] << endl;
    cout << "value six is:   " << str[5][ix] << endl;
    cout << "value seven is: " << str[6][ix] << endl;
    
    system("pause");
    return 0;
}

Last edited on
Thank you this has been so helpful!!!

I also wanted to multiply "value two" by another number and then divide it, however I keep getting the following error..

Error C2677: binary '*' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)

Do you know any way to get around this error?

Thank you!
I'm glad that was useful.

I didn't know what sort of data was in the input file, I just saw you had an array of strings.

If you want to do any sort of calculations with the string, it needs to first be converted into a numeric type, such as double or int.

There are several different ways to do that. You could use the functions atof or atoi
http://www.cplusplus.com/reference/cstdlib/atof/
http://www.cplusplus.com/reference/cstdlib/atoi/

The code might be something like this:
double n = atof(str[1][ix].c_str());
Note the use of .c_str() to convert the C++ string to a plain character string.

You might instead use a stringstream.
1
2
3
istringstream ss(str[1][ix]);
double d;
ss >> d;


I find stringstream very useful, it's worth becoming familiar with. http://www.cplusplus.com/reference/sstream/


Note - if all of the data in the text file consists of numbers, then you should probably not be using an array of strings to handle them. Right from the start, read and store each value as a number. (But if you have a mixture of strings and numbers, then there are several different ways to look at this).
Hope this helps.
Last edited on
Topic archived. No new replies allowed.