Visual C++ - My while 循环不起作用



我想检查不能大于 2 和小于 1 以及不能是字符的用户输入(因为输入类型是 int),但我的代码似乎不起作用.....有人可以帮助我吗?我尝试了各种方法,但似乎我仍然明白更新:这里是部分代码,源在上一部分声明为int

Student stu;
List<Student> list;
char id[10];
string str;
int choice;
int source;
bool ask;
while(true){
switch(menu()) {
case 1:
{
    system("cls");
    if (ReadFile("student.txt", &list)) {
        cout << "nRead File Successfully!n";
    }
    else
        cout << "nRead File Failed!n";
    system("pause");
    break;
}
case 2:
{
    system("cls");
    if(list.empty()){
        cout<<"nThe list is empty!n";
        break;
    }
    cout<<"nStudent id: ";
    getline(cin,str);
    while(!isdigit(str)){
        cout<<"nEnter [x] back to menu or re-enter the student id: ";
        getline(cin,str); 
        if(str=="x"||str=="X"){
            break;
        }
    }
    strcpy_s(id,10,str.c_str());
    if(DeleteRecord(&list, id)){
        cout<<"nStudent successfully deleted!n";
    }
    else
        cout<<"nDelete student record failed!n";
    system("pause");
    break;
}
case 3:
{
    system("cls");
    if(list.empty()){
        cout<<"nThe list is empty!n";
        break;
    }
    cout<<"nStudent id: ";
    getline(cin,str);
    while(!isdigit(str)){
        cout<<"nEnter [x] back to menu or re-enter the student id: ";
        getline(cin,str);
        if(str=="x"||str=="X"){
            break;
        }
    }
    strcpy_s(id,10,str.c_str());
    if(SearchStudent(&list, id, stu)){
        cout<<"nStudent record found!n";
    }
    else
        cout<<"nStudent record not found!n";
    system("pause");
    break;
}
case 4:
{
    system("cls");
    /*if(InsertResult("exam.txt",&list))    //Call InsertResult function
            cout<<"*INSERT RESULT SUCCESSFULLY!"<<endl;
        else
            cout<<"*INSERT RESULT FAILED!"<<endl;
    system("pause");
    break;
}
case 5:
{
    system("cls");
    /*if(InsertSubject("subject.txt",&list))    //Call InsertSubject function
            cout<<"*INSERT SUBJECT SUCCESSFULLY!"<<endl;
        else
            cout<<"*INSERT SUBJECT FAILED!"<<endl;*/
    system("pause");
    break;
}
case 6:
{
    system("cls");
    cout<<"nWhere Do You Want To Display The Information?"<<endl;
    cout<<"n1.Screen."<<endl;
    cout<<"n2.File."<<endl;
    cout<<endl;
    cin>>source;
    //check the input
    while(!isdigit(source)||source<1||source>2)
    {
        cout<<"nPlease Enter A Valid Number Of Source!"<<endl;
        cin>>source;
        cout<<endl;
    }
    cout<<"nWhich Information Do You Want To Display?"<<endl;
    cout<<"n1.Student Information."<<endl;
    cout<<"n2.Student Information & Past Exam Result."<<endl;
    cout<<"n3.Student Information & Current Subject Taken."<<endl;
    cout<<"n4.Student Information & Past Exam Result & Current Subject Taken."<<endl;
    cout<<endl;
    cin>>choice;
    //check the input
        if(!isdigit(choice) || choice<1 || choice>2)
    {
        cout<<"nPlease Enter A Valid Number Of Choice!"<<endl;
        cin>>choice;
        cout<<endl;
    }
    Display(&list,choice,source);
    system("pause");
    break;
}
case 7:
{
    system("cls");
    cout << "nThank You For Using The Program!n";
    system("pause");
    return 0;
}
}
}
cout << endl;
system("pause");

}

isdigit 希望你向它传递一个字符变量。我提供的链接有一个示例:

/* isdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
  char str[]="1776ad";
  int year;
  if (isdigit(str[0]))
  {
    year = atoi (str);
    printf ("The year that followed %d was %d.n",year,year+1);
  }
  return 0;
}
您不需要使用

isdigit,因为您已经在使用 int 变量。因此,这是您需要的代码:

case 6:
{
    system("cls");
    cout<<"nWhere Do You Want To Display The Information?"<<endl;
    cout<<"n1.Screen."<<endl;
    cout<<"n2.File."<<endl;
    cout<<endl;
    cin>>source;
    //check the input
    while (source < 1 || source > 2)
    {
        cout<<"nPlease Enter A Valid Number Of Source!"<<endl;
        cin>>source;
        cout<<endl;
    }
 }

不能大于 2

source > 2

且小于 1

猜测你的意思是"不能小于 1"source < 1

演示:

#include <iostream>
#include <ctype.h>
using namespace std;
int main(){
    char source;  // assuming source is char!!
    cin>>source;
while(source < '1' || source > '2' || (!isdigit(source)) )
{
    cout<<"nPlease Enter A Valid Number Of Source!"<<endl;
    cin>>source;
    cout<<endl;
}
return 0;
 }

考虑像这样更改代码

while(!isdigit(source) || source < '1' || source > '2'))
    {
        cout<<"nPlease Enter A Valid Number Of Source!"<<endl;
        cin>>source;
        cout<<endl;
    }

试试这个做边循环 条件,

   do{
      cout<<"nPlease Enter A Valid Number Of Source!"<<endl;
      cin>>source;
      cout<<endl;
     }while(source<1 || source>2 || !isdigit(source));

编辑

char digit; // holds only one char or one digit (0 1 2 3 ...9)
int source;
cin>>digit; 
source=digit-'0'; // char to int
do{
      cout<<"nPlease Enter A Valid Number Of Source!"<<endl;
      cin>>source;
      cout<<endl;
  }while(source<1 || source>2 || !isdigit(digit));//digit is character

isdigit(arg)函数中,传递char参数其工作正常。

相关内容

  • 没有找到相关文章

最新更新