Public Group

Input Help

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
#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;}

Share on other sites
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()*/}

Share on other sites
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

Share on other sites
What are you trying to do? Does an array of chars have anything special you need that a string can't provide?

Share on other sites
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;}

Share on other sites
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.

Share on other sites
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.

Share on other sites
can't you just write a for-loop to do this?

you're prolly looking for a better solution that that though :/

Share on other sites
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.

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• Forum Statistics

• Total Topics
633368
• Total Posts
3011532
• Who's Online (See full list)

There are no registered users currently online

×