-3

I have the following code which simply takes a string and find each character's index in the alphabet.

void encrypt()
{
    string alpha = "abcdefghijklmnopqrstuvwxyz";
    string word;
    vector<char> temp;
    char a, b;


    cout << "Enter string to encrypt: \n";
    cin >> word;

    for (int i=0; i<word.length(); i++)
    {
        bool t = false;
        a = word[i];
        for (int j=0; j<alpha.length(); j++)
        {

            b = alpha[j];
            if (a == b)
            {
                cout << a << "'s index = " << j+1 << endl;
                t = true;
            }
        }
        if (t == false)
        {
            cout << "space here\n";
        }
    }

}

when i input a word/string with no space the code works fine but when i input a string with a space the program goes into an infinite loop.

edit main() added due to request:

main()
{
    int a;
    bool b = false;

    while (b == false)
    {
        cout << "1. Encrypt a string\n";
        cout << "2. Decrypt a string\n";
        cout << "3. Exit\n";
        cout << endl;
        cin >> a;
        cout << endl;

        if (a == 1)
        {
            encrypt();
        }
        else if (a == 2)
        {
            decrypt();
        }
        else if (a == 3)
        {
            b = true;
        }
    }
    return 0;
}
nanjero echizen
  • 241
  • 1
  • 5
  • 14
  • I cannot reproduce this by calling your function exactly once from `main`. Please show a full minimum example with main. – merlin2011 Aug 17 '16 at 05:27
  • Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). `cin >> word` will read only until the first whitespace after first non-whitespace character. How and where did you input a string with a space ? – MikeCAT Aug 17 '16 at 05:27
  • @merlin2011 main() added – nanjero echizen Aug 17 '16 at 05:31
  • @nanjeroechizen You have on infinite loop `bool b = false` and then `while b == false`, which you never set to `true`. Please post a minimal example – Bernhard Aug 17 '16 at 05:31

3 Answers3

1
cin >> word;

will read only the first word and leave the second word in the input stream. After that, the call

cin >> a;

will result in an error unless the second word starts with a number. Once the program enters a state of error, nothing is read and the program stays in a loop.

To diagnose problems like these, always check the state of the stream after a read operation.

if ( cin >> word )
{
   // Use word
}
else
{
   // Deal with error.
}


if ( cin >> a )
{
   // Use a
}
else
{
   // Deal with error.
}

To address your real problem, don't use operator>> to read space separated string. Use getline (and use a variable name different from word).

std::string str;
if ( getline(std::cin, str) )
{
   // Use str
}
else
{
   // Deal with error.
}

However, in order to use getline successfully, you have to make sure that after a is read, you ignore the rest of the line. Otherwise, the rest of the line will be read by getline.

if ( cin >> a )
{
   // Ignore rest of the line
   cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   // Use a
}
else
{
   // Deal with error.
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You can check whether cin is accepting the space separated string completely, by doing a cout instantly after the cin. If cin is not accepting the space separated string, then try using getline

Issue resolved:

Use the following:

cout << "Enter string to encrypt: ";
scanf(" %[^\n]s",word);

for (int i=0; word[i]!='\0'; i++)
{

use

include <cstdio>

Hope this solves the problem!! I will get back to you with the solution using string..

User_Targaryen
  • 4,125
  • 4
  • 30
  • 51
0

Replace cin >> word; with getline(cin, word);. It will accept a line as input. Which will resolves your input containing spaces.

As far as infinite loop concern, clear the error bits on the stream cin.clear();

Shravan40
  • 8,922
  • 6
  • 28
  • 48