We help IT Professionals succeed at work.

# How to calculate large figures

on
Medium Priority
254 Views
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
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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).

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?
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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/

Commented:
Senior Software Engineer (Avast)
CERTIFIED EXPERT

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;
}
``````

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
Senior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> sstream no such file or directory
You really need to sort this out :)
Senior Software Engineer (Avast)
CERTIFIED EXPERT

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;
}
``````
Senior Software Engineer (Avast)
CERTIFIED EXPERT

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;
}
``````
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> that 0 or negative may both be value values (possibly?).

0 can be valid I guess (on a bad day). But negative sales ? ;)
Senior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> But negative sales
Refunds?

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?
Senior Software Engineer (Avast)
CERTIFIED EXPERT
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;
}
``````
Senior Software Engineer (Avast)
CERTIFIED EXPERT

Commented: