-5

I am trying to take user input as floating point only. to expect such value

Enter a floating-point number: a Please enter floating-point only: 123.45 floating-point number: 123.45 You entered: 123.45

Here is what I have tried so far.

#include <iostream>
using namespace std;

double readDouble(string prompt){
    prompt = cout << "Enter a floating point number: ";
    if (cin.fail())
        cout << "Numbers Only Please " << endl;
    return 0;
}

int main() {
    double value = readDouble("Enter a floating-point number: ");
    cin >> value;
    cout << "You entered: " << value << endl;
    if (cin.fail())
        cout << "Please Enter Numbers only Please " << endl;
}

while (true){
    if (cin != value);
    cout << "Number Only!!" << endl;
    cin >> value << endl;
}
user93097373
  • 187
  • 1
  • 3
  • 11

2 Answers2

1

I have no idea what half of your code means, but here is your solution. I don't understand why you have a while loop after main? Many other "Why's?" , but here is the code:

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
string input = "";
int number = 0;

while (true)
{
    cout << "Enter a float value: ";
    getline(cin, input);

    stringstream myStream(input);

        if (myStream >> number)
        {
            system("CLS");
            break;
        }
    system("CLS");

    cout << "Invalid input! Try again" << endl << endl;



}

cout << "Float: " << input << endl << endl;

system("PAUSE");


return 0;
}
0

This is not C++. Please consider forgetting what you think you know and read those pages:

There's everything on these three pages to write a simple program that prompts the user for a floating point number until she does provide one.

*I need at least 10 reputation points to post more that 2 links :(

prgasp77
  • 37
  • 1