#1
I've looked all over and can't find an understandable answer. How do I add a comma to a number (has be be between 1,001 and 999,999)?

I've found things about string, but I'm not sure how that would work.

I have something like this, which doesn't work

#include <iostream>
using namespace std;

void main ()

{
	while (true)

	{
		int x;

		cout << "Enter a number between than 1,001 and 999,999: ";
		cin >> x;
		cout << endl << x % 100 << "," << x % 100;

	}
}
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#2
move your mouse over a number between your mentioned limitations. left click the mouse. hit the "," key. might not be the answer you were looking for though :-)

in all seriousness, i doubt its POSSIBLE.
#4
Quote by szekelymihai
int is for integers
You want to use float or double. Also, it's not actually a comma, it's a dot " . "

Just realized I should use double

But not "."
I need "," COMMA!

EDIT: Never mind. Have to use int. % doesn't work with double

Quote by westley23j
move your mouse over a number between your mentioned limitations. left click the mouse. hit the "," key. might not be the answer you were looking for though :-)

in all seriousness, i doubt its POSSIBLE.

From what I read, you have to do it in a roundabout sort of way or something
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
Last edited by izbbass at Sep 23, 2010,
#5
Quote by szekelymihai
int is for integers
You want to use float or double. Also, it's not actually a comma, it's a dot " . "


On the other hand if you take those numbers to be in the thousands then an int type works and the comma separates the thousands out it's perfectly reasonable.
R.I.P. My Signature. Lost to us in the great Signature Massacre of 2014.

Quote by Master Foo
“A man who mistakes secrets for knowledge is like a man who, seeking light, hugs a candle so closely that he smothers it and burns his hand.”


Album.
Legion.
#6
You need to use a library (I'm not sure which one), and a command similar to "setpoint" that formats your i/o.
When the user enters an integer such as 1,000 the computer is only going to read to the comma so it's actually going to store 1 into x rather than 1000.
If I were writing the program I would just tell the user to not use comma's but if that's the objective of the assignment, you can probably find a library and command that allows you to control the i/o in the way you want here http://www.cplusplus.com/
oh also you can't use a float or a double, these people don't know what they're talking about.
Last edited by KurdtStaley at Sep 23, 2010,
#7
Quote by KurdtStaley
You need to use a library (I'm not sure which one), and a command similar to "setpoint" that formats your i/o.
When the user enters an integer such as 1,000 the computer is only going to read to the comma so it's actually going to store 1 into x rather than 1000.
If I were writing the program I would just tell the user to not use comma's but if that's the objective of the assignment, you can probably find a library and command that allows you to control the i/o in the way you want here http://www.cplusplus.com/
oh also you can't use a float or a double, these people don't know what they're talking about.

Found that out already.
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#9
There's probably some sort of function you can use to convert the input integer to a string. Then it's just a matter of looking at the length of the string to work out its magnitude, and therefore how many commas are required, and where.

You are standing in an open field west of a white house, with a boarded front door.
There is a small mailbox here.



Steam: | PSN: Zeroxxed | Twitter:
#10
Quote by Kilobyte
There's probably some sort of function you can use to convert the input integer to a string. Then it's just a matter of looking at the length of the string to work out its magnitude, and therefore how many commas are required, and where.

Yeah

Still, I don't understand the purpose of your algorithm.
#11
Quote by -Collapse-
Oh, hello mister tracer! Lets dance!
Quote by CodySG
And we all poop in the sandwiches!


New Drug Chat, Eh? CLICK HERE


pass: misterhoffman
#12
I'll write you a function if you want to wait 5-10 minutes.
Quote by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
#13
Quote by darkstar2466
I'll write you a function if you want to wait 5-10 minutes.

I can wait. I've resorted to looking through C++ for Dummies
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#14
Quote by izbbass
I can wait. I've resorted to looking through C++ for Dummies



I have this book too and I really don't like it.

It's alright I suppose. But I acquired a collection of e-books via the internet and have found a lot better material.
XBL Gamertag: SealCubs
#15
Well, it's taking me longer than 5-10 minutes haha. I haven't programmed in a few months now. Very rusty. It's almost done though.
Quote by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
#16
Are you trying to accept input that contains a comma, or output stuff in comma'd format?
If the latter, just convert it into a string, using a stringstream or such. Then, get the length of the string, we'll call it len. You know the first comma starts at len - 4, and a new comma is inserted every 3 letters.
So you can get the number of commas needed by (len - 4) % 3, plus one if len is greater than or equal to 4. Then just insert them at the required indexes, such as copying a string.


EDIT:
Uhh. I don't know why I assumed the first comma started at 4 instead of 3, since that'd be 1,0000 instead of 10,000. :P

So scratch that, and just get len % 3 is the number of commas needed.
Last edited by Kapps at Sep 23, 2010,
#17
Yeah this line make just about no sense to me: cout << endl << x % 100 << "," << x % 100;
all it's doing is taking the remainder of x/100 twice and separating them with a comma.
As I said earlier this is a formatting/data type issue and unless the objective of the assignment is to format your i/o in a way that is more normal to the user you should exclude the use of comma's altogether.
You cannot easily convert your int's to string's and it would be stupid to do anyways because then if you wanted to manipulate the i/o later on like say take "x * y/2" it wouldn't work.
Last edited by KurdtStaley at Sep 23, 2010,
#18
Quote by Dirge Humani
You're %100 twice...why?

Also, between than makes no sense

try cout << x / 100 << "," << x%100;

Made it:

cout << x / 1000 << "," << x % 1000;

and it works with every number that it needs to UNLESS IT HAS A ZERO...

5000 gets me 5,0

1001 gets 1,1

It ignores the zeros. Anyway to fix it?

Quote by Kapps
Are you trying to accept input that contains a comma, or output stuff in comma'd format?
If the latter, just convert it into a string, using a stringstream or such. Then, get the length of the string, we'll call it len. You know the first comma starts at len - 4, and a new comma is inserted every 3 letters.
So you can get the number of commas needed by (len - 4) % 3, plus one if len is greater than or equal to 4. Then just insert them at the required indexes, such as copying a string.


EDIT:
Uhh. I don't know why I assumed the first comma started at 4 instead of 3, since that'd be 1,0000 instead of 10,000. :P

So scratch that, and just get len % 3 is the number of commas needed.

How do I use the stringstream? No clue how

EDIT: or are you just saying instead of int x use string len?
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
Last edited by izbbass at Sep 23, 2010,
#19
Sorry for the delay. It works like a charm.

Quote by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
#20
Sorry for the delay. It works like a charm.

Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#21
Quote by izbbass
Thanks. Now...how do I use this?


Quote by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
#22
Where do I put it I mean.
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#24
Quote by UntilISleep
In the compiler

You don't understand me. Do I add it to the code I have and where or is that all it is?
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#25
Don't mean to be a dick, but I am not helping you any further. Revisit the concept of subroutines and what their purpose is. And study the syntax of C++ a little more. You can definitely figure it out.
Quote by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
#26
Quote by darkstar2466
Don't mean to be a dick, but I am not helping you any further. Revisit the concept of subroutines and what their purpose is. And study the syntax of C++ a little more. You can definitely figure it out.

Subroutines?

EDIT: Well I got it to work, except with numbers with zeros.

#include <iostream>
using namespace std;

void main ()

{
	while (true)

	{
		int x;

		cout << "Enter a number between 1,001 and 999,999: ";
		cin >> x;
		cout << endl << x / 1000 << "," << x % 1000 << endl;

	}
}
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
Last edited by izbbass at Sep 23, 2010,
#27
izbass, what are you trying to accomplish with this? i don't see what you could use this for.
do the commas matter than much to you?
People in the pit take my post way too seriously.

MyAnimeList
7-String Legion

If you have a question PM me and I will always get back to you.
#28
Quote by Deadlock Riff
izbass, what are you trying to accomplish with this? i don't see what you could use this for.
do the commas matter than much to you?

Yea I need it for an assignment
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#29
Your method does not work because it is logically incorrect. Look at my code and understand what I am doing. Before you do that, first understand the very basics of C++. You know what variables are, and you probably know how to use them. Now learn what subroutines (functions/methods/calls) are and and how to use them. That knowledge, you seem to lack.
Quote by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
#30
Quote by darkstar2466
Your method does not work because it is logically incorrect. Look at my code and understand what I am doing. Before you do that, first understand the very basics of C++. You know what variables are, and you probably know how to use them. Now learn what subroutines (functions/methods/calls) are and and how to use them. That knowledge, you seem to lack.

I don't understand most of it though
I've only had 3 classes :/
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#31
Quote by izbbass
I don't understand most of it though
I've only had 3 classes :/


did they start you off straight on C++?

i learned basic C first, it helps.
People in the pit take my post way too seriously.

MyAnimeList
7-String Legion

If you have a question PM me and I will always get back to you.
#32
Quote by Deadlock Riff
did they start you off straight on C++?

i learned basic C first, it helps.

Yea. Intro to Computer Programming.
Quote by UntilISleep
You have excellent taste in literature, dear sir

Quote by Primus2112
You have excellent taste in video games, good sir.

Quote by GbAdimDb5m7
You have terrible taste in signatures, idiotic sir.

kkoo
#34
I can't exactly help you, since I seem to only know about as much C++ as you do. I'm also taking it.

EDIT: Wait, you don't know what a subroutine is?

I learned that in my second week of class. What kind of crazy-ass professor do you have, dude?
Last edited by Holy Katana at Sep 23, 2010,
#35
darkstar wrote like the perfect function for you.

But if you're only 3 classes in I wonder if maybe you're expected not to use arrays...
I don't know how detailed you're getting with this but I would add a catch to test if the number entered is actually between the range you specified, and not anything else (words etc).
. . ▄████▄ . ▄███▄
. █████████▀ . . . . . . . . . . . . ▄▄
.███▌█|█|█|█|█|█|█|█|█|█▐███▄▄
. ████████▄ . . . . . . . . . . . . . º º º º º º º
. . ▀████▀. ▀██▀
#36
...i should've payed attention in my AP computer science :/
ADVERTISING SPACE FOR SALE!
I don't charge much...

If you don't click this , you'll die :/
#38
Quote by izbbass
Made it:

cout << x / 1000 << "," << x % 1000;

and it works with every number that it needs to UNLESS IT HAS A ZERO...

5000 gets me 5,0

1001 gets 1,1

It ignores the zeros. Anyway to fix it?

So, look at your example for 5000:
cout << x/1000 << "," << x % 1000;

says

cout << 5000/1000 << "," << 5000%1000;

which becomes

cout << 5 << "," << 0;
Because 5000/1000 = 5 and 5000%1000 = 0.

Same thing with 1001:
1001/1000 = 1 and 1001 % 1000 = 1

The % refers to modular arithmetic, basically, the remainder of the first when divided by the second, eg. 5%6 = 5, 17 % 5 = 2, etc.

I would probably convert it to a string, then starting from the index at the end, put a comma in every 3rd position.