A Simple problem, Need fast solution !!!

Ok my problem might be really stupid but i can;t figure it out and i am close to start pumping my head on wall :P
The thing i want to do but it fails is that.
I have put a #define but that number should be given by the user and not be predifined.
But with the little knowlegde i have , can't do it.
I need help asap !!!

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
#include <cstdlib> 
#include <iostream> 
#define MAX_ATHLETES 4

using namespace std; 
struct athlete 
{ 
    char lastname[40]; 
    float p1,p2,p3,p4; 
}; 


void show_athletes(athlete A[]); 
int main(int argc, char *argv[]){ 
    int i; 
   athlete A[MAX_ATHLETES]; 
    for(i=0;i<MAX_ATHLETES;i++){ // H for the kanei oses epanalipseis dhlwsame
       printf("\n*** Give Athletes Data: \n"); 
       printf("Lastname: "); 
       gets(A[i].lastname); 
       do{ 
            printf("First Jump: "); 
            scanf("%f",&A[i].p1);getchar(); 
       }while(A[i].p1<0); 
       do{ 
            printf("Second Jump: "); 
            scanf("%f",&A[i].p2);getchar(); 
       }while(A[i].p2<0); 
       do{ 
            printf("Third Jump: "); 
            scanf("%f",&A[i].p3);getchar(); 
       }while(A[i].p3<0); 
       do{ 
            printf("Fourth Jump: "); 
            scanf("%f",&A[i].p4);getchar(); 
       }while(A[i].p4<0); 
    } 
     printf("Press Enter to Show Athletes with Jumps longer than 7.50"); 
     getchar(); 
     show_athletes(A); 
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 
void show_athletes(athlete A[]){  
    int i; 
    for(i=0;i<MAX_ATHLETES; i++){ 
       if (A[i].p1>7.5 || A[i].p2>7.5 || A[i].p3>7.5 || A[i].p4>7.5) 
          printf("The Athlete:%40s Jumped Longer than 7.50\n",A[i].lastname);
          printf("There Jumps are:\n%f\n%f\n%f\n%f\n",A[i].p1,A[i].p2,A[i].p3,A[i].p4); 
    } 
} 

Are you supposed to use c or c++? You use c functions, but include c++ headers..

if c++,
1
2
3
4
5
6
7
8
9
10
int main(){
   int max_athletes;
   cout << "how many athletes? ";
   cin >> max_athletes;
   athlete* A = new athlete[max_athletes];
   //do stuff...
   //note that you'll have to pass max_athletes into show_athletes.
   delete[] A;
   return 0;
}


if c,
1
2
3
4
5
6
7
8
9
10
int main(){
   int max_athletes;
   printf("how many athletes? ");
   scanf("%d", &max_athletes);
   athlete* A = (athlete*)calloc(max_athletes, sizeof(athlete);
   //do stuff...
   //note that you'll have to pass max_athletes into show_athletes.
   free(A);
   return 0;
}
Last edited on
@hamsterman
i suppose to use C++
the problem is that i can't pass anything to show_athletes...
i might doing something wrong
Last edited on
1
2
3
4
5
6
7
8
9
10
11
void show_athletes(athlete A[], int max);

int main(){
   //previous post
   show_athletes(A, max_athletes);
   //...
}

void show_athletes(athlete A[], int max_athletes){
   //...
}
closed account (z05DSL3A)
If by " i can't pass anything to show_athletes" you mean that you are not allowed to change the signature, use a global:
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
#include <cstdlib> 
#include <iostream> 

struct athlete 
{ 
    char lastname[40]; 
    float p1,p2,p3,p4; 
}; 


void show_athletes(athlete A[]); 

int max_athletes = 0;
int main(){
   printf("how many athletes? ");
   scanf("%d", &max_athletes);
   athlete* A = (athlete*)calloc(max_athletes, sizeof(athlete));
   
   show_athletes(A);

   free(A);
   return 0;
}

void show_athletes(athlete A[])
{  
    int i; 
    for(i=0;i<max_athletes; i++){ 
       if (A[i].p1>7.5 || A[i].p2>7.5 || A[i].p3>7.5 || A[i].p4>7.5) 
          printf("The Athlete:%40s Jumped Longer than 7.50\n",A[i].lastname);
          printf("There Jumps are:\n%f\n%f\n%f\n%f\n",A[i].p1,A[i].p2,A[i].p3,A[i].p4); 
    } 
}
Ok i did with both ways, and both
give me that :

How many athletes; 2
***Give athletes Data:
Lastname: First Jump:


Instead of:
How many athletes; 2
***Give athletes Data:
Lastname:
First Jump:

With few words skips the 1st lastname, the other one goes ok.
Here is the code.

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
#include <cstdlib> 
#include <iostream> 

using namespace std; 
struct athlete 
{ 
    char lastname[40]; 
    float p1,p2,p3,p4; 
}; 


void show_athletes(athlete A[], int MAX_ATHLETES); 
int main(int argc, char *argv[]){ 
    int MAX_ATHLETES;
    cout << "How many Athltes; ";
    cin >> MAX_ATHLETES;
    athlete* A = new athlete[MAX_ATHLETES];
    int i;
    for(i=0;i<MAX_ATHLETES;i++){ 
       printf("\n*** Give Athletes Data: \n"); 
       printf("Lastname: "); 
       gets(A[i].lastname); 
       do{ 
            printf("First Jump: "); 
            scanf("%f",&A[i].p1);getchar(); 
       }while(A[i].p1<0); 
       do{ 
            printf("Second Jump: "); 
            scanf("%f",&A[i].p2);getchar(); 
       }while(A[i].p2<0); 
       do{ 
            printf("Third Jump: "); 
            scanf("%f",&A[i].p3);getchar(); 
       }while(A[i].p3<0); 
       do{ 
            printf("Fourth Jump: "); 
            scanf("%f",&A[i].p4);getchar(); 
       }while(A[i].p4<0); 
    } 
     printf("Press Enter to Show Athletes with Jumps longer than 7.50"); 
     getchar(); 
     show_athletes(A,MAX_ATHLETES); 
     system("PAUSE"); 
    return EXIT_SUCCESS; 
} 
void show_athletes(athlete A[],int MAX_ATHLETES){  
    int i; 
    for(i=0;i<MAX_ATHLETES; i++){ 
       if (A[i].p1>7.5 || A[i].p2>7.5 || A[i].p3>7.5 || A[i].p4>7.5) 
          printf("The Athlete:%40s Jumped Longer than 7.50\n",A[i].lastname);
          printf("Ta almata tou einai:\n%f\n%f\n%f\n%f\n",A[i].p1,A[i].p2,A[i].p3,A[i].p4); 
    } 
} 
Nevermind guys i found it.
i just changed the
gets(A[i].lastname)
with
cin >> A[i].lastname
and worked perfectly
Thnx for help :)
Topic archived. No new replies allowed.