Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Character Strings with gets

Status
Not open for further replies.

pecan204

Programmer
Jan 25, 2001
24
0
0
US
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(&quot;How many students are in the class?\n&quot;);
scanf(&quot;%d&quot; , &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(&quot;Enter student #%d name :\n&quot;, i+1);
gets( n);

printf(&quot;Enter test 1 grade :\n&quot;);
scanf(&quot;%d&quot;, &t1);

printf(&quot;Enter test 2 grade :\n&quot;);
scanf(&quot;%d&quot;, &t2);

printf(&quot;Enter final exam grade :\n&quot;);
scanf(&quot;%d&quot;, &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(&quot;Report of class.\n\n&quot;);

for( x=0; x < c; x++){

printf(&quot;Sudent\t\tTest 1\tTest 2\tFinal\tAverage\n&quot;);
printf(&quot;%s%16d%9d%9d%11.2f\n\n&quot;, n[x],t1[x],t2[x],f[x],a[x]);
}
printf(&quot;The class average is %6.2f\n\n&quot;, ca);
return;
}

</CODE>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top