Solved

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

Posted on 2003-11-05
13
1,153 Views
Last Modified: 2007-12-19
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);
}
0
Comment
Question by:smconvey
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9689918
>>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;
}
0
 

Author Comment

by:smconvey
ID: 9689970
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9690008
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.
0
Technology Partners: 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!

 

Author Comment

by:smconvey
ID: 9690086
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);
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 9690194
>>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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9690300
Hum, if you want to learn "learn to use the peek and seekg methods", try using a stringstream for that purpose:

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

//...

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

char ac [ 256];
cin >> ac;

stringstream ss;

ss << ac;

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

Author Comment

by:smconvey
ID: 9690331
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9690414
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)
0
 
LVL 4

Expert Comment

by:havman56
ID: 9694286

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





0
 
LVL 4

Expert Comment

by:havman56
ID: 9694480
check also std::isspace
0
 

Author Comment

by:smconvey
ID: 9695235
Thanks, but I had to move on. I have already changed my code to accomplish my goal another way. My project was due.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10242556
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question