简体   繁体   English

使用scanf读取整数或字符串

[英]Using scanf to read an integer or a string

How can I use scanf to read either an int or a string? 如何使用scanf读取int或字符串?

What I'm trying to do is: 我想要做的是:

printf("Enter Your Guess, 0 - 100: "); /* Input your guess */

scanf("%as", &pass);

printf("%s", pass);

In this program the player can either chose to enter a number or pass how can I scan either the number or the word pass. 在此程序中,玩家可以选择输入数字或通过如何扫描数字或单词通行证。 Is there and if statement I can put in or something I've been going into this for like 1 hour 是否存在,如果声明我可以放入或者我已经进入过这样的事情,持续1小时

扫描一个字符串,并确定它是否完全由一个数字的合法符号构成,或者该字符串是否显示为"pass" ,如果是一个数字,则可以将其转换为您选择的类型。

好吧,解决方法是简单地一直读取字符串并使用atoi或snprintf将所需的字符串转换为数字,如果它不包含除[0-9]之外的其他字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM