Read Numbers form text file. Help college project .

Hello i need help for my college project i am stack in one problem for more then a weak and i can`t find any solution on google.

So this is my project.

I need to have text doc who have data for 2 teams that have 15 player each.
Each player have info about him :

Name :
Last Name:
Team Name :
Number:
Gools:
Strikes:
Time:

So first we need to submit all player info in array and after to submit only Name , Last Name and gold in text file.

Name:
Last Name:
Gools:

So when we finis all that i need to use that text file to see which team have more gools and who is best player on that match .

Now i don`t know how to read Int form text files and after use that numbers to collect each other. (1+1+1) bed english :)

In thah text file i have somethink like this:

Team One

Name:Leon
LastName:Polo
Gools: 5

Name:Marko
LastName:Polo
Gools: 5

Team Two

Name:Spider
LastName:Man
Gools:

Name:Bat
LastName:Man
Gools: 1000


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    struct team1{
    char Name[15];
    char LastName[15];
    char TeamName[15];
    int Gools;
    int Strikes;
    }team1[15];

  {
    struct team2{
    char Name[15];
    char LastName[15];
    char TeamName[15];
    int Gools;
    int Strikes;
    }team2[15];
;

    ofstream out("sord.txt");


    cout<<"Sublit info fore Team 1"<<endl;
    for(int i=0;i<=1;i++)
    {
        cout<<"Sumbit Name";
        cin>>team1[i].Name;
         cout<<"Submit LastName";
        cin>>team1[i].LastName;
        cout<<"Sumbit Team";
        cin>>team1[i].Team;
        cout<<"SumbitGoolsi";
        cin>>team1[i].Gools;
        cout<<"Sumbit Strikes";
        cin>>team1[i].Strikes;
    }


     cout<<"Sublit info fore Team 2"<<endl;
    for(int i=0;i<=1;i++)
    {
        cout<<"Sumbit Name";
        cin>>team2[i].Name;
         cout<<"Submit LastName";
        cin>>team2[i].LastName;
        cout<<"Sumbit Team";
        cin>>team2[i].Team;
        cout<<"SumbitGoolsi";
        cin>>team2[i].Gools;
        cout<<"Sumbit Strikes";
        cin>>team2[i].Strikes;
    }

    out<<"Tean One"<<endl;
    for(int i=0;i<=1;i++)
    {
    out<<"Name";
       out <<team1[i].Name<<endl;
    out<<"LastName";
       out <<team1[i].LastName<<endl;
    out<<"Gools";
       out <<team1[i].Gools<<endl;


    }

    out<<"Tean Two"<<endl;
    for(int i=0;i<=1;i++)
    {
    out<<"Name";
       out <<team2[i].Name<<endl;
    out<<"LastName";
       out <<team2[i].LastName<<endl;
    out<<"Gools";
       out <<team2[i].Gools<<endl;


    }

}


I have only this so far and i need to finis this till Monday.


Any help is welcome.


BTW now i change text ofstream to be in this for :

Pato Milan 5
Istra Bill 5
Ilia Nasa 6

I think it will be easy like this .
Last edited on
closed account (48T7M4Gy)
Your data model is no good :(

To start you need to have a struct called Player which encapsulates the various data items. Read the tutorial here about how struct's work.
http://www.cplusplus.com/doc/tutorial/structures/

Use strings instead of char arrays.

A team would be an array (or vector if you like) of Player objects.

To read data to and from a text file you need to read up on it and the following tutorial shows you the way including simple sample programs.

http://www.cplusplus.com/doc/tutorial/files/
I am stuck on one problem for more than a week

Ask for help sooner. My advice is to ask for help if you get stuck for more than an hour.

I need to have text doc who have data for 2 teams

Quick English lesson: use "who" when referring to people and "that" when referring to things. So this should be "...text doc that...". Also "have" is third person plural. "Has" is the singular form, so it should be either "text doc that has" (singular) or "text docs that have" (plural).

So you need to read from the file, not from the cin. Create a Player class like kemort suggests and include a >> operator that reads a player from a stream.

Does the project specify the exact format of the text files, or is that something that you can control? It will be easier to parse if your text file looks like this:
Leon
Polo
5
Marko
Polo
5

In other words, the file shouldn't contain the actual words "Name," "Last Name" etc. Instead it should contain the name, last name etc of each player.
Yes , i don`t need to have Name: before all " Names or Last Name" i because of that i use only this

1
2
3
4
5
6
7
8
9
10
11
12
13

for(int i=0;i<=1;i++)
    {
    //out<<"Name";
       out <<team1[i].Name<<"  ";
   // out<<"Prezime";
       out <<team1[i].LastName<<"  ";
    //out<<"Golovi";
       out <<team1[i].Gools<<"  "<<endl;


    }


so text file look like this

Marko Polo 5
Spider Man 5
Bat Man 6

Alos now i am looking at kemort suggests
Last edited on
closed account (48T7M4Gy)
'Goals', maybe not Gools?
Ye is Goals :) But if code don`t work Goals will do the seam job like Gools . .. Nothing.
Last edited on
closed account (48T7M4Gy)
That's true. So it's now your job to make it so it does work whatever spelling you choose.
I haven't seen anyone answer the part about reading from a file yet, so I'll do a quick summary.

You use ofstream to write data to a file, and ifstream to read data from a file.

It's similar to how cout and cin work, just open a file ifstream input("file_Name.txt"); then input >> variable;, just like cin the symbols are turned around for reading to a variable.

The best part is that you know the order that you wrote the values into the file so you just read from the file in the same order. You might have to do some error checking in case the user tries to open the wrong file. (You get teachers who are strict about error-checking some times).


Also remember to close all files after you're done using them with the close() function;
input.close();


[The following won't be needed for your assignment]:

There is also the idea of a cursor, it's an iterator that tells the program where you last read from and makes sure things are read in order. Some times you might want to skip around in a file so look into the tellg() and seekg() functions when you have some free time.

There are also functions to read a single char variable = input.get(); or to read an entire line getline(input, stringVariable); which you might also want to practice using.

http://www.cplusplus.com/reference/fstream/ifstream/
Last edited on
Tnx to all i find my solution liek this
javascript:editbox1.editTag('code')
{
ifstream in("sord.txt");
string Name;
string LastName;
int Goals;
int sum1=0;
int sum2=0;
int n=0;
ifstream Sum("sord.txt");
while(Sum>>LastName>>Name>>Goals)

{
if(n<3)
{
sum1+=Goals;
n++;
}else{
sum2+=Goals;
n++;
}
}
javascript:editbox1.editTag('code')

sord.txt file look like this

Example Example 5
Example Example 51
Example Example 12
Example Example 1
Example Example 5


Topic archived. No new replies allowed.