我想要你的帮助(在C中) [英] I want to your help (in C)

查看:101
本文介绍了我想要你的帮助(在C中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是韩国人。所以,这个背景是错误的....



我想要一些数字(逗号分隔)或什么都没有。

我可以(scanf_s )功能和读取数字

但我什么都看不懂(没有输入并按回车)



如何解决我的问题?



我尝试过:



做{

scanf_s(%d,& mints [i]);

mintcount ++;

i ++;

} while( getc(stdin)==',');


$ C $ b in C

I'm korean. so, this context is something wrong....

I want some numbers(comma seperated) or nothing.
I can (scanf_s)function and read the numbers
but I can't read nothing(no input and press enter)

how solve the my question?

What I have tried:

do {
scanf_s("%d", &mints[i]);
mintcount++;
i++;
} while (getc(stdin) == ',');

in C

推荐答案

不读它直接进入 mints 数组 - 首先将其读入缓冲区,并检查它。

如果用户按下ENTER,则为第一个字符缓冲区将为null:'\ 0',这是你检查的第一件事。

如果不是,请使用sscanf而不是scanf_s处理缓冲区作为逗号分隔值并推进指针缓冲区依次提取每个数字/>


哦,不要做你喜欢的事情 - 你没有上限所以在某些时候 mints 数组将耗尽空间 - 您应该检查 i 值作为循环的一部分而不是只是让你的应用程序在空间不足时崩溃。
Don't read it directly into the mints array - read it into a buffer first, and check that.
If the user presses ENTER, the first character of the buffer will be null: '\0' and that is the first thing you check.
If it isn't, process the buffer as comma separated values using sscanf instead of scanf_s and advancing your pointer through the buffer to extract each number in turn.

Oh, and don't do things like you show - you have no upper limit on i so at some point the mints array is going to run out of space - you should be checking the i value as part of your loop rather that just letting your app crash when it runs out of space.


这篇关于我想要你的帮助(在C中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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