0

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;

}

Edited by Carc369

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by vijayan121
0

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?

0
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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.