Stuck on infinite loop

江枫思渺然 提交于 2020-05-17 08:21:06

问题


noob programmer here. Taking my first CS class in college and making first post on here so excuse me if the info i provide is not sufficient in advanced.

Still trying to figure out loops. Seem to get it but once there is loops within loops or if statements inside loops, I get thrown off and have no idea on how to proceed. For my assignment, I need the following to occur.

Would you like to process all the records in the file? (y/n) W Please enter either y or n. Would you like to process all the records in the file? (y/n) n Enter number of records to process: two XXXXXXXXXX Error-non numeric or negative value, try again Enter number of records to process: 10

Here is my code:

char a = 0;             //User chooses Y or N
int ProcessAmount = 0;  //Amount of times to process if not all

  cout << "Would you like to process all the records in the file? (y/n) ";
  cin >> a;

  do{

    bool notDone = true;

      if(a  == 'n'){
        while(notDone){
              cout << "Enter records to process: ";
              cin >> ProcessAmount;

              if (cin.fail()){
                  cin.clear();
                  cin.ignore(40,'\n');
                  cout << "" << endl;
              }
              else{
                  notDone = false;  
              }
        }
      }else if(a != 'y' or a != 'n');
            cout <<"Please enter either y or n." << endl;

  }while( a != 'y');  

回答1:


Most problems are explained in comments, here is how I would fix it:

char a = 0;             //User chooses Y or N
int ProcessAmount = 0;  //Amount of times to process if not all

cout << "Would you like to process all the records in the file? (y/n) ";
cin >> a;

while (a != 'y') {

  bool notDone = true;

  if(a  == 'n'){
    while(notDone){
      cout << "Enter records to process: ";
      cin >> ProcessAmount;

      if (cin.fail()){
          cin.clear();
          cin.ignore(40,'\n');
          cout << "" << endl;
      } else {
        notDone = false;
      }
    }
  } else if(a != 'y' or a != 'n') {
    cout <<"Please enter either y or n." << endl;
    cin >> a; // Need to get new input because old one is invalid.
  }

};

Also I don't see how notDone is used. Also I would strongly advise of using proper indentation, spaces around keywords as while, for, if, else as it is good style.




回答2:


You just put the y/n solicitation out of your loop then 'a' won't never change its value. Take a look of the change you may want:

do {
    cout << "Would you like to process all the records in the file? (y/n/f) ";  //f to break the loop
    cin >> a;

    bool notDone = true;

    if (a  == 'n') {
        //. . .
    } else if (a == 'y') {
        //You may want to do something when yes
    } else if (a != 'f')
              cout <<"Please enter either y/n or f." << endl;

} while( a != 'f')


来源:https://stackoverflow.com/questions/60576144/stuck-on-infinite-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!