Taking multiple prices then adding them together...

A

agent349

Hi,

Could someone lead me in the right direction here?

Here are my requirements:
1. I want the user to input as many prices as they want (ie: $9.99
$19.99 $29.99 $1.99 $4.99)
2. Then, when they are done (press enter) I want to add all of the
prices and get a total sum.

Thats it! Thanks in advance.
 
K

Kevin Goodsell

agent349 said:
Hi,

Could someone lead me in the right direction here?

Here are my requirements:
1. I want the user to input as many prices as they want (ie: $9.99
$19.99 $29.99 $1.99 $4.99)
2. Then, when they are done (press enter) I want to add all of the
prices and get a total sum.

Thats it! Thanks in advance.

Do you have a specific question? Or a first attempt for us to comment on?

We're not in the habit of doing programs for people, and giving advice
based on a vague program description isn't very easy.

If you want all the prices to be entered on a single line, you should
probably use std::getline() to read the complete line, then parse the
prices out of it. Getting the line is easy, parsing may be a bit more
tricky. You can use things like the find() member function of
std::string, but it would probably be easier to use a
std::istringstream. That would allow you to read a "word" at a time from
the string (the line). Each "word" should be a price. Add it into a
running total, and you should be good to go.

Assuming you want to follow that route, start with this:

#include <iostream>
#include <sstream> // for istringstream
#include <string>

using namespace std;

int main()
{
cout << "Enter prices, all on one line:" << endl;

// ...

return 0;
}
 
D

David Harmon

On 16 Apr 2004 22:34:55 -0700 in comp.lang.c++, (e-mail address removed)
(agent349) wrote,
1. I want the user to input as many prices as they want (ie: $9.99
$19.99 $29.99 $1.99 $4.99)
2. Then, when they are done (press enter) I want to add all of the
prices and get a total sum.

Consider a simpler approach. Use a running total variable that starts
at zero and gets each price added as you input it. As soon as you have
got them all, you have your answer.

If you get stuck, post the code you have so far and ask specific
questions about it. This issue is covered in Marshall Cline's C++ FAQ.
See the topic "[5.8] How do I post a question about code that doesn't
work correctly?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
E

E. Robert Tisdale

Kevin said:
Assuming you want to follow that route, start with this:

#include <iostream>
#include <sstream> // for istringstream
#include <string>

int main(int argc, char* argv[]) {

using namespace std;

cout << "Enter prices, all on one line:" << endl;

// ...

return 0;
}
 
J

Jon Bell

Here are my requirements:
1. I want the user to input as many prices as they want (ie: $9.99
$19.99 $29.99 $1.99 $4.99)

Are you required to include the $ signs as part of the input? If you can
do without them, the problem will be much easier.
 
K

Kevin Goodsell

E. Robert Tisdale said:
Kevin Goodsell wrote:

In fact, I didn't.
Assuming you want to follow that route, start with this:

#include <iostream>
#include <sstream> // for istringstream
#include <string>

int main(int argc, char* argv[]) {

using namespace std;

cout << "Enter prices, all on one line:" << endl;

// ...

return 0;
}

I don't see anything in your version that improves correctness over
mine. All your changes seem to do is increase the potential for confusion.

-Kevin
 
J

Jakob Bieling

E. Robert Tisdale said:
Kevin said:
Assuming you want to follow that route, start with this:

#include <iostream>
#include <sstream> // for istringstream
#include <string>

int main(int argc, char* argv[]) {

using namespace std;

cout << "Enter prices, all on one line:" << endl;

// ...

return 0;
}


Is that style your picking on?
 
S

Siemel Naran

Kevin Goodsell said:
E. Robert Tisdale wrote:
int main(int argc, char* argv[]) {

using namespace std;

// ...

}
I don't see anything in your version that improves correctness over
mine. All your changes seem to do is increase the potential for confusion.

Where is the potential for confusion? I think he just moved the using
namespace directive into function scope. Generally, it's preferred not to
put using directives at file scope.
 
K

Kevin Goodsell

Siemel said:
E. Robert Tisdale wrote:
int main(int argc, char* argv[]) {

using namespace std;

// ...

}

I don't see anything in your version that improves correctness over
mine. All your changes seem to do is increase the potential for confusion.


Where is the potential for confusion? I think he just moved the using
namespace directive into function scope. Generally, it's preferred not to
put using directives at file scope.

There are two other changes I noticed: Formatting and the addition of
the argv and argc parameters. I find the new formatting more confusing,
and expect a newbie would as well (though it certainly depends on the
newbie). The main() arguments add complexity without adding value, and a
newbie may not have seen them yet, and/or may not understand them.

As for the 'using', I would agree that it can't hurt to limit its scope.
However, for newbies I would recommend keeping it at the top of the file
until they know about namespaces and how to use them properly.
Otherwise, the newbie will probably get confused when he or she moves on
to writing functions, and finds that his or her compiler complains about
'cout' or 'string' being undefined in the function.

-Kevin
 
A

agent349

Are you required to include the $ signs as part of the input? If you can
do without them, the problem will be much easier.

Thanks for the help, sorry I will post my code with my questions next time! :)
After some work, here's what I came up with. It seems to do the trick.

-----------------------------------
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
double sum = 0;

for (;;) {

double value = 0;
char dollarsign;

cout << "Enter next price: ";

cin >> dollarsign >> value; // gets rid of "$"

if (value == 0) { // enter $0 to exit

break;
}

sum = sum + value;

}

cout << "\nYour total is $" << sum << endl;

return 0;

}
 
S

Siemel Naran

There are two other changes I noticed: Formatting and the addition of
the argv and argc parameters. I find the new formatting more confusing,
and expect a newbie would as well (though it certainly depends on the
newbie). The main() arguments add complexity without adding value, and a
newbie may not have seen them yet, and/or may not understand them.

Not sure I agree with the above, that the new formatting is more confusing.
IMHO, neither formatting style is inherently better than the other.
As for the 'using', I would agree that it can't hurt to limit its scope.
However, for newbies I would recommend keeping it at the top of the file
until they know about namespaces and how to use them properly.
Otherwise, the newbie will probably get confused when he or she moves on
to writing functions, and finds that his or her compiler complains about
'cout' or 'string' being undefined in the function.

For teaching newbies, I think it's best to not even use using namespace
directives at all.

(Plan is: then introduce using declarations like "using N::classname";.
Then Koenig lookup. Then using directives.)
 
S

Siemel Naran

agent349 said:
Thanks for the help, sorry I will post my code with my questions next time! :)
After some work, here's what I came up with. It seems to do the trick.

-----------------------------------
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
double sum = 0;

for (;;) {

double value = 0;
char dollarsign;

cout << "Enter next price: ";

cin >> dollarsign >> value; // gets rid of "$"

if (value == 0) { // enter $0 to exit

break;
}

sum = sum + value;

}

cout << "\nYour total is $" << sum << endl;

return 0;

}

This is good. Only thing I don't like is that you have to enter a $0 to
exit. We should be able to just press <enter>, or at least in a text
console program that's nice to have. Can you write this?
 
M

Mark A. Gibbs

Siemel said:
For teaching newbies, I think it's best to not even use using namespace
directives at all.

(Plan is: then introduce using declarations like "using N::classname";.
Then Koenig lookup. Then using directives.)

I don't agree with that entirely. In my experience, the more clutter you
introduce into a sample program for teaching purposes - even if it's
just syntactic frosting - the harder it is for the student to swallow
the critical concept that program is trying to get across. In fact,
whenever I've taught (though I'm not a teacher by trade), I prefer to
first show the critical lines that demonstrate the point separate and
non-compilable, then drop those lines into a pre-introduced boilerplate
framework to demonstrate them in action.

Actually, I prefer it when things break - sometimes I even hope for it.
I've never written books or taught by any long-distance methods, so I'm
always standing right there if the learner has a problem. If there were
a namespace collision in a stage of the lesson before namespaces are
introduced, I would call attention to what the problem is, explain why
it has happened, then show a simple workaround (ie, renaming the
identifier so it doesn't conflict). Then when namespaces are discussed
later, I would hark back to the issue, and offer it as real-life
justification for namespaces' existence.

It's also my experience that if you teach a person 10 equivalent ways to
do something, they will almost always use the last way you taught them
exclusively. That's why I've always taught "using" in the order of
directives then declarations. I've always taught Koenig lookup before
either, while describing namespaces in general, but now you've given me
something to think about.

mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top