A difficult one(At least for me:D)

Hey guys, I have to make a program and i really struggle to deal with this part of it:

I have a char from 50 symbols in which i have to CIN the names of the footballer and between each of these names should have "space" interval. I've come up with the idea to concatenate different strings[which actually are the first,the second...and the last name] and to get the whole name by them
I can use strcat to make it but if i know for exactly how many footballers... if the user has to CIN the number of the names and it is variable, i cant do it...
Can u help me? I have done it here and it works only for 3 names for each player, but not for Y, but how i do it with a variable ??? :(((
I have done only 1/10 from the real problem but i will post it anyway cause i have to do first this with the names, then the other part . And if i can do it another way tell me please...
I dont know if u have understood exactly what i struggle to do cause i am not familiar with the termins in C++ in english... and i am 16y.o.

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
ofstream file1("Domashna.txt",ios::out);
int n,y; //y=broi imena, n - futbolisti
struct Footballer
{
    
      int number;
      char name[10][50];
      int goals;
      char country[400];
      char teams[200];
           
      void Input();
    void Output();
    
};

void Footballer::Input()
{
                      
                        do
                        {
                         cout<<"Footballer number from 1 to 30";
                         cin>>number;
                         file1<<number;
                         }while((number>30)||(number<1));    
                       
                        cout<<"How many names does the player have?";   //here is the main idea for Y players...
                        cin>>y;
                             for(int i=0;i<y;i++)
                        {
                        cout<<"Bring in"<<i+1<<"-o name of the player";
                        cin>>name[i];
                        file1<<name[i];
                        }
                        
                        
                         do
                        {
                         cout<<"Bring in how many goals he has scored";
                         cin>>goals;
                         }while((goals>20)||(goals<0));
                         
                         cout<<"Bring in country";
                         cin>>country;
                         file1<<country;
                         cout<<"Bring in teams, for which he has played";
                         cin>>teams;
                         file1<<teams;
                         file1<<"\n";
}

void Footballer::Output()
{
     cout<<number;
     cout<<setw(10)<<strcat(strcat(strcat(strcat(name[0]," "),name[1])," "),name[2]); //but its working for only 3... how do i concatenate for these y?? :(
     cout<<setw(10)<<goals;
     cout<<setw(10)<<country;
     cout<<setw(10)<<teams;
}

void TopScorer(Footballer Futbolist[])
{
     int max,j;
     max=Futbolist[0].goals;
for(int m=0;m<n;m++)
if(max<Futbolist[m].goals) // ne sam pravil proverka za r avni golmaistori + 2-ta gola po -malko
{
max=Futbolist[m].goals;
j=m;
}
      cout<<"TopScorer:"<<"\n"<<"__________________________________________________________"<<"\n";
       cout<<"Number"<<setw(6)<<"Name"<<setw(10)<<"Goals"<<setw(10)<<"Country"<<setw(10)<<"Teams"<<"\n";   

     cout<<Futbolist[j].number<<setw(10)<<Futbolist[j].name[0]<<setw(10)<<Futbolist[j].goals<<setw(10)<<Futbolist[j].country<<setw(10)<<Futbolist[j].teams<<"\t"<<"\n";
     
}
      

int main()
{
 
    do
    {
         cout<<"Number footballers from 1 to 5";
         cin>>n;
     }while((n>5)||(n<1)); 
    
   Footballer Futbolist[n];
    
    
    
  
    for(int i=0;i<n;i++)
    {
    Futbolist[i].Input(); 
    }
     cout<<"Number"<<setw(6)<<"Name"<<setw(10)<<"Goals"<<setw(10)<<"Country"<<setw(10)<<"Teams";   
    for(int i=0;i<n;i++)
    {
    cout<<"\n";
    Futbolist[i].Output();
    }
    cout<<"\n";
TopScorer(Futbolist);

   
    system("pause");
    return 0;
}             
                         
                         
Last edited on
closed account (zwA4jE8b)
I don't understand but maybe instead of char name[10][50] use string name[10]
then when you output all you have to do is

cout << name[1] << " " << name[2] << etc.... etc...
Noo, i havent explained it clearly....
For example I am 5-named footballer and i tell the computer that i have 5 names and the comp should cout them in this way:

First Second Third Fourth Fifth...

the user should cin every name individually and then i have to cout it as a whole name.

then for the next footballer the users cin that it is 4-named footballer and the result should be

First Second Third Fourth...

It should be something like that
1
2
3
4
5
6
7
8
9
10
cout<<"First??"
cin>>First 
cout<<"Second??"
cin>>Second
cout<<"Third??"
cin>>Third
cin>>....
cin>>....
cout<<"N-name?"
cin>>N-name


and i should get them in just one string and between each of the names should be space interval... But how is it supposed to be done, having in mind that i dont know how many names the user will cin... they are variable and for every footballer it is different...
Last edited on
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
 #include<iostream>
#include<iomanip>
#include<fstream>
#include <cstring>
using namespace std;
ofstream file1("Domashna.txt",ios::out);
int n,y; //y=broi imena, n - futbolisti
struct Footballer
{
      int broy;
      int number;
      char name[10][50];
      int goals;
      char country[400];
      char teams[200];
           char full_name[50];
      void Input();
    void Output();
    
};

void Footballer::Input()
{
                      
                        do
                        {
                         cout<<"Footballer number from 1 to 30";
                         cin>>number;
                         file1<<number;
                         }while((number>30)||(number<1));    
                       
                        cout<<"How many names does the player have?";
                        cin>>broy;
                             for(int i=0;i<broy;i++)
                        {
                        cout<<"Bring in"<<i+1<<"-o name of the player";
                        cin>>name[i];
                        file1<<name[i];
                        }
                        
                        
                         do
                        {
                         cout<<"Bring in how many goals he has scored";
                         cin>>goals;
                         }while((goals>20)||(goals<0));
                         
                         cout<<"Bring in country";
                         cin>>country;
                         file1<<country;
                         cout<<"Bring in teams, for which he has played";
                         cin>>teams;
                         file1<<teams;
                         file1<<"\n";
}

void Footballer::Output()
{
     cout<<number;
     
     
     full_name[0] = '\0';
     for ( int i=0; i<broy; ++i ) {
      if ( i == 0 ) strcat ( full_name, name[i] );
      else {
   strcat ( full_name, " " );
   strcat ( full_name, name[i] );
   }
      }
     
     cout<<setw(10)<<full_name;
     cout<<setw(10)<<goals;
     cout<<setw(10)<<country;
     cout<<setw(10)<<teams;
}

void TopScorer(Footballer Futbolist[])
{
     int max,j;
     max=Futbolist[0].goals;
for(int m=0;m<n;m++)
if(max<Futbolist[m].goals) // ne sam pravil proverka za r avni golmaistori + 2-ta gola po -malko
{
max=Futbolist[m].goals;

}
     cout<<"TopScorer(s):"<<"\n"<<"__________________________________________________________"<<"\n";
       cout<<"Number"<<setw(6)<<"Name"<<setw(10)<<"Goals"<<setw(10)<<"Country"<<setw(10)<<"Teams"<<"\n";   
     
for(int m=0;m<n;m++)
if(max==Futbolist[m].goals)
{     
      
cout<<"\n";
     cout<<Futbolist[m].number<<setw(10)<<Futbolist[m].full_name<<setw(10)<<Futbolist[m].goals<<setw(10)<<Futbolist[m].country<<setw(10)<<Futbolist[m].teams<<"\t"<<"\n";
}
     
}
      

int main()
{
 
    do
    {
         cout<<"Number footballers from 1 to 5";
         cin>>n;
     }while((n>5)||(n<1)); 
    
   Footballer Futbolist[n];
    
    
    
  
    for(int i=0;i<n;i++)
    {
    Futbolist[i].Input(); 
    }
     cout<<"Number"<<setw(6)<<"Name"<<setw(10)<<"Goals"<<setw(10)<<"Country"<<setw(10)<<"Teams";   
    for(int i=0;i<n;i++)
    {
    cout<<"\n";
    Futbolist[i].Output();
    }
    cout<<"\n";
TopScorer(Futbolist);

   
 system("pause");
    return 0;
}

A friend of mine helped me and it's done now.
Topic archived. No new replies allowed.