Reading input

I'm reading input from cin. and I want to store every word or number thats seperated by a whitespace into a string or cstring. Also i want the first character of every word or number stored in a char.... How would i do this?
~Thanks in advance
RhyoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcgabeCommented:
What have you tried so far?
0
RhyoxAuthor Commented:
I have tried using cin.get, cin.putback, and cin.getline... i currently dont have anything typed in for it right now. but it was something like..

char current[100];
char first;

cin.get(&first, 1, '  ');
cin.putback(first);
cin.getline(current, 100, '  ');
0
RhyoxAuthor Commented:
Or if you can tell me a way to tell if a string has only numbers in it. then i could convert it to a double easy enough, which would also reach my goal... the number can also have a decimal in it.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

rajeev_devinCommented:
Your question is not clear to me. Can you post some examples ?
0
RhyoxAuthor Commented:
ie i type in...
foo 92 b    32.5  1   work it

and for code i have
////////////////
string s;

while(cin.peek() != '\n')
{
       cin>>s;
       //determine if s is a number
       //do some more stuff
}
///////////////
this will seperate s into different parts by the whitespaces.. s can be of any letters/numbers/symbols.. I only need to know how to be able to determine if it is a number... for example if i could find a way to get the first character of s i could compare it to 0-9...
 
0
rajeev_devinCommented:
Do something like this

string s;      
while(cin.peek() != '\n')
{
      cin >> s;

      for (int i = 0; i < s.size(); ++i) {
            if (!isdigit(s[i]) && s[i] != '.')
            {
                  cout << s << " is not a number" << endl;
                  break;
            }
      }
      if (i == s.size())
      {
            cout << s << " is a number" << endl;
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RhyoxAuthor Commented:
Thank you
0
pcgabeCommented:
Be careful.  That doesn't take into account multiple decimal points in a single token.  For example, "2.3.4" will return " is a number".  However, that being said, if you know FOR SURE that the user input will always be valid, then that isn't necessarily a problem.

Also, it fails on negative numbers, but again, there were no negative numbers in the example, so perhaps there are no negatives in the input.

Either way, both of those safeguards can easily be added to rajeev's example.

Rhyox, you said: "if i could find a way to get the first character of s i could compare it to 0-9... "  Look at this line from rajeev's code:
          if (!isdigit(s[i]) && s[i] != '.')

See the 's[i]'?  's', being a string, can be accessed like an array of chars.  So 's[0]' would be the first character of the string.  The function 'isdigit(n)' (as its name implies) looks at 'n' to see if it's between '0' and '9'.
0
RhyoxAuthor Commented:
I do get a warning though when i compile on the for loop line..
warning C4018: '<' : signed/unsigned mismatch
0
pcgabeCommented:
No worries.  'i' is a signed int, and 's.size()' is unsigned (can only be positive).  You can put 'unsigned' before the 'int' and the error will go away.  Eg:

     for (unsigned int i = 0; i < s.size(); ++i) {
0
RhyoxAuthor Commented:
Thanks for the response on that but i just casted the s.size to an int instead.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.