lundi 4 mars 2019

How could I design this without using sstream? c++11, c++

enter image description here

This code is currently written using sstream, but I'm curious as to how i could approach it without using JUST fstream and getline. I am not great with c++ so explain with some minor detail. I appreciate all of the help I receive in advance.

Again, preferably no string stream.

#include <string>

#include <algorithm>

#include <stdlib.h>

#include <sstream>

#include <fstream>

using namespace std;



int readRatings(char filename[],string usernames[],int rating[100][50], int &row,int &column)

{

ifstream infile;

infile.open(filename);

if(!infile)

{

cout<<"Error to open file "<<filename<<endl;

return -1;

}

string line;

row=0;

column=0;

while(getline(cin,line)!=NULL)

{

stringstream s(line);

string word;

int count = 0;

while(s>>word)

{

cout<<word<<endl;

if(count==0)

{

usernames[row]=word;

count++;

}

else

{

int x=0;

stringstream temp(word);

temp>>x;

rating[row][count++]=x;

}

}

if(count>column)

column=count;

row++;

}

}



// Driver code

int main()

{

char filename[100] ="Ratings.txt";

int row,column,rating[100][50];

string usernames[100];

readRatings(filename,usernames,rating,row,column);

for(int i=0; i<row; i++)

{

string username=usernames[i];

cout<<username<<" ";

for(int j=0; j<column; j++)

{

cout<<rating[i][j]<<" ";

}

cout<<endl;

}

return 0;

}

Aucun commentaire:

Enregistrer un commentaire