user2810493
user2810493

Reputation: 11

Why the second 'cin >>' won't be processed?

I'm new in C++, I have learned Java last year, and immediately in this semester I have to learn cryptography programming using C++ and MFC / API stuffs, many confusing now, anyway, please take a look on the code:

#include <iostream>
#include <string>

using namespace std;

#define strSize 100

int main()
{
    char message[strSize];
    char cipher[strSize];
    char plaintext[strSize];
    int nCount;
    char nEKey, nDKey;

    for(int i=0; i<strSize; ++i)
    {
        message[i] = '\0';
        cipher[i] = '\0';
        plaintext[i] = '\0';
    }

    cout << "Please enter your confidential message:" << endl;
    cin.getline(message, strSize);
    nCount = cin.gcount();
    cout << nCount << " characters read." << endl;

    cout << "Enter secret key:" << endl;
    cin >> nEKey;
    for(int j=0; j<nCount-1; ++j)
        cipher[j] = message[j] + nEKey;
    cout << "Message is encrypted into:" << endl << cipher << endl << endl;

    cout << "Type your secret key again:" << endl;
    cin >> nDKey;

    for (int k=0; k<nCount-1; ++k)
        plaintext[k] = cipher[k] - nDKey;
    cout << "Cipher text is decrypted to:" << endl << plaintext << endl;

    return 0;
}

When running the compiled program, the result is:

Please enter your confidential message:
hello world
12 characters read.
Enter secret key:
abc
Message is encrypted into:
袴?望答

Type your secret key again:
Cipher text is decrypted to:
gdkknvnqkc

I have both compiled it with g++ and in visual studio, they produced the same output.

Now my question is: Why the second cin won't be processed?

while in visual studio it gave me a warning says:

...\enc.cpp(25): warning C4244: '=' : conversion from 'std::streamsize' to 'int', possible loss of data

OMD, I'm a newbie, just don't know what its means, can anyone help to fix it?

Lots of thanks!

Upvotes: 0

Views: 125

Answers (4)

4pie0
4pie0

Reputation: 29744

cin >> nEKey; // nEKey = 'a'

reads single character to nEKey. The rest of the input is left in the stream. The next time you read the next character is read:

//...
cin >> nDKey; // nDKey = 'b'

You should ignore the rest of input after reading a single character. Consider use of std::string to read all input into string.

The warning

warning '=':conversion from 'std::streamsize' to 'int',possible loss of data

is caused by possible loss of data when packing larger std::streamsize into int. Use std::streamsize:

std::streamsize nCount = cin.gcount(); 

Upvotes: 1

Silouane Gerin
Silouane Gerin

Reputation: 1251

For the warning from the compiler try casting cin.gcount() into an int, like this.

 nCount = (int)cin.gcount();

You should be certain that the size of the string can be contained in an signed int.

Upvotes: 0

Caduchon
Caduchon

Reputation: 5231

cin >> nEKey; reads the character 'a' and not the string "abc". Then, cin >> nDKey; reads the next character (i.e. 'b').

Upvotes: 1

kevinz
kevinz

Reputation: 200

nEKey and nDKey are both of type char, which mean they can only store one char.

cin >> nEKey read just the a of abc and the later cin >> nDKey read the b which is the next character in the input.

Upvotes: 1

Related Questions