HOW TO GET 1,000 to be read as 1000 and decimal point C++


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.

Thanks for any help.
You need to parse the string and remove the seperators then use one of the standard conversion functions (like atof()).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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

using namespace 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;

}
Last edited on
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);
If you are really talking about I/O, the standard locale library helps here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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>
using namespace 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.
Last edited on
Pretty much every system comes with US English locale, which actually uses commas as thousands separators.

try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <locale>
#include <iostream>
int main()
{
#if _WIN32
    std::locale::global(std::locale("English_USA.1252"));
#else
    std::locale::global(std::locale("en_US.UTF-8"));
#endif

    std::cout.imbue(std::locale());
    std::cin.imbue(std::locale());
    std::cout << "Enter the number " << 12345678 << "> ";

    int n;
    std::cin >> n;

    if (n == 12345678) std::cout << "Good job!\n";
    else               std::cout << "Fooey!\n";
}
Locales are broken on Windows. That is only going to work regularly on Linux.
@Cubbi
Thank you
I never knew that I can also use preprocesser even in main, I thought they are used only at the beginning or above main.
Topic archived. No new replies allowed.