How to calculate large figures

Running bloodshed free dev C++ compiler

i'm looking to have a user enter in x amt of days to calculate sales figures.  For example

User 1 wants to calculate there sales figures for 10 days.... and print back there total.

They will have to enter in each day how much they sold....and then have the program calculate the total.

Is there a quick way to program that instead of having to do 10 inputs followed by addition coding.

What if I needed them to intput 100 days..

int a
int b
int c

cin >>a
cin >>b
cin >>c

total = a + b + c ..........................................................

I don't want to have to do that?

is there an easier way?

thanks for any ideas
WestonGroupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> I don't want to have to do that?

Well, if there are 10 values to be input, then somehow, you have to obtain them.

Just one bit I don't understand ... Do they on each day, enter the sales for that day, and then at any time can ask for the total up till now ? If so, then you'll have to save the current total in a file (or maybe even the sales per day).
0
WestonGroupAuthor Commented:
If the program is being run.

The user will be prompted to enter how many days would you like to calculate your sales figures for.

They then will enter in x amt of days and the program will request they input that amt of days sales figures in and then calculate the total.

So if the salesman wants to see what his total sales were for a 20 day stretch.

he would enter in   day 1 = $300  day 2 = $50  day 3 = $40  so on and so on for 20 days

and then spit back his total sales...

I'm just not sure what the most efficient method would be for this... cause in reality he could enter in 1 day 10 days or 100 days... how can I make the program know to run that many days for example how do I know how many variables to declare... I can't predict how many days the saleman would want to look at.

I'm assuming I just have to pick x amt of days they are allowed to view and use that many potential variables.

Does that make sense?
0
Infinity08Commented:
>> I'm assuming I just have to pick x amt of days they are allowed to view and use that many potential variables.

You can use a STL vector for that. Something similar to :

        std::vector day_sales;
        double day_sale = 0.0;
        while (1) {
            std::cin >> day_sale;
            if (day_sale < 0.0) break;                  // <--- stop input when the user enters a negative value
            day_sales.push_back(day_sale);      // <--- add the input'ed value to the vector
        }
        // now you can sum the elements in the vector, and show the result

You'll of course have to add some output to instruct the user what to enter etc.

Do you know vectors ? If not, take a look here :

        http://www.cplusplus.com/reference/stl/vector/
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

WestonGroupAuthor Commented:
I don't know vectors but I will look at your link
0
evilrixSenior Software Engineer (Avast)Commented:
Below is a simple way you could do this.
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
 
int main()
{
	double dTotal = 0.0;
	for(unsigned int nDay = 1 ; ; ++nDay)
	{
		std::cout << "Enter sales figures for day " << nDay << " ('QUIT' to finish and calcualte total): ";
		std::string sInput;
		std::cin >> sInput;
 
		if(sInput == "QUIT") { break; }
 
		std::stringstream ssInput(sInput);
		double dInput = 0;
		ssInput >> dInput;
		dTotal += dInput;
 
	}
 
	std::cout << "Total: " << std::setprecision(2) << std::fixed << dTotal;
}

Open in new window

0
WestonGroupAuthor Commented:
I tried that code... back to an old problem I have with my compiler dev c++ from bloodshed version 4

I complied the code and get this error

sstream no such file or directory
0
evilrixSenior Software Engineer (Avast)Commented:
>> sstream no such file or directory
You really need to sort this out :)
0
evilrixSenior Software Engineer (Avast)Commented:
Ok, try this. It's not quite as explicit to quit (any non-numeric will cause it to quit) but it should work for you.
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
 
int main()
{
	double dTotal = 0.0;
	for(unsigned int nDay = 1 ; ; ++nDay)
	{
		std::cout << "Enter sales figures for day " << nDay << " ('0' to finish and calcualte total): ";
		double dInput = 0;
		std::cin >> dInput;
		if(0 == dInput) { break; }
		dTotal += dInput;
	}
 
	std::cout << "Total: " << std::setprecision(2) << std::fixed << dTotal;
}

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
Or this, where you enter a negative value (as per I8's example).

The problem with this and the approach above it that 0 or negative may both be value values (possibly?). Unfortunately, unless you get stringstream <sstream> to work you'd a bit limited as to how you can process input!

-Rx.
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
 
void flush(std::istream & is)
{
	is.clear();
	char nextChar;
	while( (nextChar = is.get()) != '\n' && nextChar != EOF);
	is.clear();
}
 
int main()
{
	double dTotal = 0.0;
	for(unsigned int nDay = 1 ; ; ++nDay)
	{
		std::cout << "Enter sales figures for day " << nDay << " ('< 0' to finish and calculate total): ";
		double dInput = 0;
		std::cin >> dInput;
		flush(std::cin);
		if(dInput < 0) { break; }
		dTotal += dInput;
	}
 
	std::cout << "Total: " << std::setprecision(2) << std::fixed << dTotal;
}

Open in new window

0
Infinity08Commented:
So, you only need the total then, not all of the daily sales values ? Then you can indeed leave out the vector from my code, and get something similar to evilrix's last code.
0
Infinity08Commented:
>> that 0 or negative may both be value values (possibly?).

0 can be valid I guess (on a bad day). But negative sales ? ;)
0
evilrixSenior Software Engineer (Avast)Commented:
>> But negative sales
Refunds?
0
WestonGroupAuthor Commented:
that code works well

without the user having ot enter in how many days they want to calculate...what if I want them prompted to select how many days they want to calculate .. so "how many days do you want to calculate"

user selects x days and then starts the input..is that possible with the above code?
0
evilrixSenior Software Engineer (Avast)Commented:
Another way -- Enter the number of days to process at the start... no going back once entered though!
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
 
void flush(std::istream & is)
{
	is.clear();
	char nextChar;
	while( (nextChar = is.get()) != '\n' && nextChar != EOF);
	is.clear();
}
 
int main()
{
	std::cout << "How many days to process? ";
	unsigned int nDayCnt;
	std::cin >> nDayCnt;
 
	double dTotal = 0.0;
	for(unsigned int nDay = 0 ; nDay < nDayCnt ; ++nDay)
	{
		std::cout << "Enter sales figures for day " << (nDay + 1) << ": ";
		double dInput = 0;
		std::cin >> dInput;
		dTotal += dInput;
		flush(std::cin);
	}
 
	std::cout << "Total: " << std::setprecision(2) << std::fixed << dTotal;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
It seems I read your mind :)
0
Infinity08Commented:
>> user selects x days and then starts the input..is that possible with the above code?

Isn't that what you had in the beginning ? ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.