tags:

views:

102

answers:

3

I have a member function that asks the user for a file name and then gets input from file and uses it on different functions. Each function takes in chars or chars. However its not quite executing the functions. I think it has to do with sstream picking the data out from the file then making them chars? Is it possible to separate the elements from the file using sstream then use those elements as chars?

void My_Function::file()
{
    fstream data;//file input
    char filename[80];
    string line;
    int first;
    char sec, third, fourth;

    cout<<"Enter file name: \n";
    cin>>fileName;
    data.open(fileName);

    while(getline(data,line))
    {
        stringstream str(line);
     istringstream ins;
     ins.str(line);//get line
     str >> first >> sec >> third >> fourth;

         switch(first)
         {
             case 1:
                  add(sec);
                  break;
             case 2:
                  delete_item(sec, third);
                  break;
             case 3:
                  print_everything(sec, third);
                  break;
             case 4:
                  makenew(sec, third);
                  break;
             case 5:
                  find(sec, third, fourth);
                  break;
             case 0:
                  break;
        }
    }
}
A: 

Your code doesn't compile as written, but after fixing the "filename"/"fileName" problem it seems to me that this particular part does what it is supposed to do. You should consider trying to clarify your question, and to explain what you expected the code to do and how that differs from what it actually does. It really is a bit unclear what you mean by "not quite executing the functions"!

To test things like these, it is sometimes useful to change a line like

delete_item(sec, third);

to

cout << "delete_item(" << sec << ", " << third << ")\n";
Thomas Padron-McCarthy
I tried this and all the input from the file is where it should be. Does this change its memory type? stringstream str(line); istringstream ins; ins.str(line);//get line str >> first >> sec >> third >> fourth;
Jack Null
A: 

You might want to change

stringstream str(line);
istringstream ins;
ins.str(line);//get line
str >> first >> sec >> third >> fourth;

into

istringstream ins(line);
ins >> first >> sec >> third >> fourth;
Peter Jansson
I tried this, it does the samething. The int works but the char's dont seem to be doing there jobs.
Jack Null
+1  A: 

I think that your problem is with the data. For your code to work it should be formated like :

0 1 2 3
1 1 2 3
2 1 2 3
3 1 2 3
4 1 2 3
5 1 2 3

Post a sample of your data for us to check.

This code works as expected with those data (your code in a main with couts) :

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
    fstream data;//file input
    char fileName[80];
    string line;
    int first;
    char sec, third, fourth;

    cout<<"Enter file name: \n";
    cin>>fileName;
    data.open(fileName);

    while(getline(data,line))
    {
        stringstream str(line);
        istringstream ins;
        ins.str(line);//get line
        str >> first >> sec >> third >> fourth;

        cout << "line = " << str.str() << endl;

         switch(first)
         {
             case 1:
                  cout << "add(sec); " << sec << endl;
                  break;
             case 2:
                  cout << "delete_item(sec, third); " << sec << ", " << third << endl;
                  break;
             case 3:
                  cout << "print_everything(sec, third); " << sec << ", " << third << endl;
                  break;
             case 4:
                  cout << "makenew(sec, third);" << sec << ", " << third << endl;
                  break;
             case 5:
                  cout << "find(sec, third, fourth); " << sec << ", " << third << ", " << fourth << endl;
                  break;
             case 0:
                  cout << "0" << endl;
                  break;
        }
    }
}

with the output :

Enter file name: 
toto
line = 0 1 2 3
0
line = 1 1 2 3
add(sec); 1
line = 2 1 2 3
delete_item(sec, third); 1, 2
line = 3 1 2 3
print_everything(sec, third); 1, 2
line = 4 1 2 3
makenew(sec, third);1, 2
line = 5 1 2 3
find(sec, third, fourth); 1, 2, 3
line = 
find(sec, third, fourth); 1, 2, 3
neuro