Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Reading and Storing data

sabsLONsabsLON Member Posts: 2
Hi, I am writing a mathematical program in C++ and am having a couple of problems.

The user inputs a number of entries in the form
( type parameter1 parameter2 weight ) eg

>> ( alpha 2 3 0.4 ) + ( gamma 6 2 0.6 )

The above is one input with 2 entries. There are only 5 types of names, and the type of the name determines the manner in which the parameters in an entry are used.

The user can enter as many entries as he wishes.

What I am having problems with is how to read the entries and
how to store them.

Im thinking of using arrays, and storing similar elements in
the same array, so
I would get something like below

name_array[0] = alpha name_array[1]= gamma
param1_array[0] = 2 param1_array[1] = 6
param2_array[0] = 3 param2_array[0] = 2
weight_array[0] = 0.4 weight[1] = 0.6

What I would like to know is:
How to read the input and store the individual elements of an entry??
How would I read the input so that the program recognises what it is reading is the name, or what it is reading is the 2nd parameter. The program should be able to cope with more than 1 whitespace between different elements in an entry.

any help or ideas would be really appreciated.

Thanks

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    [blue]use getline(infile,line) instead of the >> operator, then parse the string in-memory. For example:[/blue]
    [code]
    #include
    #include
    int main()
    {
    string line;
    fstream infile;
    infile.open("filename",iod::in);
    while( !infile.eof())
    {
    getline(infile,line);
    // now parse line
    }
    }
    [/code]

    : Hi, I am writing a mathematical program in C++ and am having a couple of problems.
    :
    : The user inputs a number of entries in the form
    : ( type parameter1 parameter2 weight ) eg
    :
    : >> ( alpha 2 3 0.4 ) + ( gamma 6 2 0.6 )
    :
    : The above is one input with 2 entries. There are only 5 types of names, and the type of the name determines the manner in which the parameters in an entry are used.
    :
    : The user can enter as many entries as he wishes.
    :
    : What I am having problems with is how to read the entries and
    : how to store them.
    :
    : Im thinking of using arrays, and storing similar elements in
    : the same array, so
    : I would get something like below
    :
    : name_array[0] = alpha name_array[1]= gamma
    : param1_array[0] = 2 param1_array[1] = 6
    : param2_array[0] = 3 param2_array[0] = 2
    : weight_array[0] = 0.4 weight[1] = 0.6
    :
    : What I would like to know is:
    : How to read the input and store the individual elements of an entry??
    : How would I read the input so that the program recognises what it is reading is the name, or what it is reading is the 2nd parameter. The program should be able to cope with more than 1 whitespace between different elements in an entry.
    :
    : any help or ideas would be really appreciated.
    :
    : Thanks
    :

  • warenetwarenet Member Posts: 8
    --------------------------------------------------------

    I'd set up a loop to get input from the user,
    using a variable to keep track of storing the data
    in appropriate positions of the data arrays,
    such as:

    int index = 0;
    char exit;

    do
    {
    cout << "Enter entry: ";
    cin >> name_array[index] >> param1_array[index]
    >> param2_array[index] >> weight_array[index];
    index++; // increment index for next entry
    cout << "Enter another entry (Y/N)?";
    cin >> exit;
    } while (exit == 'N' || exit == 'n')


    ---------------------------------------------------------


    : Hi, I am writing a mathematical program in C++ and am having a couple of problems.
    :
    : The user inputs a number of entries in the form
    : ( type parameter1 parameter2 weight ) eg
    :
    : >> ( alpha 2 3 0.4 ) + ( gamma 6 2 0.6 )
    :
    : The above is one input with 2 entries. There are only 5 types of names, and the type of the name determines the manner in which the parameters in an entry are used.
    :
    : The user can enter as many entries as he wishes.
    :
    : What I am having problems with is how to read the entries and
    : how to store them.
    :
    : Im thinking of using arrays, and storing similar elements in
    : the same array, so
    : I would get something like below
    :
    : name_array[0] = alpha name_array[1]= gamma
    : param1_array[0] = 2 param1_array[1] = 6
    : param2_array[0] = 3 param2_array[0] = 2
    : weight_array[0] = 0.4 weight[1] = 0.6
    :
    : What I would like to know is:
    : How to read the input and store the individual elements of an entry??
    : How would I read the input so that the program recognises what it is reading is the name, or what it is reading is the 2nd parameter. The program should be able to cope with more than 1 whitespace between different elements in an entry.
    :
    : any help or ideas would be really appreciated.
    :
    : Thanks
    :

    ---------------------------------------
    - Mike Ware
    - "The Programmer's Domain"
    - http://www11.brinkster.com/warenet
    ---------------------------------------

Sign In or Register to comment.