限制字符串长度和字符串未输出的问题

时间:2015-07-16 04:16:20

标签: c++

首先,我的代码到目前为止

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Car
{
   public:
      void setUp(string, int, string, bool, string);
      void output();
   private:
      string reportingMark;
      int carNumber;
      string kind;
      bool loaded;
      string destination;
};

void input (Car *ptr);

int main()
{
   Car *ptrCar = new Car;
   string reportingMark = " ";
   int carNumber=0;
   string kind ="business";
   bool loaded= true;
   string destination =" ";
   Car *ptr = new Car;
   input(ptr);
   ptr->setUp(reportingMark, carNumber, kind, loaded, destination);
   ptr->output();
}

void input (Car *ptr)
{
   string reportingMark;
   int carNumber;
   string kind;
   bool loaded;
   string destination;

   cout << "Please input your AAR reporting mark" << endl;
   cin >> reportingMark;
   do
   {
      if (reportingMark.length() <2 || reportingMark.length() >4);
      {
         cout << "Invalid. Please try again."<< endl;
         cout << reportingMark.length();
         cin >> reportingMark;
      }
   }while(reportingMark.length() >= 2 || reportingMark.length() <= 4);

   cout<< reportingMark << endl;
   cout<< "Please input your car number." << endl;
   cin >> carNumber;
   cout << carNumber<<endl;
   cout << "What kind of car is it?" << endl;
   cin.ignore();
   getline(cin,kind);
   cout << kind << endl;
   cout <<"Is your car loaded? (1 - yes or 0 - no)" <<endl;
   cin >> loaded;
   cout << loaded << endl;

   if(loaded == 0)
   {
      cout << "Do you have a destination? If so, where? If not, type NONE" << endl; 
      cin.ignore(); 
      getline(cin,destination);
   }else if (loaded == 1)
   {
      cout << "Where is your destination?" << endl;
      cin.ignore();
      getline(cin,destination);
      cout << destination << endl;
   }

}

void Car::setUp(string rMark, int cNumber, string cKind, bool cLoaded,    
                string dest)
{
   reportingMark = rMark;
   carNumber = cNumber;
   kind = cKind;
   loaded = cLoaded;
   destination = dest;

}

void Car::output()
{
   cout << "AAR Reporting Mark:" <<  reportingMark << endl;
   cout << "Car Number:" << carNumber << endl;
   cout << "Kind:" << kind << endl;
   cout << "Your car is:" << loaded << endl;
   cout << "Destination:" << destination << endl;
}

我正在努力的具体是我的实验室要求

  

名为reportingMark的字符串,包含两到四个字符

当字符串中的字符数不是2-4时,我输入的每个输入都会给我一个无效选项。即使我尝试输入2-4个字符。

我的另一个问题是“目的地”我给出的输入没有正确输出我的输入,它只是出现在我的int main中的空白区域。

1 个答案:

答案 0 :(得分:0)

do-while循环中有几处错误。

do
{
   if (reportingMark.length() <2 || reportingMark.length() >4);
   // The ; in the above line is the end of the if statment.
   // The following block of code gets executed no matter what

   {
      cout << "Invalid. Please try again."<< endl;
      cout << reportingMark.length();
      cin >> reportingMark;
   }
}while(reportingMark.length() >= 2 || reportingMark.length() <= 4);

while中的条件也不正确。它应该与if语句中的那个相同。

}while(reportingMark.length() < 2 || reportingMark.length() > 4);

您可以使用以下方法删除重复的代码:

while ( true )
{
   cin >> reportingMark;

   // The length is used many times. Might as well use a variable.
   size_t length = reportingMark.length();
   if (length >= 2 && length <= 4)
   {
      break;
   }
   cout << "Invalid length. Please try again."<< endl;
   cout << length << endl;
}