Input Help

Started by
14 comments, last by Zahlman 18 years, 8 months ago
Hey guys, i have a little problem which is really frustrating. I have an array i called plaintext[20] it is of type char. so i have: char plaintext[20] anyway, i want to prompt the user to enter a word(in which i want to manipulate the letters). Anyway, i have cin>>plaintext, to read in, but have a problem. If i was to enter "playfair cipher" or any 2 words, it cuts the last word off due to the space. How do i ignore spaces in the input. I searched google, and got cin>>skipws>>plaintext, this doesnt work. How could i ignore spaces in order to stick two words into one long word.
Advertisement
#include <iostream>#include <string>using namespace std;int main(){    string text;    getline(cin, text, '\n');    cout << "you typed: " << text;    cout << "first letter: " << text[0];    return 0;}
"It's better to regret something you've done than to regret something you haven't done."
bikola_p, you will need to use cin.getline(). Here is a quick snippet ripped strait from the Visual C++ docs:

cin.getline( char *Storage, int MaxChars, char Terminating);

Storage - Where you want the input stored
MaxChars - The maximum number of characters to retrieve before it gives up (you should set this to the size of your char array)
Terminating - The character, which, when encountered, will terminate the retrieving process prematurely.



#include <iostream.h> /*Standard IO header*/int main()/*Begin main()*/{   char line[100];/*Your string*/   cout << " Type a line terminated by 't'" << endl;/*Promp the user*/   cin.getline( line, 100, 't' );/*get the user input*/   cout << line;/*print what the user entered*/return 0;/*return 0, and end main()*/}
ok, thanks, but not exactly what i was loking for. The thing is i need to store the text into a array of char's, rather then a single string. I have finished writing my whole program, but realised that i didnt take into consideration, if the user enters 2 words
What are you trying to do? Does an array of chars have anything special you need that a string can't provide?
"It's better to regret something you've done than to regret something you haven't done."
here's a slighty modified version of liam666's code which would do the trick

#include <iostream>using namespace std;int main(){   char line[100];   cout << " Type a line " << endl;   cin.getline( line, 100, '\n' );   cout << line;   return 0;}
"It's better to regret something you've done than to regret something you haven't done."
well yeah, because, im trying to construct a 5 * 5 matrix with the english alphabet and the word entered, then using the array positiions, the letters in the word are substituted with others.
ok,everything mentioned works, but not what i want. simply, i want to Take the space out, sorry for not mentioning earlier,but if i was to enter "grey wall", the array should store it as greywall, at the moment, the way you guys have sampled for me the array stores it as "grey wall", rather then greywall, which i want.
can't you just write a for-loop to do this?

you're prolly looking for a better solution that that though :/
"It's better to regret something you've done than to regret something you haven't done."
ok, simply is there a way to read in and ignore spaces. I can do it from a text file. but directly from the user doesnt work.

This topic is closed to new replies.

Advertisement