如何摆脱这个do-while循环?

-5 c++ do-while stl-algorithm

我正在尝试创建一个插入一串字符的程序,验证它,然后对其进行排序并将其打印出来.

我确定这里有一个明显的逻辑错误,有人可以帮忙指出来吗?我花了好几个小时盯着我的屏幕.在我对C++的有限知识中,我尝试了所有我知道的东西,但是我仍然无法使用它.

你能提供的任何东西都会以某种方式帮助我,即使它是居高临下的.

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

void mySort(string &s);

int main()
{
    string str;
    char c;
    bool invalid = true;

    cout<<"Please enter some alphabetical characters:"<<endl;
    cout<<"(* to end input): ";

    do
    {
      getline(cin, str, '*');

      for(int i = 0; i < str.length(); i++)
      {
        c = str.at(i);
      }

      if(! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) )
      {
         cout<<"Error!"<<endl;
      }
      else
      {
        (invalid==false);
        cout<<"You entered: "<<str<<endl;
        mySort(str);
      }
    } while(invalid==true);

    system("PAUSE");
    return(0);
}

void mySort(string &s)
{
    sort(s.begin(), s.end());
    cout<<"The string after sorting is: "<<s<<endl;
}
Run Code Online (Sandbox Code Playgroud)

我几乎可以肯定验证的问题在于这一行:

if(! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) )
Run Code Online (Sandbox Code Playgroud)

我确定我的bool也是错的.

任何东西,任何东西,因为这个原因,我浪费了几个小时的时间撞到了墙上.

Pau*_*oub 5

你永远不会设置invalid任何东西true.

这一行:

(invalid==false);
Run Code Online (Sandbox Code Playgroud)

应该:

invalid = false;
Run Code Online (Sandbox Code Playgroud)

前者的版本进行比较 invalidfalse,然后扔掉比较的结果.没有什么变化.