I've tried cin >> userInput; and it stops reading the word after it hits a space. but then I tried cin.get and it just skips all the other cins. Then I tried cin.getline and it skips over the userInput cin and goes right to the next cin (cin >> direction).

What am I doing wrong? How do I get user input and read it in as char array. I'm not allowed to use strings so they must be C-style. Thanks!

void encode(){
    char userInput[100], direction[9], keyword[15];
    int transposition;
    cout << "Enter the text to be encoded: ";
    cin.get(userInput, 99);
    cout << userInput;
    cout << "Enter direction (Forwards or Reverse): ";
    cin >> direction;
    cout << "Enter transposition value (0...25): ";
    cin >> transposition;
    cout << "Enter a keyword for Vigenere encryption: ";
    cin >> keyword;

}

Recommended Answers

All 2 Replies

Are you sure it is not working??? I just copy your code and paste it to my visual studio project (win32 application), and it works.

Does it show any error or not?

void encode()
{
    char userInput[512], direction[32], keyword[32];
    int transposition;

    cout << "Enter the text to be encoded: ";
    cin.get( userInput, sizeof(userInput) ) ; // unformatted input

    cout << "Enter direction (Forwards or Reverse): ";
    cin.get( direction, sizeof(direction) ) ; // unformatted input

    cout << "Enter transposition value (0...25): ";
    cin >> transposition; // formatted input
    cin.ignore( 1000, '\n' ) ;

    cout << "Enter a keyword for Vigenere encryption: ";
    cin.get( keyword, sizeof(keyword) ) ; // unformatted input
}

See: http://www.cplusplus.com/forum/general/69685/#msg372532

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.