473,416 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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!
Jul 22 '05 #1
12 3827

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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
Jul 22 '05 #2

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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
Jul 22 '05 #3

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:%2*****************@newsread1.news.pas.earthl ink.net...

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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


I think you are forgetting the period in the input.

john
Jul 22 '05 #4

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:%2*****************@newsread1.news.pas.earthl ink.net...

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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


I think you are forgetting the period in the input.

john
Jul 22 '05 #5

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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
Jul 22 '05 #6

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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
Jul 22 '05 #7
On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
<mk******@mkwahler.net> wrote,
std::getline(std::cin, input);
if(!input.empty())
{
std::istringstream iss(input.substr(1));
iss >> amount;
}


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

Jul 22 '05 #8
On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
<mk******@mkwahler.net> wrote,
std::getline(std::cin, input);
if(!input.empty())
{
std::istringstream iss(input.substr(1));
iss >> amount;
}


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

Jul 22 '05 #9

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:%2*****************@newsread1.news.pas.earthl ink.net...

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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


I think you are forgetting the period in the input.


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

-Mike
Jul 22 '05 #10

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:%2*****************@newsread1.news.pas.earthl ink.net...

"agent349" <ag******@yahoo.com> wrote in message
news:e2**************************@posting.google.c om...
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!


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


I think you are forgetting the period in the input.


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

-Mike
Jul 22 '05 #11
David Harmon wrote:
On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
<mk******@mkwahler.net> wrote,
std::getline(std::cin, input);
if(!input.empty())
{
std::istringstream iss(input.substr(1));
iss >> amount;
}

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


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.
Jul 22 '05 #12
David Harmon wrote:
On Fri, 09 Apr 2004 06:05:47 GMT in comp.lang.c++, "Mike Wahler"
<mk******@mkwahler.net> wrote,
std::getline(std::cin, input);
if(!input.empty())
{
std::istringstream iss(input.substr(1));
iss >> amount;
}

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


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.
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: hokiegal99 | last post by:
I don't understand how to use a loop to keep track of user input. Could someone show me how to do what the program below does with a loop? Thnaks! ---------------------------- #Write a...
3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
12
by: agent349 | last post by:
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...
0
by: Michael | last post by:
Hi All, I am working on my intranet reporting web site, by use of IIS5 + Office2000 +SQL2000. Currently, the first step, I show user an ASP web-interface to collect user input, keyword...
17
by: comp.lang.tcl | last post by:
The TCL command I am using will do a command-line action on a PHP script: set cannotRunPHP I have to do it this way as both the TCL script and the PHP script run as CLI. However, "info.php"...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
2
by: danielboendergaard | last post by:
Hey Im making a homepage in php. I use a html form to put data into mysql and i want to make some buttons which inserts user input values into a textarea. I have used a button like this: <input...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
12
by: Tarique | last post by:
I have tried to restrict the no. of columns in a line oriented user input.Can anyone please point out potential flaws in this method? btw.. 1.I have not used dynamic memory allocation because...
5
by: renzy0113 | last post by:
thank you. how do I change the all the numbers inputted, but the words still stays the same? like this: if the user enters, Mary had 45 lambs. my program will output,
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.