Jump to content
  • Advertisement
Sign in to follow this  
bikola_p

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.

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!