Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/*The following is just an initial part of my code. I am trying to store the first name, last name and score in their respective arrays. When I run this code two odd things happen :

The first name of the first entry isn't printed
Though the code seems correct to me the float scores are always printed as 0.00000​ or some garbage value.

Why is so happening ?
*/

C++
#include<stdio.h>
#include<string.h>
int main()
{
int entry,i,option;
printf("\nPlease indicate the number of records you want to enter :\n");
scanf("%d",&entry);
char name[entry][19] ,surname[entry][19],search_last_name [19],search_first_name[19];
float score[entry];
printf("\nPlease input records of students (enter a new line after each record), with following format first name last name score \n");
for(i=0;i<entry;i++)>
	{
	scanf("%s%s%f",name[i],surname[i],score[i]);
	}
for(i=0;i<entry;i++)>
	{
	printf("\n %s %s %f",name[i],surname[i],score[i]);
	}
return 0;
}
Posted
Updated 23-Apr-16 22:19pm
v5
Comments
nv3 24-Apr-16 4:13am    
Your program should actually not even compile. The C language does not have dynamically sized arrays. Hence, a construct like

char name[entry][19];

is not allowed. All array dimensions must be constant and known at compile time.
There are ways you can allocate your arrays with a variable size, but now I would simply specify a reasonably large size instead of entry.
[no name] 24-Apr-16 4:24am    
Thanks !

1 solution

Quote:
C++
char name[entry][19] ,surname[entry][19],search_last_name [19],search_first_name[19];
float score[entry];
This work only if entry is known at compile-time (not your case).
If entry is known only at run-time (your case) you need to use dynamic memory allocation.

This is HomeWork, you do the search.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900