Link to home
Start Free TrialLog in
Avatar of smconvey
smconvey

asked on

cin.peek() & cin.seekg() in a for loop??

Why doesn't the for loop advance the read position of the cin stream via the seekg()line, so that I can test each character via cin.peek()???

bool success = true;
charar invalidChar = '\0';
double operand = NULL;
string discard;
cout<< "Please enter an operand --> ";

for(int n = 0; success == true && cin.peek() != '\n'; n++)
{
   invalidChar = cin.peek();
   if(!isdigit(invalidChar))
         if(!(invalidChar == '.'))
            success = false;
   cin.seekg((n+1), ios::beg);
}
Avatar of jkr
jkr
Flag of Germany image

>>advance the read position of the cin stream

Err, why would you want to do that? 'Advancing' cin is sort of an oxymoron. Why don't you just

for(int n = 0; success == true && invalidChar != '\n'; n++)
{
  cin >> invalidChar;
  if(!isdigit(invalidChar))
        if(!(invalidChar == '.'))
           success = false;
}
Avatar of smconvey
smconvey

ASKER

I'm trying to read in a double, but I wanted to test it first. By peeking at and testing each character, I still have it in the cin stream to be able to read it into operand if it is valid. If I do it your way, the potential operand is gone (dicarded a character at a time). I am a student, and I am trying to learn to use the peek and seekg methods.
Ah, OK, that makes sense. 'seekg()' won't be able to advance 'cin' as there is no further input past the current stream position So, using

for(int n = 0; success == true && cin.peek() != '\n'; n++)
{
  invalidChar = cin.peek();
  if(!isdigit(invalidChar))
        if(!(invalidChar == '.'))
           success = false;
 cin >> invalidChar;
}

will make it work.
cout<< "Please enter an operand --> ";

If I would have entered 35.68 to the above prompt, why wouldn't seekg have moved the read position incrementally with each iteration of the for loop so that peek could validate each of the characters until the newline is reached, and if success cin>>operand?

for(int n = 0; success == true && cin.peek() != '\n'; n++)
{
   invalidChar = cin.peek();
   if(!isdigit(invalidChar))
         if(!(invalidChar == '.'))
            success = false;
   cin.seekg((n+1), ios::beg);
}
>>If I would have entered 35.68 to the above prompt, why wouldn't seekg have moved the read position
>>incrementally

Because you are not giving it a chance to do so - your code is executed even *before* the '3' is entered.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just tried emailing you at XRKFNDZEMORB@spammotel.com as indicated in your profile, but I'm not sure if it is current. Can I call you to discuss? I'm confused on a few points.
The email address is correct. Are you sure you want to make long distance call to good ole europe? BTW, gonna leave for lunch soon anyway :o)

there are two seekg functions are there

basic_istream<charT,traits>& seekg(off_type, ios_base::seekdir);
basic_istream<charT,traits>& seekg(off_type);

you are using the first one . so std::ios::beg will initialise the stream pointer ate beg
so seekg(n+1,std::ios:beg)
will make ur pointer to not to move i feel so

firts initialise with std::ios::beg but later use only seekg(n+1)
use only seekg(n+1)

i hope it will work





check also std::isspace
Thanks, but I had to move on. I have already changed my code to accomplish my goal another way. My project was due.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr {http:#9690300}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer