🔒 Locked
keyboard input
Advertisement
Original Post
i am having problems getting keyboard input into a char array. The main problem is that when i type say "I am a Programmer" all that is put in the char array is "I". Or if i typed "Program Error" then all that is in my char array is "Program". no space and no second word. How can I do this?
I think i could use strings but i would rather stick with the char array for the input from the user.
Any help will be very much appreciatted!
werdy666
The question is unanswerable since you didn''t provide enough details. Are you using streams/scanf/getch/Windows messages? How about the code that reads input?
I''ll make a guess that you''re trying to read a string from cin, like so:
string s;
cin >> s;
If this is the case, >> will only extract until whitespace. To extract a line, use getline. It should be overloaded for use with std::string as well.
I''ll make a guess that you''re trying to read a string from cin, like so:
string s;
cin >> s;
If this is the case, >> will only extract until whitespace. To extract a line, use getline. It should be overloaded for use with std::string as well.
sorry about lack of info. i will try to explain better!
at the moment i am trying to get what the user inputs from my cin to a whole sentence. getline will work but i want the input to go into my char array.
This is what i have at the moment.
char answer[]="program runnning";
char guess[80]
cout << "What is the answer?"
cin >> guess;
if (*guess == *answer)
{
cout << "wow u got it right!"
}
else cout << "Bad Luck!";
its crude but that is basically what i am doing at the moment. the problem lies with the fact that whitespaces are ignored. strings will work, but how do i take a string and put it into my char array. ie
How can I make - ''String a="a answer"'' and change it into ''guess[80] ="a answer"''??
I hope that makes more sense.
And i will use any commands to get this to work. sscan, getch, cin whatever to make it happen.
Thanks for your quick reply IndirectX!
werdy666
at the moment i am trying to get what the user inputs from my cin to a whole sentence. getline will work but i want the input to go into my char array.
This is what i have at the moment.
char answer[]="program runnning";
char guess[80]
cout << "What is the answer?"
cin >> guess;
if (*guess == *answer)
{
cout << "wow u got it right!"
}
else cout << "Bad Luck!";
its crude but that is basically what i am doing at the moment. the problem lies with the fact that whitespaces are ignored. strings will work, but how do i take a string and put it into my char array. ie
How can I make - ''String a="a answer"'' and change it into ''guess[80] ="a answer"''??
I hope that makes more sense.
And i will use any commands to get this to work. sscan, getch, cin whatever to make it happen.
Thanks for your quick reply IndirectX!
werdy666
cin.getline(guess, 80);
Not good. You can''t compare strings like that. You are doing pointer comparison, and in your context, the result will almost guaranteed will always be false. Use strcmp to compare strings.
Also, to post before some STL advocates: use std::string. :D
std::string str;
getline(cin, str);
quote:
if (*guess == *answer)
Not good. You can''t compare strings like that. You are doing pointer comparison, and in your context, the result will almost guaranteed will always be false. Use strcmp to compare strings.
Also, to post before some STL advocates: use std::string. :D
std::string str;
getline(cin, str);
ok up until this point i load a file and put each word in the file into an array. The main reason for this is that i randomly jumble the letters of the word. Then the user has to guess the correct word. I will teach myself strcmp etc. so thanks for your help.
But can i take my arrays of my words jumble them up, and put both the jumbled and original words into a string from an array?
i mean something like
original[80] = "Program"
jumbled[80] = "mPoagrr"
then convert the "original[80]" into "string original" and then be able to do a strcmp on the guess compared to the "string original"
ie:
string original_word = original[80]; This here won''t work. How do i do it? any hints or tips?
The converting an array to a string is what i need help with!
I hope that makes sense!
But can i take my arrays of my words jumble them up, and put both the jumbled and original words into a string from an array?
i mean something like
original[80] = "Program"
jumbled[80] = "mPoagrr"
then convert the "original[80]" into "string original" and then be able to do a strcmp on the guess compared to the "string original"
ie:
string original_word = original[80]; This here won''t work. How do i do it? any hints or tips?
The converting an array to a string is what i need help with!
I hope that makes sense!
You don''t make a lot of sense. 
You can jumble your string by randomly selecting two letters and swapping them. See rand().
strcmp works on C strings. std::strings can be compared using ==, which is one reason to prefer them over C strings.
I also messed up with if (*guess == *answer). This code will actually compare the first letters of guess and answer.
string original_word = original;
Should work like so. original[80] is the 81-st character in the ''original'' array, which is out of its bounds.
You can jumble your string by randomly selecting two letters and swapping them. See rand().
strcmp works on C strings. std::strings can be compared using ==, which is one reason to prefer them over C strings.
I also messed up with if (*guess == *answer). This code will actually compare the first letters of guess and answer.
string original_word = original;
Should work like so. original[80] is the 81-st character in the ''original'' array, which is out of its bounds.
lol
yeah i am just an aussie! lol not much sense out of me at the best of times! 
But even though you didn''t understand me very well, You helped me tremendously! I have now got it set up so that with spaces or anything i use strcmp to check the original with the guess and it seems to work beautifully.
Thanks again for your help!
Andy
if i ever release the source for beginners like me your name will be in the credits!
But even though you didn''t understand me very well, You helped me tremendously! I have now got it set up so that with spaces or anything i use strcmp to check the original with the guess and it seems to work beautifully.
Thanks again for your help!
Andy
if i ever release the source for beginners like me your name will be in the credits!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement