Manipulating user input | | |
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! | | | | 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 | | | | 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 | | | | 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 | | | | 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 | | | | 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 | | | | 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 | | | | 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); | | | | 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); | | | | 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 | | | | 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 | | | | 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. | | | | 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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|