Re: how to assigned comma and period in a thousand number?
std C/C++ numerical types don't recognize comma separaters. So, as vijayan has demostrated, you need to find a way to convert the numerical value to a string, then insert the commas and print out the string. There are other ways to do this basic sequence beyond what vijayan has demonstrated, but which way is best for you will depend on what tools you have in your tool chest. To better help us help you we need to know what tools you have available. Do you know about the different types of strings? If so which do you use? Do you know about manipulating arrays/vectors? Do you know about the Standard Template Library, etc. Did you know that main() returns an int in it's standard form? Did you know that you must somehow invoke namespace std when using cout with ith iostream header file? Do you know how to convert numerical values into strings?
Re: how to assigned comma and period in a thousand number?
To Lerner;
Do you know about the different types of strings?
- not really
If so which do you use? Do you know about manipulating arrays/vectors? - i know array a litle
the rest i don't know i am not sure..
Re: how to assigned comma and period in a thousand number?
All right. First things first.
It should always be int main(), not void main() or plain main(). void main() and plain old main() may work on some compilers, but it's not standard. See vijayan's examples.
If you are going to use the standard iostream header file instead of the (deprecated?) iostream.h header file then you need to inovke namespace std somehow. You can do that by including the line
using namespace std;
right after the list of includes. While this isn't the most sophisticated way to access material in namespace std, it's simple, straightforward, and good enough for most purposes, particularly for beginners. See vijayan's examples.
In the final analysis strings are null terminated char arrays. That means that there must be a null char in the char array just after the last char you want to visualize. This basic type string is often called a C style string because it's the only type string you can use in C. In C++ you can still use that style string, but C++ has a standard string class that can be used that does a lot of the grunt work for you that you have to do on your own if you use C style strings. Technically this string class is part to the Standard Template Library and is sometimes called an STL string or just a C++ string to differentiate it from a C style string. This string class has an embedded C style string in it as a data member and it can be accessed as such when you need to do so. This string class is in namespace std and can be accessed by including the string header file and using some technique to gain access to namespace std. (see vijayan's examples) There is a learning curve in terms of using either type string, but more and more people are recommended beginners in C++ learn how to use the C++ style strings right from the start.
To see how to use the C++ string class, see vijayan's example. If you aren't allowed to use this string class and must use the C style strings, then so be it.
In his second example, vijayan takes advantage of some of the methods available in the string class to insert the commas. In his first example he makes extensive use of features available in C++ that will likely cost you points if you submit such a method to an instructor in a beginners class.
To start with I'd suggest you write a program using standard code that creates a string, assigns the value 12345.67 to it and prints the string to the screen. Post it once you've got it completed and then someone can help you figure out how to insert comma's in it if you don't want to use vijayan's example.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.