help with getting spaces from string

Started by
4 comments, last by l3mon 21 years, 4 months ago
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
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]
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
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 ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
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";


    #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]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement