Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

l3mon

help with getting spaces from string

This topic is 5797 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

Hi! I finally got around to ask for some help myself... I am currently working on a program that converts letters to morse code and vice versa. So far everything works fine but one thing. I would like to ''read'' spaces and put them back as spaces. When I put a space they are read as ''\0'' which doesn''t help a lot. I found this piece of code "cin.unsetf(ios:skipws)" but it won''t work for me says I''m not allowed to use ''ios'' in this matter. I am currently using #include <iostream.h> #include <string.h> Oh while we are at it... how do I convert all input to uppercase? If you could help me on this matter I''d apreciate it

Share this post


Link to post
Share on other sites
Advertisement
Don't use iostream.h, it's deprecated (out of date). Use #include <iostream>

That might fix your problem - I'm not too experienced with iostreams I'm afraid.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions


[edited by - siaspete on December 1, 2002 7:00:31 AM]

Share this post


Link to post
Share on other sites
If I do that, my compiler goes nuts, which it actually shouldn''t, so I''ll look into it, but it doesn''t look like a solution.

Thanks anyway

Share this post


Link to post
Share on other sites
Right ...

cin >> myStringVariable;

This reads input until a whitespace is encountered (space / enter). So if you type "Hello String Variable" only "Hello" will be inserted into myStringVariable, leaving "String Variable" in the input queue.

To read spaces you have to use something that determines \n as the end of input, instead of whitespaces. For the istream, there are a couple of member functions that can do this for you, yet I think this will be the easiest to start off with:

cin.getline(pointerToChar, size);

The first argument is the variable you want input to insert into, and the second argument is a limit on the number of characters to be read (for example, if you pass 30 as the argument, it reads no more than 29 characters and then plonks the null character at the end).

So, with ''string'', I think you would call it like so:

cin.getline(myStringVariable.c_str(), limitingFactor);

I''m pretty sure that this will work, but I haven''t much experience with the string class

As for converting to uppercase, include cctype (#include <cctype>).
toupper(char) takes a lowercase character and returns the uppercase version of it. If it is not lowercase, it returns the argument unaltered. For example, it could be used like so:

char upperCaseVersion = toupper(lowerCaseCharacter);

There might be some way of converting an entire string in the string class, but I don''t know. Maybe for converting to uppercase, you could just loop through a the characters in a string variable and set each one using toupper() ... ask me if you need help, I''m bored

Good luck


Lektrix
[ Google || ACCU || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
quote:
Original post by l3mon
If I do that, my compiler goes nuts, which it actually shouldn''t, so I''ll look into it, but it doesn''t look like a solution.
you should put using namespace std; somewhere after the includes, or accessing the functions like std::cout >> "something";


Share this post


Link to post
Share on other sites

    
#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

int main(void)
{
string input;
getline(cin, input);
transform(input.begin(), input.end(), input.begin(), toupper);
cout << input;
}


[edited by - smart_idiot on December 1, 2002 12:36:07 PM]

Share this post


Link to post
Share on other sites

  • 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!