Originally Posted by
Therry
Sorry guys, I got the answer that I was looking for. Instead of pressing return after each >> just use a space until the last input like:
Code:
x [space] x [space] x [space] x {enter]
and it will print x x x x, just what I wanted. thanks for all your help.
Yet your code above is not safe.
Using spaces may work in your current context, but may fail later on when you try to use it in a different project. Say for file I/O as an example.
You should study the use of getline thoroughly. It can be a powerful asset with input and file I/O.
In your situation you should take the input as a single line as you have with getline.
i.e.
Code:
getline( std::ifstream, std::string, Delimiter Character );
then parse the input in a function
Code:
// Eynhallow.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
std::string FormatString(const std::string& Src)
{
std::string ret_val;
std::string::const_iterator it = Src.begin();
while ( it != Src.end() )
{
/* With this switch statement you can add other characters that you want removed from your strings */
switch ( (int)(*it) )
{
case '\n':
break;
default:
ret_val += (*it);
break;g
}
++it;
}
return ret_val;
}
int main(int argc, char* argv[])
{
/* Get Input from user */
std::string original;
std::getline(std::cin, original, '\n');
/* Make a formatted string from the original */
std::string Test = FormatString(original);
std::cout << "Original String: " << original << '\n';
std::cout << "FormatString(): " << FormatString(original) << std::endl;
cin.get();
return 0;
}