• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

Not skipping

I want to know if it's possible to use an istream with >> but without skipping white spaces. As an example, if I try

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string s;
  cin >> noskipws >> s;
  cout << s << endl;
  return 0;
}

with "Hello world" I obtain in s "Hello"

I would like to use >> instead of getline, because sometimes I have a string and sometimes some other data type. I would like to do an automatic type conversion.
Thanks.
0
anasagasti
Asked:
anasagasti
1 Solution
 
pmdwCommented:
0
 
anasagastiAuthor Commented:
Sorry, I think I misunderstood the use of cin>>.
What I want is to type for example "MY INFORMATION" and get this string in only one string variable using directly cin >> my_string;
0
 
rahul_r_jadhavCommented:
can you use cin.getline() ??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
anasagastiAuthor Commented:
getline is a solution, but the thing is that sometimes I have a string that I want to store in a string variable, other I have an int to store in an int varible, etc.
0
 
B1-66ERCommented:
Hi anasagasti.
You can try:

  string lol, bla;
 
  cin >> lol ;
  bla += lol ;
  while (cin.rdbuf()->in_avail() != 1)
  {
        cin >> lol ;
    bla += " " + lol ;
  }
  cout << bla << endl;
  while(1) ;

but, will be better if you explain what input data you are waiting for. For examle:
 word integer word integer ....

0
 
anasagastiAuthor Commented:
Ok, I'll try to explain: I have an istringstream, which can content the information of a string and int or a doulble.
The function in which I'm using this is a template so what I use to convert to the final parameter is >>.
More or less this is what I want to do:

      template<class T>
      T getAttributeFromString(const std::string& s) const
      {
            std::istringstream i(s);      

            T value;
            if (!(i >> value))
            {
                  // Error: throw exception
            }
            return value;
      }

s could be: "my string" "1" "1.0" and I want to get a string with "my string" an int with 1 and a double with 1.0. The problem is in the case of "my string", the space between "my" and "string".
0
 
CaudaxCommented:
Why not just use an explicit conversion?

(data-type)cin.getline()

For instance,

(int)cin.getline()

Should give you cin.getline() but as an integer.
0
 
B1-66ERCommented:
Then i`ll propose to check first what kind of input data we have (str/or smthng else):

                std::istringstream i("my str");    
  string str_val ;
               
         T value;

              if (!(i >> str_val))
          {
           // not a string
           i.clear() ; // make stream alive
                  if (!(i >> value))
                  {
               // Error: throw exception
                  }
          } else
              {
 //and if it is a string, just save it
           str_val = i.str() ;
              }
         
  cout << value << " " <<str_val << endl;
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now