I'm completely new to coding, so please keep this in mind before commenting.
So I've been trying to get into coding for a while, and today I went to the library and picked up a book called "programming in c++". I've written some basic programs, but I have gotten stuck at one point, I have no idea how to create a function that makes sure that when the user is prompted for a double, what they enter is valid. (If the user enters a character like 'k', the program just breaks). I searched here and on the net, and there are some answers, but they are more along the line of "here's a line of code that works, insert x, y, z, into it". And I don't have enough experience to know what to do. So here is a sample that is a mix of code from another question, and me failing to try to make it work in my program:
#include <iostream>
#include <stdio.h>
using namespace std;
double GetDouble () {
double x;
cin >> x;
int valid = 0;
while (valid == 0) {
if (x == double) {
return x;
break;
} else if (x != double) {
cout << "Invalid Input! Please input a numerical value." << endl;
cin.clear();
while (cin.get() != '\n') ; // empty loop
}
}
return x;
}
Now what I want it to do is to use "cin << x" to get a user input for x, and then make sure that x is a double, and I'm failing quite hard at this. If someone could explain this to in a manner that clarifies each operation, I would be truly grateful. For example, I don't know what the break function does, and what the cin.clear(), and cin.get() do. (I know this is a duplicate, but the answers on the other question does not at all address what I'm confused about, thank you for taking time to read & answer this! :)