如何让float数组接受值而不是打印垃圾值? [英] How do I get the float array to accept values instead of print garbage values ?

查看:103
本文介绍了如何让float数组接受值而不是打印垃圾值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *以下只是我的代码的初始部分。我试图在它们各自的数组中存储名字,姓氏和分数。当我运行此代码时,会发生两件奇怪的事情:



第一个条目的第一个名称未打印

虽然代码似乎正确对我来说,浮点数总是打印为0.00000或一些垃圾值。



为什么会这样?

* /



/*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 ?
*/

#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;
}

推荐答案

Quote:

char name[entry][19] ,surname[entry][19],search_last_name [19],search_first_name[19];
float score[entry];

仅当条目在编译时已知(不是你的情况)。

如果条目仅在运行时(你的情况)知道你需要使用动态内存分配。



这是HomeWork,你进行搜索。

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.


这篇关于如何让float数组接受值而不是打印垃圾值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆