Connecting Tech Pros Worldwide Forums | Help | Site Map

Manipulating user input

agent349
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,

I'm fairly new to c++. I need user input in the form of dollar
amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
store the rest in a variable. How do I go about removing the dollar
sign? Thanks in advance!

Mike Wahler
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Manipulating user input



"agent349" <agent349@yahoo.com> wrote in message
news:e23997a6.0404082133.700878a0@posting.google.c om...[color=blue]
> Hi,
>
> I'm fairly new to c++. I need user input in the form of dollar
> amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> store the rest in a variable. How do I go about removing the dollar
> sign? Thanks in advance![/color]

std::string input;
int amount(0);

std::getline(std::cin, input);
if(!input.empty())
{
std::istringstream iss(input.substr(1));
iss >> amount;
}

std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';

-Mike


Mike Wahler
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Manipulating user input



"agent349" <agent349@yahoo.com> wrote in message
news:e23997a6.0404082133.700878a0@posting.google.c om...[color=blue]
> Hi,
>
> I'm fairly new to c++. I need user input in the form of dollar
> amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> store the rest in a variable. How do I go about removing the dollar
> sign? Thanks in advance![/color]

std::string input;
int amount(0);

std::getline(std::cin, input);
if(!input.empty())
{
std::istringstream iss(input.substr(1));
iss >> amount;
}

std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';

-Mike


John Harrison
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Manipulating user input



"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:%2rdc.2855$A_4.2826@newsread1.news.pas.earthl ink.net...[color=blue]
>
> "agent349" <agent349@yahoo.com> wrote in message
> news:e23997a6.0404082133.700878a0@posting.google.c om...[color=green]
> > Hi,
> >
> > I'm fairly new to c++. I need user input in the form of dollar
> > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> > store the rest in a variable. How do I go about removing the dollar
> > sign? Thanks in advance![/color]
>
> std::string input;
> int amount(0);
>
> std::getline(std::cin, input);
> if(!input.empty())
> {
> std::istringstream iss(input.substr(1));
> iss >> amount;
> }
>
> std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
>
> -Mike
>[/color]

I think you are forgetting the period in the input.

john


John Harrison
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Manipulating user input



"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:%2rdc.2855$A_4.2826@newsread1.news.pas.earthl ink.net...[color=blue]
>
> "agent349" <agent349@yahoo.com> wrote in message
> news:e23997a6.0404082133.700878a0@posting.google.c om...[color=green]
> > Hi,
> >
> > I'm fairly new to c++. I need user input in the form of dollar
> > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> > store the rest in a variable. How do I go about removing the dollar
> > sign? Thanks in advance![/color]
>
> std::string input;
> int amount(0);
>
> std::getline(std::cin, input);
> if(!input.empty())
> {
> std::istringstream iss(input.substr(1));
> iss >> amount;
> }
>
> std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
>
> -Mike
>[/color]

I think you are forgetting the period in the input.

john


John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Manipulating user input



"agent349" <agent349@yahoo.com> wrote in message
news:e23997a6.0404082133.700878a0@posting.google.c om...[color=blue]
> Hi,
>
> I'm fairly new to c++. I need user input in the form of dollar
> amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> store the rest in a variable. How do I go about removing the dollar
> sign? Thanks in advance![/color]

What sort of variable? int, double, some custom money type?

Integer variables are the best for manipulating money, just count you money
in cents, and then convert to dollars when you have to output figures. A
typical newbie error is 'money prints with a period, therefore I must use a
double or a float because they print with periods'. That is an error, money
is an integral quantity, you only ever have a whole number of cents,
therefore integers are the correct thing to use. How you print a money total
has got nothing to do with it.

There are lots of ways to do what you want, here's one way. It works but it
may not do exactly what you want since your requirements are not clearly
defined.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << "enter some money (like this $12.34) "
char dummy1, dummy2;
int dollars, cents;
cin >> dummy1 >> dollars >> dummy2 >> cents;
int total_cents = 100*dollars + cents;

cout << "You entered $" << total_cents/100 << '.' << setfill('0') <<
setw(2) << total_cents%100 << '\n';
}

This is untested code.

The dummy1 and dummy2 variables are there just to absorb the dollar sign and
period from the input.

The last line shows how you can pretty print a money value from an int
variable which is a number of cents.

The big problem with this program is that it makes no attempt to deal with
incorrect input on the part of the user, it will accept some incorrect input
and barf on others. But you didn't specify what you wanted to happen on
incorrect input (or even what correct input was) so I ignored that issue.

john


John Harrison
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Manipulating user input



"agent349" <agent349@yahoo.com> wrote in message
news:e23997a6.0404082133.700878a0@posting.google.c om...[color=blue]
> Hi,
>
> I'm fairly new to c++. I need user input in the form of dollar
> amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> store the rest in a variable. How do I go about removing the dollar
> sign? Thanks in advance![/color]

What sort of variable? int, double, some custom money type?

Integer variables are the best for manipulating money, just count you money
in cents, and then convert to dollars when you have to output figures. A
typical newbie error is 'money prints with a period, therefore I must use a
double or a float because they print with periods'. That is an error, money
is an integral quantity, you only ever have a whole number of cents,
therefore integers are the correct thing to use. How you print a money total
has got nothing to do with it.

There are lots of ways to do what you want, here's one way. It works but it
may not do exactly what you want since your requirements are not clearly
defined.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << "enter some money (like this $12.34) "
char dummy1, dummy2;
int dollars, cents;
cin >> dummy1 >> dollars >> dummy2 >> cents;
int total_cents = 100*dollars + cents;

cout << "You entered $" << total_cents/100 << '.' << setfill('0') <<
setw(2) << total_cents%100 << '\n';
}

This is untested code.

The dummy1 and dummy2 variables are there just to absorb the dollar sign and
period from the input.

The last line shows how you can pretty print a money value from an int
variable which is a number of cents.

The big problem with this program is that it makes no attempt to deal with
incorrect input on the part of the user, it will accept some incorrect input
and barf on others. But you didn't specify what you wanted to happen on
incorrect input (or even what correct input was) so I ignored that issue.

john


David Harmon
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Manipulating user input


On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
<mkwahler@mkwahler.net> wrote,[color=blue]
>std::getline(std::cin, input);
>if(!input.empty())
>{
> std::istringstream iss(input.substr(1));
> iss >> amount;
>}[/color]

Perhaps excessive?
if (cin.peek() == '$')
cin.ignore(1);

David Harmon
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Manipulating user input


On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
<mkwahler@mkwahler.net> wrote,[color=blue]
>std::getline(std::cin, input);
>if(!input.empty())
>{
> std::istringstream iss(input.substr(1));
> iss >> amount;
>}[/color]

Perhaps excessive?
if (cin.peek() == '$')
cin.ignore(1);

Mike Wahler
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Manipulating user input



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c55j5c$2mt6ki$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Mike Wahler" <mkwahler@mkwahler.net> wrote in message
> news:%2rdc.2855$A_4.2826@newsread1.news.pas.earthl ink.net...[color=green]
> >
> > "agent349" <agent349@yahoo.com> wrote in message
> > news:e23997a6.0404082133.700878a0@posting.google.c om...[color=darkred]
> > > Hi,
> > >
> > > I'm fairly new to c++. I need user input in the form of dollar
> > > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> > > store the rest in a variable. How do I go about removing the dollar
> > > sign? Thanks in advance![/color]
> >
> > std::string input;
> > int amount(0);
> >
> > std::getline(std::cin, input);
> > if(!input.empty())
> > {
> > std::istringstream iss(input.substr(1));
> > iss >> amount;
> > }
> >
> > std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
> >
> > -Mike
> >[/color]
>
> I think you are forgetting the period in the input.[/color]

Had to leave something for OP to do. :-)

-Mike


Mike Wahler
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Manipulating user input



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c55j5c$2mt6ki$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Mike Wahler" <mkwahler@mkwahler.net> wrote in message
> news:%2rdc.2855$A_4.2826@newsread1.news.pas.earthl ink.net...[color=green]
> >
> > "agent349" <agent349@yahoo.com> wrote in message
> > news:e23997a6.0404082133.700878a0@posting.google.c om...[color=darkred]
> > > Hi,
> > >
> > > I'm fairly new to c++. I need user input in the form of dollar
> > > amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then
> > > store the rest in a variable. How do I go about removing the dollar
> > > sign? Thanks in advance![/color]
> >
> > std::string input;
> > int amount(0);
> >
> > std::getline(std::cin, input);
> > if(!input.empty())
> > {
> > std::istringstream iss(input.substr(1));
> > iss >> amount;
> > }
> >
> > std::cout << '$' << amount / 100 << '.' << amount % 100 << '\n';
> >
> > -Mike
> >[/color]
>
> I think you are forgetting the period in the input.[/color]

Had to leave something for OP to do. :-)

-Mike


Kevin Goodsell
Guest
 
Posts: n/a
#12: Jul 22 '05

re: Manipulating user input


David Harmon wrote:
[color=blue]
> On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
> <mkwahler@mkwahler.net> wrote,
>[color=green]
>>std::getline(std::cin, input);
>>if(!input.empty())
>>{
>> std::istringstream iss(input.substr(1));
>> iss >> amount;
>>}[/color]
>
>
> Perhaps excessive?
> if (cin.peek() == '$')
> cin.ignore(1);
>[/color]

I think there's a 0-argument ignore() for removing 1 character.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Kevin Goodsell
Guest
 
Posts: n/a
#13: Jul 22 '05

re: Manipulating user input


David Harmon wrote:
[color=blue]
> On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
> <mkwahler@mkwahler.net> wrote,
>[color=green]
>>std::getline(std::cin, input);
>>if(!input.empty())
>>{
>> std::istringstream iss(input.substr(1));
>> iss >> amount;
>>}[/color]
>
>
> Perhaps excessive?
> if (cin.peek() == '$')
> cin.ignore(1);
>[/color]

I think there's a 0-argument ignore() for removing 1 character.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Closed Thread


Similar C / C++ bytes