0

I am working on a client/server program using sockets and I am trying to parse the input file. I have a struct to store the Majors, the early career pay, and the mid-career, respectively. The client program asks the user to input the name of the major and the server program returns both salaries.

The issue with the input file is this:

Geophysics 54100 122200

Cognitive Science 54000 121900

Electrical Power Engineering 68600 119100

They are all separated as Major[TAB]Pay[Tab]Pay, and the majors have spaces in them. I want to store each of them in the struct.

Any solution to this?

3

2 Answers 2

1

Start with something this:

ifstream f("c:\\temp\\test.txt"); string s; while (getline(f, s)) { istringstream iss(s); string major, early, mid; getline(iss, major, '\t'); getline(iss, early, '\t'); getline(iss, mid, '\t'); cout << major << '|' << early << '|' << mid << endl; } 
Sign up to request clarification or add additional context in comments.

3 Comments

So that prints out everything except the first letter of every major. If I remove the space from the second getline, I get this: Geophysics54100 122200
This is what I get as output: Major: Petroleum Early: Mid: Major: Engineering Early: 94600 Mid: Major: 175500
But I have to push the data into the struct. I was just using the cout to see if it's being split and stored the way I want it to.
1

You can use the third argument in getline() to say what character to stop at. The default is \n, but you can also specify that it is \t to have it stop at the tab you want:

getline(std::cin, line, '\t'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.