Search code examples
c++do-whilestl-algorithm

How do I get out of this do-while loop?


I'm trying to create a program that intakes a string of characters, verifies it, then sorts it and prints it out.

I'm sure there is a glaring logic error in here somewhere, can someone help point it out? I've spent hours staring at my screen. I tried everything I know in my limited knowledge of C++, but I still can't get the thing working.

Anything you can offer will help me in some way, even if it's condescending.

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

I'm almost sure the problem with the verification lies in this line:

if(! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) )

I'm sure my bools are wrong as well.

Anything, anything at all, I've wasted several hours of my life banging my head against the wall because of this.


Solution

  • (invalid==false); Should be invalid=false;