0

this is a piece of code I've made that asks the user to input a string of lowercase letters only, if not they'd have to input it again. Then, it scans through the string and outputs the amount of letters used in the string and the amount not used. It works perfectly and exactly the way I wanted it to...except when the string has a space in it. So for example, if the string is "asd fg" the output will be "3 letters used, 23 not used" when we can clearly see it needs to be "5 letters used, 21 not used." How do I fix this?

#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string inp;
    int l_inp, i, un, in, ln;
    un=0;
    ln=26;
    char alpha;
    bool flag;
    flag=false;
    do
    {
        cout<<"Please Enter String of Lowercase Letters: ";
        cin>>inp;
        i=0;
        while (inp[i]>=97 && inp[i]<=122)
        i++;
        if (inp[i]=='\0')flag=true;
        l_inp=0;
        if (inp[l_inp]=='\0')flag=false;
    }
    while (flag==false);
    for (alpha='a';alpha<='z';alpha++)
    {
        for (in=0;inp[in]!='\0';in++)
        {
            inp[in];
        }
        std::size_t found = inp.find(alpha);
        if (found!=std::string::npos)
        {
            un++;
            ln--;
        }
    }
    cout<<" "<<un<<" letters are used in the string"<<endl;
    cout<<" "<<ln<<" letters are not used in the string"<<endl;
    return 0;
}
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Please format your code. It's impossible to read minimized. – Carcigenicate Dec 15 '15 at 18:56
  • Output the string you read into `inp`, then go and read about how `>>` extracts values from a stream. – molbdnilo Dec 15 '15 at 19:04
  • The first step is to check whether you actually read anything at all. The next step depends on your preference: you can read a line (using `std::getline()`) or change the stream to not consider space space (and only consider `'\n'` to be space). The latter approach is more interesting but also a lot more involved. – Dietmar Kühl Dec 15 '15 at 19:07

0 Answers0