如何在我的变速器必须不是角色的情况下放入 [英] How do I put in while condition that my viariable must be not a character
问题描述
你好;
我有一些小麻烦找到一种方法来处理我的条件下我的变量int必须不是从请求的输入中获取的字符在我的周期之前。
虽然循环用于检查输入中写入的变量是否与算法所需的条件不匹配。
我尝试过:
这里是一个代码示例:
#include< stdio。 h>
int main(){
int n;
scanf(%d,& ; n);
while(n< = - 1){
printf(不正确。写一个正整数。);
scanf(%d,& n);
}
返回0;
}
这适用于数字,但如果在输入中我写了一个像g这样的字符,它就会松开,我必须控制+ c,阻止它。
Hello;
I am having some little troubles finding out a way to put under my while condition the fact that my variable which is an "int" must be not a character took from an input requested before my while cycle.
While cycle is used to check if the variable wrote in input doesnt match with conditions i need for the algorithm.
What I have tried:
here is an example of code:
#include <stdio.h>
int main(){
int n;
scanf("%d", &n);
while(n<=-1 ){
printf("Incorrect. Write a positive integer.");
scanf("%d", &n);
}
return 0;
}
This is working for numbers, but if in input i write a character like "g" it loopes and i must control+c, blocking it.
推荐答案
你标记了这个C#,但它我显然是C或C ++。
然而,这是一个非常简单的问题需要循环解决:
You tagged this C#, but it is clearly C or C++.
However, this is quite a simple problem to resolve with a loop:
int n;
while (scanf("%d", &n) == 0 || n < 0) // if no values captured, or the result is negative
{
printf("Incorrect. Write a positive integer.");
fseek(stdin,0,SEEK_END);
}
这篇关于如何在我的变速器必须不是角色的情况下放入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!