C语言如何控制输出11位手机号码,不能多不能少,能控制第一位数字是1就更好了,录入信息用的,大学C语言课设作业,急求!!!弄不完回不了家😭
#include <iostream>
using namespace std;
int getTel(char *tel)
{
char ch;
int n = 0;
while (n != 11)
{
cin >> ch;
if (ch >= '0' && ch <= '9')
{
if (n == 0 && ch != '1')
{
cout << "手机号第一位必须是1" << endl;
fflush(stdin);
continue;
}
*(tel + n) = ch;
}
else
{
cout << "手机号必须是数字" << endl;
fflush(stdin);
n = 0;
continue;
}
n++;
}
*(tel + n) = '\0';
return n;
}
int main()
{
char tel[12] = {0};
int n;
while (1)
{
n = getTel(tel);
if (n == 11)
{
cout << tel << endl;
break;
}
else
cout << "输入错误,请重新输入!\n" << endl;
}
return 0;
}
循环getch()啊
#include <conio.h>
void main()
{
int n = 0;
char tel[12] = {0};
char c = getch();
putch(c);
while(1)
{
if(c<'0' || c>'9')
{
printf("手机号必须是数字\n");
c = getch();
putch(c);
continue;
}
if(n==0)
{
if(c != '1')
{
printf("第一位必须是1\n");
c = getch();
putch(c);
continue;
}
}
tel[n++] = c;
if(n==11)
break;
c = getch();
putch(c);
}
printf("tel=%s",tel);
system("pause");
}