我的while循环不起作用

时间:2016-07-18 10:08:11

标签: visual-c++ while-loop integer

我想检查大于2且小于1的用户输入以及不能为char(因为输入类型为int)但我的代码似乎无法工作.....任何人都可以帮助我吗?我试过各种各样的方式,但似乎我仍然得到它 更新:这里是代码的一部分,source在前一部分中声明为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");

}

4 个答案:

答案 0 :(得分:1)

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;
    }
 }

答案 1 :(得分:0)

  

不能大于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;
 }

答案 2 :(得分:0)

考虑改变这样的代码

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

答案 3 :(得分:0)

尝试这个do-while循环条件,

   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参数传递给它正常工作。