Impulse
Impulse

Reputation: 155

File Input, trying to get extract Int's from a string (C++)

Hey guys, I'm trying to separate this list from a text file

15

Albert Einstein 52 67 63

Steve Abrew 90 86 90 93

David Nagasake 100 85 93 89

Mike Black 81 87 81 85

Andrew Van Den 90 82 95 87

Joanne Dong Nguyen 84 80 95 91

Chris Walljasper 86 100 96 89

Fred Albert 70 68

Dennis Dudley 74 79 77 81

Leo Rice 95

Fred Flinstone 73 81 78 74

Frances Dupre 82 76 79

Dave Light 89 76 91 83

Hua Tran Du 91 81 87 94

Sarah Trapp 83 98

Into Full name, so Albert Einstein, and then their following int's as an array.

However I'm not sure how to go about doing this.

This is what I have been doing so far, but it's just not clicking for me.

void Student::getData(Student * stdPtr, int len)
{
    int tempt;
    int sucker = 0;

    ifstream fin;
    fin.open("students.dat");
    fin >> tempt;

    while(!fin.eof())
    {
        string temp;
        getline(fin, temp,'\n');

        stringstream ss;
        ss << temp;
        ss >> sucker;

        cout << temp << " "<<  sucker <<   endl;
        sucker = 0;
    }
    fin.close();
}

I feel like I'm somewhat close, with actually been able to get the numbers by themselves with the stringstream, but I don't know how to indicate to my program that I'm starting on a new student

Thanks for the help guys!

Upvotes: 2

Views: 649

Answers (3)

Robin Halder
Robin Halder

Reputation: 243

I am just giving You the technique how to separate integers and string from a line. So from this You can implement Your own one :

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>


using namespace std;


int main()
{
    string in;
    int i,j,k,n;

    scanf("%d",&n);

    getchar();

    for(j=0 ;j<n ;j++){



        getline(cin,in);

        stringstream ss(in);
        string tem;

        vector<string>vstring;
        vector<int> vint;

        while(ss>>tem){

            if(isalpha(tem[0]))
                vstring.push_back(tem);
            else{

                k  = atoi(tem.c_str());
                vint.push_back(k);
            }

        }

        cout<<"String are : ";

        for(i=0 ;i<vstring.size() ;i++)
            cout<<vstring[i]<<" ";

        cout<<"\nIntegers are : ";

         for(i=0 ;i<vint.size() ;i++)
            cout<<vint[i]<<" ";

            cout<<endl;

    }



    return 0;
}

Upvotes: 0

DarioP
DarioP

Reputation: 5465

Try something like this after the getline:

stringstream ss(temp);
string name;
string surname;
ss >> name >> surname;
int i;
while (ss >> i) {
  cout << i << ' ';
}
//follows a fast fix to manage names with three words
if (!ss.eof()) { //we tried to extract an int but there was another string
                 //so it failed and didn't reach eof
  ss.clear();    //clear the error bit set trying to extract a string to an int
  string thirdname;
  ss >> thirdname;
  while (ss >> i) {
    cout << i << ' ';
  }
}

...or check out this example: https://ideone.com/OWLHjO

Upvotes: 1

Pervez Alam
Pervez Alam

Reputation: 1256

Here is simple algo (too lazy to write full code :P)

1) Keep reading using fin >> temp_str here temp_str is std::string.

2) Use std::stoi(temp_str) to convert string to integer.

If it is not integer and is string, it will through invalid exception. Use this exception to:

2A) Last value was int: It is name-data for new object.

2B) Last value was not int: It is next part of name and you should append to last string.

3) If no exception was thrown, it is a number, save in the current object.

4) Keep reading file till end.

Upvotes: 2

Related Questions