當前位置:首頁 » 編程語言 » c語言提示輸入數據不符合要求
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言提示輸入數據不符合要求

發布時間: 2023-01-02 20:01:38

c語言問題。輸入數據要符合要求,不符合提示錯誤並重性輸入

/*
092134179x:輸入錯誤,繼續輸入,'#'結束
21344009764x:輸入錯誤,繼續輸入,'#'結束
09213417921344009764
Press any key to continue
*/
#include <stdio.h>
#include <conio.h>
int main()
{ char ch = 0,s[36];
int i = 0;
while(1) {
ch = getch();
if(ch >= '0' && ch <= '9' && i < 35) {
s[i++] = ch;
putchar(ch);
}
else if(ch == '#') {
s[i++] = '\0';
break;
}
else {
printf("x:輸入錯誤,繼續輸入,'#'結束\n");
continue;
}
}
printf("%s\n",s);
return 0;
}

㈡ c語言中數據輸入錯誤了如何提示輸入錯誤並實現要求重新輸入

cin>>sth;
while(!check(sth))
{
//check
函數檢查輸入值,如果滿足就不需要在輸入,這是就返回1或者true
cout<<"enter
again,
because
it
is
invalidate"
cin>>sth;
}

㈢ C語言中怎樣實現當輸入的數據不符合要求時給出提示並要求重新輸入

#include <iostream>

using namespace std;

void main()

{
int a,b,c,max;

cout<<"請輸入\n";

while(!(cin>>a>>b>>c))
{
cin.clear(); //這是關鍵
cin.ignore(); //這是關鍵

cout<<"數據格式有誤 \n";

}
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
cout<<"最大值為"<<max;

}

㈣ C語言(判定輸入數是否符合要求)

#include<stdio.h>
intmain()
{
intb;
printf("請輸入一個大於或等於3的整數:");
scanf("%d",&b);
if(b<3){
printf("你輸入的數值不符合題目要求,請重新輸入! ");
}
elseprintf("OK,b=%d ",b);
}

㈤ C語言輸入的字元串不符合條件,怎麼提示錯誤並重新輸入循環要怎麼加上去

寫一個函數(例如int hf(char*s);),判斷字元串是否符合要求,不符合要求就循環,一直到符合要求為止。
循環的方式多種多樣,例如下面這一種:
while(scanf("%s",&name)&&hf(name));