473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reporting formatted input operations that fail

I would like to report formatted input operations that fail. To achieve this
I write messages that reflect failed operations to std::cerr. For example,

#include <iostream>
#include <string>

class Foo
{
int a;
int b;

public:
Foo() : a(0), b(0) { }

Foo(int a_, int b_) : a(a_), b(b_) { }

std::istream &read(std::istream &is);
};

std::istream &Foo::read(std::istream &is)
{
int a;
if (!(is >> a))
{
std::cerr << "Failed to read instance of int" << std::endl;
return is;
}

// as you can see, these print statements are a pain to write out
// they also make the code look horrible
// can these be factored out into a separate class or method?
// how?

int b;
if (!(is >> b))
{
std::cerr << "Failed to read instance of int" << std::endl;
return is;
}

*this = Foo(a, b);
return is;
}

inline std::istream &operator>>(std::istream &is, Foo &foo)
{ return foo.read(is); }

int main()
{
std::string s;
if (!(std::cin >> s))
{
std::cerr << "Failed to read instance of std::string" << std::endl;
return 1;
}

Foo foo;
if (!(std::cin >> foo))
{
std::cerr << "Failed to read instance of Foo" << std::endl;
return 1;
}

// what about a quiet version of std::cin >> foo?
// operator>> is already defined to report failures
// what do I do?

return 0;
}

How can I address the issues that are described in comments? Thanks.
Jul 23 '05 #1
0 961

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bob Smith | last post by:
question in subject. does the standard say anything about this or is it platform dependent? any advice much appeciated /B
7
by: TheDD | last post by:
Hello, i'm developping some function to read pbm image files. Somewhere in my code i have: texel_t val; while (in >> val) { ...
6
by: Jason Heyes | last post by:
I am starting to worry about the performance of formatted read/write operations on data-redundant objects in my program.What can I do to improve this performance should it become an issue? ...
4
by: Fabian von Romberg | last post by:
Hi, I have installed Sql Reporting Services on 2 machines, one is WIN 2000 PRO and the other one is WIN 2000 ADV. SERVER. When I try to access a report using the webbrowser, I get the following...
5
by: Todd | last post by:
Is there any sort of formatted reporting (outside of just a datagrid with results of a query from a dataset) that is available within the .NET framework or is a 3rd party tool needed to do this...
3
by: Paul Ritchie | last post by:
We are writing a web application for sale to clients to deploy on their own web server. Is there a good reference available comparing the various .NET compatible reporting packages? Cost of...
8
by: Mateusz Ɓoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
43
by: SLH | last post by:
hi people. im trying to validate input received via a text area on an ASP page before writing it to a database. i cant use client side javascript due to policy, so it all has to happen on the...
2
by: Todd VanDenHul | last post by:
Hey All, Does anyone know of any good reporting engines for .NET? We're currently evaluating Telerik, Reporting Services and Crystal Reports. We need to be able to have multiple input/output...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.