Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1050891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:50:26+00:00 2026-05-16T16:50:26+00:00

In java, if I wanted to create some application which could receive both doubles

  • 0

In java, if I wanted to create some application which could receive both doubles and strings as appropriate input, I would probably do the following:

String input = getInput();//
try { 
    double foo = Double.valueOf(input);
    //Do stuff with foo here 
} catch (NumberFormatException e) { 
    //Do other validation with input
} 

How would you go about doing that in c++? atof() returns 0.0 for invalid input, but how would you differentiate that from a valid double of “0.0”? As an aside, I can only include <iostream>, <string>, <cstdlib>, and <cassert> in this project. I’m assuming I need to use cin in some way, but how can you grab the original input after cin fails to parse some string as a double?

Edit: I could probably use the following, but as I said before, I’m not allowed to import <sstream> on this assignment for some reason

string input;
getline(cin, input);

double x;
istringstream foo(input);
foo >> x
if(cin){
    //do manipulations with x
}
else{
    //since it's not a number, check if input is a valid command etc..
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:50:27+00:00Added an answer on May 16, 2026 at 4:50 pm

    Exceptions should be reserved for exceptional situations. While you certainly can abuse them like this, it’s a lousy idea — clearly you’re pretty much expecting things other than doubles, so treating it as an exception doesn’t make any real sense.

    The only real question is the exact circumstance under which you want the input treated as a string. Just for example, if the input string was something like “1.23e2 non-numeric data”, do you want to just use the “1.23e2” as a number, or do you want to treat the whole thing as a string?

    Either way, you’d want to us strtod for the conversion — the difference is only how you react to what it returns. In particular, strtod takes two parameters instead of just one like atof does. The second parameter is a pointer to pointer to char. Assuming you pass a non-NULL pointer, strtod will set it to point at the last character in the input string that it successfully converted. If it’s pointing to the beginning of the input string, nothing was converted. If it’s pointing to the end, everything was converted. If it’s somewhere in between, part of the string converted, and part didn’t.

    For the moment, I’m going to assume that you want a double value holding whatever number could be converted at the beginning of the string, and whatever couldn’t be converted treated as a string:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main() { 
        char input[] = "12.34 non-numeric data";
        char *string;
        double value = strtod(input, &string);
    
        printf("Number: %f\tstring: %s\n", value, string);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 543k
  • Answers 543k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use parents("tr:first") to walk up the chain and select the… May 17, 2026 at 6:29 am
  • Editorial Team
    Editorial Team added an answer Just do the deserialization in a background thread in all… May 17, 2026 at 6:29 am
  • Editorial Team
    Editorial Team added an answer There are no inherent disadvantages of using Java reflection to… May 17, 2026 at 6:28 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I wanted to add to my jdk6\jre\lib\security\java.policy file an interdiction to create some classes
I'm about to port a smallish library from Java to Python and wanted some
From the perspective of a cross application/applet java accessibility service, how would you link
If I wanted to create a commercial web-project (like e.g. facebook) with a Java
I wanted to make Map of Collections in Java, so I can make something
My comfort level with java is good , I wanted to teach my friend
At work we do almost everything in Java and perl, but I wanted to
At present I am using eclipse for JAVA project. I always wanted to use
I've written a fairly simple java application that allows you to drag your mouse
I'm totally new to both java and java Server worlds... But I've a good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.