?
Solved

How to calculate large figures

Posted on 2008-01-26
16
Medium Priority
?
242 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:WestonGroup
  • 7
  • 5
  • 4
16 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20749776
>> 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
 

Author Comment

by:WestonGroup
ID: 20749786
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20749812
>> 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:WestonGroup
ID: 20749817
I don't know vectors but I will look at your link
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20749818
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
 

Author Comment

by:WestonGroup
ID: 20749827
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20749831
>> sstream no such file or directory
You really need to sort this out :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20749844
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20749850
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20749853
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20749854
>> that 0 or negative may both be value values (possibly?).

0 can be valid I guess (on a bad day). But negative sales ? ;)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20749858
>> But negative sales
Refunds?
0
 

Author Comment

by:WestonGroup
ID: 20749864
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
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 20749867
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20749870
It seems I read your mind :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20749882
>> 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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question