Solved

cin>>overflow

Posted on 1997-10-13
2
347 Views
Last Modified: 2006-11-17
int Getvalue(double& d)
{

 //Flush clears the input buffer and resets error flags
 //Max and Mix resp 1-999
//Question how do i prevent an overflow
//eg.
//assign d 99999999999999999999999999999999999999999
//get an execption error?

inline void  Flush(istream& str)
{
      str.clear();
      int  char_to_skip =
            str.rdbuf()->in_avail();
   
       str.ignore(char_to_skip);
}

 Flush(cin);
 while(!(cin>>d).gcount()<4){

         if(!cin||d<Min||d>Max)
            cout<<endl<<"Invalid input!"<<endl<<"ReEnter Value: ";
    else break;

       Flush(cin);
     if(cin.eof())
        return 0;
 }
 return(1);
}
0
Comment
Question by:sigjas
2 Comments
 
LVL 3

Accepted Solution

by:
eugenem earned 100 total points
ID: 1171231
Read from cin not to a double but to string. Than convert this string to double (on this step you can perform any checks).
0
 

Author Comment

by:sigjas
ID: 1171232
Thank you
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 139
C++ - Loading Managed Assembly From Memory in Unmanaged Process 25 472
Converting to C# also VS2015 5 125
c++, dynamic object by json 1 42
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

776 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