Problems with type char arrays
Mar 7, 2011 at 12:13am UTC
I have to write a simple program for school in which a teacher has to enter the following information on the students: number, name, and GPA. At the end, the program has to print on screen the number of students that have a GPA of 7 or above. Here is what I did:
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
#include <stdio.h>
int main ()
{
int num, prom, cont, tot, opc;
char nombre [20];
cont = 0;
tot = 0;
do
{
printf(" MENU\n\n" );
printf("1. Insert student info\n" );
printf("2. Show results\n" );
printf("3. Exit\n\n" );
printf("Pick one: " );
scanf("%d" , & opc);
switch (opc)
{
case 1:
printf("Number: " );
scanf("%d" , & num);
printf("Name: " );
scanf("%s" , &nombre);
printf("GPA: " );
scanf("%d" , & prom);
if (prom >= 7)
{
cont = cont + 1;
}
else
{
}
break ;
case 2:
printf("\n\n%d de %d have a GPA of 7 or higher.\n\n" , cont, tot);
break ;
case 3:
fflush (stdin);
getchar();
return 0;
break ;
default :
printf("\n\nInvalid option." );
break ;
}//switch-case
}//do-while
while (opc != 3);
}
But the compiler shows the following error:
1 2
aprobados.cpp: In function ‘int main()’:
aprobados.cpp:28: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char * (*)[20]’
I have used arrays once before and I used them just as I did now, and it didn't give me any trouble. Can someone please explain what I did wrong?
Mar 7, 2011 at 12:25am UTC
Do away with the & before nombre. Arrays already are pointers, so &nombre is actually a char**.
Mar 7, 2011 at 2:35am UTC
Thank you very much! The program works just fine now.
Topic archived. No new replies allowed.