Reputation: 458
Hello i am beginner at c++ and now just take few test of exams to learn but now i face a problem its kind missing knowledge of c++ , so i have a file with u2.txt and his content is
3 5
Petras Rasa // name of dancer
3 1 5 8 10 // scores for a technique
5 6 7 8 9 // scores for art
Rita Jurgis
6 5 8 5 8
9 8 7 6 5
Rasa Linas
10 10 10 10 10
8 8 8 8 8
so task said that the first two numbers shows dancer and judge numbers , second line shows name of dancer the line below shows scores of
technique and one line below show scores of artistry , and so its 3 dancers with a scores below .
So now i need to write a function witch read that file and stores a scores and names into variables , i am using class and array system , so i will be storing them there , the thing is i do not really know how to read such file because till now i was learning to real a file like Name,54 or so so i was using a getline and end the line with ',' but there is no comma hare so i face a problem hare this is my function till now :
const string U2 = "U2.txt";
const string U2rez = "rez.txt";
class Results
{
public:
string name;
int scorestech;
int scoresart;
};
int main()
{
Results v[5]
return 0;
}
void Freading(const string fn,Results v[])
{
int alldancers;
int alljudge;
ifstream fin(fn.c_str());
fin >> alldancers >> alljudge;
for(int a =0; a < 3; a++){
}
}
so now i need to read this file and count a scores of art and technique of each dancer
Upvotes: 0
Views: 175
Reputation: 458
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const string U2 = "U2.txt";
const string U2rez = "rez.txt";
class Results
{
public:
string name;
int scorestech;
int scoresart;
};
void Freading(const string fn,Results v[]);
int main()
{
Results v[5];
Freading(U2,v);
return 0;
}
void Freading(const string fn,Results v[])
{
int alldancers;
int alljudges;
int biggest = 0;
int smallest = 99;
ifstream fin(fn.c_str());
fin >> alldancers >> alljudges;
fin.ignore();
for(int a =0; a < alldancers; a++){
int biggest = 0;
int smallest = 99;
int totaltech = 0;
int totalart = 0;
getline(fin, v[a].name);
for(int b = 0; b < alljudges; b++)
{
int scores;
fin >> scores;
totaltech += scores;
}
for(int b =0; b < alljudges; b++)
{
int scores;
fin >> scores;
totalart += scores;
}
v[a].scorestech = totaltech;
v[a].scoresart = totalart;
cout << v[a].name << endl;
cout << v[a].scorestech <<endl;
cout << v[a].scoresart <<endl;
}
fin.close();
}
this is what i done so far but the result i got is :
Petras Rasa
27
35
0
45
0
45
ware i did mistake or what i did wrong , because somehow when the loop goes second time it doesn't capture a name of the second dancer
Upvotes: 0
Reputation: 1426
Before asking a question like this, you should have search more on this, anyway try the method given in below.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main ()
{
string line_one_by_one;
ifstream inputfile;
inputfile.open ("names.txt");
while(!inputfile.eof) // To get you all the lines.
{
getline(infile, line_one_by_one); // Saves the line in line_one_by_one
}
inputfile.close();
system ("pause");
}
What you have to do is identify each line and storing things you need. further read string to int conversion in c++.
read :
http://www.cplusplus.com/reference/istream/istream/read/
http://www.cplusplus.com/reference/string/string/?kw=string
Upvotes: 0
Reputation: 179779
Rezultatai v[5]
, besides missing a semi-colon, defines a old-style, fixed-length array. That's incorrect, the second number in the file tells you how many judges there are.
Instead, use std::vector<Type>
. That can vary its length.
And I'm guessing (since you didn't use English) that for this example you actually should have had 3 Rezultatai
, one for each dancer. Each of those should have 2x5 scores.
Upvotes: 2