473,396 Members | 1,923 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,396 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 3826

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.