Basically I need to writed a program that asks for a double and if the user inputs something keep going, but if the user just presses enter then the program should end. The problem is i can't get it to do anything after the person presses inter


double value;

cin >> value;

if(sizeof(value) == 8)

cout << "no input" <- this never happens if i input white space

Recommended Answers

All 5 Replies

you should have the input be read in as a sting and test if it is empty. if it is then quit and if it is not then continue.

If i read is as a string, how would i get back to a double? Is there a way i could read both as a double and a string? Meaning, can an input be saved at the same time into two different variables? Or would it be easier to just convert the string back to a double later?

Quick way as suggested:

string userInput;
getline(cin, userInput);
double d = atof(userInput.c_str());

Sorry if this is a dumb question, but is validate a function? If so, under what library is it? I put it validate(userInput); on the compiler but says validate is undefined. Thanks in advance.

So I should have been clear. It is a function that you need to create to validate the user's input. Here is an example :

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

void validate(const std::string& input){
  for(int i = 0; i < input.size(); ++i){
     if( ! isdigit(input[i] || input[i] != '.' ) throw std::exception("Error invalid input");

int main(){
  string input;
  getline( cin , input );
  validate( input );
  double d = atof( input.c_str() );
  cout << d * d << endl;

The above is not tested nor compiled, its to give you an example and to be more clearer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.