Help with a piece of code
This code is meant to have input as a string, and output as a float, but it returns strange numbers. I guess it is something with the converter function at the beginning. Could someone please tell me what's wrong?
here is the code (it's modified, but it conveys the problem)
Code:
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <string>
#include <stdexcept>
using namespace std;
class BadConversion : public std::runtime_error {
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};
inline float convertToFloat(const std::string& s)
{
std::istringstream i(s);
float x;
if (!(i >> x))
throw BadConversion("convertToFlaot(\"" + s + "\")");
return x;
};
int main()
{
string w;
string q;
string x;
string y;
string m;
float wint;
float qint;
float n;
char keyPressw;
char keyPressq;
char keyPressx;
char keyPressy;
cout<<"Input your first point:(";
do {
keyPressw=getch();
if ( keyPressw != char(13)){
w+=keyPressw;
cout<<keyPressw;
}
} while ( keyPressw != char(13));
cout<<",";
do {
keyPressq=getch();
if ( keyPressq != char(13)){
q+=keyPressq;
cout<<keyPressq;
}
} while ( keyPressq != char(13));
cout<<")";
cout<<"Input your second point:(";
do {
keyPressx=getch();
if ( keyPressx != char(13)){
x+=keyPressx;
cout<<keyPressx;
}
} while ( keyPressx != char(13));
cout<<",";
do {
keyPressy=getch();
if ( keyPressy != char(13)){
y+=keyPressy;
cout<<keyPressy;
}
} while ( keyPressy != char(13));
cout<<")\n";
wint==convertToFloat(w);
qint==convertToFloat(q);
wint-qint==n;
cout<<qint<<"and"<<wint;
cin.get();
}