Hello,
I am trying to read a string of firstname and lastname and once I type in the name it immediately jumps past the gets and scanf in the first called function.
Can anyone correct this issue? Thanks
<code>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void getstudents(char [][50],int *,int *,int *, int);
float classaverage(int *,int *,int *,float *, int);
void classreport( char [][50], int *,int *,int *,float *, float, int);
void main(){
char names[30][50];
int test1[30],test2[30],final[30],num;
float avg[30], clavg;
/* Begin to read number of students */
printf("How many students are in the class?\n"
;
scanf("%d" , &num);
getstudents( names , test1, test2, final, num);
clavg=classaverage( test1, test2, final, avg, num);
classreport( names, test1, test2, final, avg, clavg, num);
}
/* student name, test 1, test 2 , final */
void getstudents(char n[][50], int *t1, int *t2, int *f, int c)
{
int i;
for( i=0; i < c; i++){
printf("Enter student #%d name :\n", i+1);
gets( n);
printf("Enter test 1 grade :\n"
;
scanf("%d", &t1);
printf("Enter test 2 grade :\n"
;
scanf("%d", &t2);
printf("Enter final exam grade :\n"
;
scanf("%d", &f);
}
return;
}
/* function class average calculates the class average */
float classaverage(int *t1, int *t2, int *f, float *a, int c){
float clavg;
int i;
clavg = 0;
for ( i=0; i < c; i++)
{
a = (float)(( t1 + t2 + f) / 3);
clavg = clavg + a;
}
return clavg/c;
}
/* function class report */
void classreport(char n[][50], int *t1, int *t2, int *f, float *a, float ca, int c)
{
int x;
printf("Report of class.\n\n"
;
for( x=0; x < c; x++){
printf("Sudent\t\tTest 1\tTest 2\tFinal\tAverage\n"
;
printf("%s%16d%9d%9d%11.2f\n\n", n[x],t1[x],t2[x],f[x],a[x]);
}
printf("The class average is %6.2f\n\n", ca);
return;
}
</CODE>
I am trying to read a string of firstname and lastname and once I type in the name it immediately jumps past the gets and scanf in the first called function.
Can anyone correct this issue? Thanks
<code>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void getstudents(char [][50],int *,int *,int *, int);
float classaverage(int *,int *,int *,float *, int);
void classreport( char [][50], int *,int *,int *,float *, float, int);
void main(){
char names[30][50];
int test1[30],test2[30],final[30],num;
float avg[30], clavg;
/* Begin to read number of students */
printf("How many students are in the class?\n"
scanf("%d" , &num);
getstudents( names , test1, test2, final, num);
clavg=classaverage( test1, test2, final, avg, num);
classreport( names, test1, test2, final, avg, clavg, num);
}
/* student name, test 1, test 2 , final */
void getstudents(char n[][50], int *t1, int *t2, int *f, int c)
{
int i;
for( i=0; i < c; i++){
printf("Enter student #%d name :\n", i+1);
gets( n);
printf("Enter test 1 grade :\n"
scanf("%d", &t1);
printf("Enter test 2 grade :\n"
scanf("%d", &t2);
printf("Enter final exam grade :\n"
scanf("%d", &f);
}
return;
}
/* function class average calculates the class average */
float classaverage(int *t1, int *t2, int *f, float *a, int c){
float clavg;
int i;
clavg = 0;
for ( i=0; i < c; i++)
{
a = (float)(( t1 + t2 + f) / 3);
clavg = clavg + a;
}
return clavg/c;
}
/* function class report */
void classreport(char n[][50], int *t1, int *t2, int *f, float *a, float ca, int c)
{
int x;
printf("Report of class.\n\n"
for( x=0; x < c; x++){
printf("Sudent\t\tTest 1\tTest 2\tFinal\tAverage\n"
printf("%s%16d%9d%9d%11.2f\n\n", n[x],t1[x],t2[x],f[x],a[x]);
}
printf("The class average is %6.2f\n\n", ca);
return;
}
</CODE>