Hi I need help when i put in a number as a parameter
using float and atof()
I want a basic coding to make 1,000 to be read as 1000 instead of 1.
Also can get any reference on how to use int decCount (0) because I can't find it anywhere. or anyway to count the number of decimals POINTS not decimal places for example not accept numbers like this 1.2.54
could anyone refer me to basic code or reference to do this.
#include <iostream>
#include <string> //Might not be needed depending on complier
#include <cstdlib> //Needed for atoxx() functions
#define SEP_SYMB ',' //This will be the seperation symbol used
usingnamespace std;
int getNumber(string a) {
for(int i=0; i<=a.size(); i++) { //Loop through the entire string
if(a[i]==SEP_SYMB) { //Check for occurence of SEP_SYMB
a.erase(i,1); //If SEP_SYMB , remove that single character
}
}
//Just use as per needed return type of return
//WARNING: requires a change in this function return type also
return atoi(a.c_str()); //Will return integer
//return atof(a.c_str()); //Will return float
//return atol(a.c_str()); //Will return long integer
//return atoll(a.c_str()); //Will return long long integer
}
int main() {
//Print a messege for taking an input
cout << "\n\t\t\t A (" << SEP_SYMB << ") seperated number : ";
//Store for input string
string sInput;
//Get and store the input
cin >> sInput;
//Store the return from getNumber function
int iOutput = getNumber(sInput);
//Print a messege the number
cout << "\n\t\t\t Actual Number : " << iOutput;
return 0;
}
The same technique may be used to count number of decimals in any input number like
1 2 3 4 5 6 7 8 9
#define DEC '.'
int nDEC = 0;
for(int i=0; i<=a.size(); i++) { //Loop through the entire string
if(a[i]==DEC) { //Check for occurence of DEC
nDEC++; //If DEC , increment nDEC
}
}
There are also standard algorithms that you can use...
1 2 3 4 5 6 7 8 9
#include <algorithms>
//...
int getNumber(string a) {
std::remove(a.begin(),a.end(), SEP_SYMB);
return atoi(a.c_str());
}
//... this also does the same as the decimal counting above.
int nDec = std::count(a.begin(), a.end(), DEC);
#include <locale>
#include <string>
template <typename T>
struct thousands: std::numpunct <T>
{
T separator;
public:
thousands( T separator = ',' ):
separator( separator )
{ }
protected:
T do_thousands_sep() const
{
return separator;
}
std::basic_string <T> do_grouping() const
{
return"\3";
}
};
#include <iomanip>
#include <iostream>
usingnamespace std;
int main()
{
locale loc( locale::classic(), new thousands <char> ( ',' ) );
cin.imbue( loc );
cout.imbue( loc );
int n;
cout << "Enter the number " << 12345678 << "> ";
cin >> n;
if (n == 12345678) cout << "Good job!\n";
else cout << "Fooey!\n";
return 0;
}
If you are talking about using thousands-separators in your code, then that cannot be done. Well, actually, it can, but every way I can think to do it requires awful, evil hacks.
+1 for using std::count() for counting decimal points.
Hope this helps.
[edit] Oh yeah, I also wanted to put in a plug for the Boost Locale library.