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

125 views Asked by At

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.

4

There are 4 answers

0
Dithermaster On BEST ANSWER

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

0
Paul Roub On

You never set invalid to anything but true.

This line:

(invalid==false);

should be:

invalid = false;

The former version compares invalid to false, then throws away the result of the comparison. Nothing changes.

0
shooper On

First change:

(invalid == false);
invalid = false;
0
Remy Lebeau On

As others have said, you are not assigning the invalid variable correctly. You are also not validating the input string correctly, either. You loop through the entire string, and then validate only the last character seen, rather than validating each character while looping.

I would suggest re-writing the loop to get rid of the invalid variable and fix the validation, eg:

int main()
{
    string str;
    char c;

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

        if (!getline(cin, str, '*'))
            break;

        if (str.empty())
            cout << "You did not enter anything!" << endl;

        else if (str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") != string::npos)
            cout << "Error! Bad input" << endl;

        else
        {
            cout << "You entered: " << str << endl;
            mySort(str);
            break;
        }
      }
    }
    while (true);

    system("PAUSE");
    return 0;
}