473,799 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::istr eam &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 972

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

Similar topics

11
8383
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
1487
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
2203
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? Thanks.
4
3918
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 error, this happens only if try access the report on the SERVER, it I try on my local machine, it works just fine. Any help will be much appreciated. Thanks in advance, Fabian von Romberg
5
1169
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 reporting Thanks in advance Tod
3
1731
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 deployment and overall functionality would be the main factors. I guess the obvious candidate would be Crystal Reports? What do you use in your application and would you recommend it?
8
2445
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
2626
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 server. here is what i was trying, but pieces of it continue to break for one reason or another. the thinking behind this function was like this: if the input is less than 10 characters long, fail. if its 10 characters or greater, but it doesnt...
2
1165
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 formats, drilldown capabilities and it needs to be easy for us and our users to create reports. Any input that anyone has would be appreciated.
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10485
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.