[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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
0
Rhyox
Asked:
Rhyox
  • 6
  • 3
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now