c语言输入字母和数字,如何同时输入字符串和数字

怎么同时输入字符串和数字

本帖最后由 u012495008 于 2013-12-28 09:47:33 编辑

我在弄一个学生成绩管理系统,用一个函数怎么同时输入学生名字数组和成绩数组,新手求大神解。。。拜托

分享到:

更多

------解决方案--------------------

分两个数组,分别读入,然后后续合并做处理就可以,或者读取一行的方法读入,都可以。

------解决方案--------------------

C++可以operator >>重载

------解决方案--------------------

函数设置一个标记位Flag,表示该输入数据类型(学生名字数组和成绩数组),这样公用了一个接口。例如Flag = 1

为学生名字,Flag = 2为学生成绩  不知道这是不是你想要的。

------解决方案--------------------

引用:Quote: 引用:分两个数组,分别读入,然后后续合并做处理就可以,或者读取一行的方法读入,都可以。后续合并处理的时候老是出问题,如果输入字符串用getchar(),成绩用scanf输入的话可不可行呢?都用scanf也不行。。。

参考如下代码的输入:

#include 

#include 

#include 

typedef struct _bookinfo

{

char  name[60];

float price;

}bookinfo;

int main()

{

int nbookCount = -1;

int maxpriceindex = 0;

float maxprice = 0.0;

bookinfo *pBooklist = NULL;

printf("please intput the number of books:\n");

scanf("%d", &nbookCount);

if(nbookCount <= 0)

{

printf("error books count!\n");

exit(-1);

}

pBooklist = (bookinfo *)malloc(nbookCount * sizeof(bookinfo));

if(pBooklist == NULL)

{

printf("malloc memory failed!!\n");

exit(-1);

}

int i;

for(i=0; i

{

printf("please intput the %dth book name:\n", i+1);

scanf("%s", pBooklist[i].name);

printf("please intput the %dth book price:\n", i+1);

scanf("%f", &pBooklist[i].price);

if(maxprice 

{

maxprice = pBooklist[i].price;

maxpriceindex = i;

}

printf("\n\n----------------------------------------------------\n", i+1);

}

printf("The most dear book is:[%s]. The price is:[%f]\n", pBooklist[maxpriceindex].name, pBooklist[maxpriceindex].price);

free(pBooklist);

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值