Feb 15, 2017 at 1:23am UTC
Hi, I have a question about making spaces in message_a.
For example if I enter 'hi my name is' for message, then message_a will print out -------------, however I want there to be spaces when present in the message so ideally it would look something like:
hi my name is
-- -- ---- --
Could anybody point me in the right direction on how to do this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message;
cout << "What is your message? " ;
getline (cin, message);
message.size();
string message_a(message.size(), '-' );
cout << "message is: " << message << endl;
cout << "message_a is: " << message_a << endl;
}
Last edited on Feb 15, 2017 at 1:28am UTC
Feb 15, 2017 at 1:59am UTC
when spaces are present, like they are there in the message.
for example:
'hi my name is'
has 3 spaces present, one between 'hi' and 'my', another between 'my' and 'name' and another one between 'name' and 'is'
Feb 15, 2017 at 2:05am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
int main()
{
std::string myString{};
std::cout << "Enter your string \n" ;
getline(std::cin, myString);
for (auto & elem : myString)
{
if (elem != ' ' )
{
elem = '_' ;
}
}
std::cout << myString << '\n' ;
}
Last edited on Feb 15, 2017 at 2:06am UTC
Feb 15, 2017 at 2:09am UTC
Ah thanks man, that worked perfectly for what I needed once I tweaked it a little for my code.