Project to calculate money and time to spend all of it.

Hello, I'm trying to make a program that would calculate a certain sum of money and how long it would take you to spend it. This is a very sloppy rough draft. I'm trying to figure after the "if statment" how to return back to the beginning of the code. Also I would appreciate any other criticism on how I can better my project. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 #include <iostream>
#include <math.h>
using namespace std; 
int main()
{
	double days = 0.0;
	double hours = 0.0;
	double minutes = 0.0;
	double seconds = 0.0;
	//double money = 0.0;
	double spending = 0.0;
	double amount = 0.0;

	long long money = 0.0; 
	long long day = 0.0;
	long long hrs = 0.0;
	long long min = 0.0;
	long long sec = 0.0;



	cout << "This project is to determine" << endl;
	cout << "how long it would take to spend a certain amount of money" << endl;
	cout << "in a specified amount of time" << endl << endl;

	cout << "Enter the desired amount of money" << endl;
	cin >> money;

	if (money > 9223372036854775807 || money <= 0)
	{
		cout << "\nInvalid amount - Must be greater than $0 or less than $9,223,372,036,854,775,807 \n";
	}

	cout << "Enter how much would you want to spend - $" << endl;
	cin >> spending; 

	day = money / spending;
	hrs = money / spending * 24;
	min = hrs * 60;
	sec = min * 60;


	cout << "It would take this many days - " << day << endl;
	cout << "It would take this many hours - " << hrs << endl;
	cout << "It would take this many minutes - " << min << endl;
	cout << "It would take this many seconds - " << sec << endl;

	system("Pause");
	return 0;
}
It's not clear how it should work.
Lets say I have 1000£ and want to spend 500£
What should be the outcome?
Well lets say I have a $1,000. I chose to spend $1 a day.
It would take 1000 days. X amount of hours. X amount of seconds. etc.
Side note:
money > 9223372036854775807
If you're trying to detect overflow here, don't bother, because if that value can't be stored in a 64-bit integer, there's no way for money to be greater than the greatest number representable by a 64-bit integer.

I'm trying to figure after the "if statment" how to return back to the beginning of the code.
Use a while loop.
http://www.cplusplus.com/doc/tutorial/control/ --> See Loops section

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
	int n = 0;
	while (n != 42)
	{
		std::cout << "Enter 42: ";
		std::cin >> n;
	}

	return 0;
}
Last edited on
Something like this?


1
2
3
4
5
6
	while (money > 9223372036854775807 || money <= 0)
	{
		cout << "\nInvalid amount - Must be greater than $0 or less than $9,223,372,036,854,775,807 \n";

	}
You mean like this?
1
2
3
4
5
6
7
8
9
10
11
12
double available_money = 1000;
double amount_per_day = 500;

double days = available_amount / amount_per_day;
double hours = days * 24;
double minutes = hours * 60;
double seconds = minutes * 60;

cout << "It would take this many days - " << days << endl;
cout << "It would take this many hours - " << hours << endl;
cout << "It would take this many minutes - " << minutes << endl;
cout << "It would take this many seconds - " << seconds << endl;

Output:
It would take this many days - 2
It would take this many hours - 48
It would take this many minutes - 2880
It would take this many seconds - 172800
Press any key to continue . . .
Something like this?

No, you would be better off testing the stream state if you're worried about numeric overflow or non-digit values. If the value inputted is larger than the type can hold or a non-digit character is entered the stream will enter a fail state.


Yes @Thomas1965 exactly.

I tried my best to clean it up. Although its not perfect I hope you can see my intent with the code. Criticism and advice would be much appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <math.h>
#include <limits.h>
using namespace std; 
int main()
{
	long long days = 0.0;
	long long hours = 0.0;
	long long minutes = 0.0;
	long long seconds = 0.0;
	long long money = 0.0;
	long long spending = 0.0;
	long long amount = 0.0;


	cout << "This project is to estimate" << endl;
	cout << "how long it would take to spend a certain amount of money" << endl;
	cout << "in a specified amount of time" << endl << endl; 

	
	bool continueLoop = true;
		do
	{
	
	cout << "Enter the desired amount of money" << endl;
		cin >> money;


		if (money > 900,000,000,000,000,000 || money <= 0)
					
		{
			cout << "\nInvalid amount - Must be greater than 0 or less than 900,000,000,000,000,000 \n";
			system("Pause");
			return 0;
		};


		cout << "Enter how much would you want to spend daily - $" << endl;
			cin >> spending; 


			if (spending > money)

			{
				cout << "\nCan't spend more than whats available \n";
				system("Pause");
				return 0;
			};


			days = money / spending;
			hours = money / spending * 24;
			minutes = hours * 60;
			seconds = minutes * 60;


			cout << "It would take this many days - " << days << endl;
			cout << "It would take this many hours - " << hours << endl;
			cout << "It would take this many minutes - " << minutes << endl;
			cout << "It would take this many seconds - " << seconds << endl;

			system("Pause");
			return 0;
		} while (continueLoop == true);


	}
 
Last edited on
- return returns from the function.
- The function in question is main(). You'll be exiting the program.
- For the first two returns, were you instead looking for continue ?
- There are commas in your giant number -- does that even compile?
- semicolons after the 'if' statement brackets, probably not legal
- nothing ever changes your continueLoop flag; why even have a variable? while(true) { ... } prob simpler, or if you want a certain amount of complete runthroughs, while(count<3) , initialize count before while loop and increment that at the bottom after outputting the seconds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include iostream
#include math.h
#include limits.h
using namespace std; 
int main()
{
	long double days = 0.0;
	long long hours = 0.0;
	long long minutes = 0.0;
	long long seconds = 0.0;
	long double money = 0.0;
	long double spending = 0.0;
	long long amount = 0.0;


	cout  This project is to estimate  endl;
	cout  how long it would take to spend a certain amount of money  endl;
	cout  in a specified amount of time  endl  endl; 

	
	bool continueLoop = true;
		do
	{
	
	cout  Enter the desired amount of money  endl;
		cin  money;


		if (money  900000000000000000  money = 0)
					
		{
			cout  nInvalid amount - Must be greater than 0 or less than 900,000,000,000,000,000nn;
			continue;
		}


		cout  Enter how much would you want to spend daily - $  endl;
			cin  spending; 


			if (spending  money)

			{
				cout  nCan't spend more than whats available n;
				continue;
			}


			days = money  spending;
			hours = money  spending  24;
			minutes = hours  60;
			seconds = minutes  60;

			cout  It would take this many days -   days  endl;
			cout  It would take this many hours -   hours  endl;
			cout  It would take this many minutes -   minutes  endl;
			cout  It would take this many seconds -   seconds  endl;

			system(Pause);
			return 0;
		} while (continueLoop == true);


	} 




I've done this. I have two issues.
- I am not totally sure on my loop and would appreciate a more detailed example
- I am trying to find an integer for "days" that would handle large numbers and decimals
Something went wrong when you pasted your code, half of your symbols don't show up. Try re-posting.

As was mentioned by jlb, one option to detect valid input is to check the state of cin.
1
2
3
4
5
6
7
8
9
long long n;
if (std::cin >> n)
{
    std::cout << "Valid input.\n";
}
else
{
    std::cout << "Invalid input!\n";
}
Last edited on
closed account (E0p9LyTq)
I would appreciate any other criticism on how I can better my project.

Don't use the using namespace std; directive.

Starting out it seems easier to save a few keystrokes, but later you will run into problems with name clashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// why using namespace std is bad

#include <iostream>
#include <algorithm>

using namespace std;

unsigned int count;

int main()
{
   count++; // conflicts with std::count() in <algorithm>

   std::cout << count << '\n';
}

count in main() confuses the compiler, there is an ambiguous "match" between the global variable count and std::count in the <algorithm> header.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

Using global variables can also cause hard to detect runtime problems, if used without careful consideration.
Last edited on
Added a few tweaks:
- made all vars the same type (long double)
- added var for the upper limit
- to avoid scientific notation, manipulated cout to print with fixed notation and an arbitrary decimal precision
- bunched input pieces together
- bunched error-handling pieces together
- ask user for a single character to repeat and eat the newline after it
- made example bool variable 'repeat' to show how the choice can influence the loop (can also just move the logic over to the while loop condition)
- aligned some of the output for consistency

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>
#include <limits>
#include <locale>

using namespace std; 

struct ForceCommas : std::numpunct<char>
{
    char do_thousands_sep() const { return ','; }
    string do_grouping() const { return "\03"; }
};

int main()
{
    long double days = 0.0;
    long double hours = 0.0;
    long double minutes = 0.0;
    long double seconds = 0.0;
    long double money = 0.0;
    long double upper_limit = 900000000000000000;
    long double spending = 0.0;

    cout << "This project is to estimate how long it would " << endl << 
            "  take to spend a certain amount of money in a" << endl <<
            "  specified amount of time." << endl << endl; 
            
    // If you want fixed notation with two decimal points
    cout << fixed << setprecision(2);
    cout.imbue(locale(cout.getloc(), new ForceCommas));
    
    bool repeat;
    char redo_choice;
    do
    {
        cout << "Enter desired amount of money: $";
        cin >> money;
        cout << "         Enter daily spending: $";
        cin >> spending; 

        if (money <= 0 || money > upper_limit)
        {
            cout << "\nInvalid amount - Must be greater than 0 or less than "<< upper_limit << endl;
            continue;
        }
        if (spending > money)
        {
            cout << "\nCan't spend more than whats available \n";
            continue;
        }

        days = money / spending;
        hours = money / spending * 24;
        minutes = hours * 60;
        seconds = minutes * 60;
        
        cout << endl;
        cout << "   It would take this many days  " << days << endl;
        cout << "  It would take this many hours  " << hours << endl;
        cout << "It would take this many minutes  " << minutes << endl;
        cout << "It would take this many seconds  " << seconds << endl;

        cout << "\nAgain (y/n)? ";
        cin >> redo_choice;
        cin.ignore(numeric_limits<int>::max(), '\n');
        repeat = redo_choice=='y' || redo_choice=='Y';
    } while (repeat);
    
    return 0;
}

can test here https://repl.it/repls/PaleStarryKeyboard

example output:
This project is to estimate how long it would 
  take to spend a certain amount of money in a
  specified amount of time.

Enter desired amount of money: $ 900000000000000000
         Enter daily spending: $ 7

   It would take this many days  128,571,428,571,428,571.43
  It would take this many hours  3,085,714,285,714,285,714.25
It would take this many minutes  185,142,857,142,857,142,848.00
It would take this many seconds  11,108,571,428,571,428,571,136.00

Again (y/n)?  n


Edit: added commas to the locale for the lulz.
I Also realized the site i like to use, repl.it, adds an extra space after every 'cin', unlike cpp.sh. Weird; your output may vary slightly.
Last edited on
Thank you guys very much you all were a big help!
I really need to work on my loops and decimal formatting.
1
2
3
4
5
struct ForceCommas : std::numpunct<char>
{
    char do_thousands_sep() const { return ','; }
    string do_grouping() const { return "\03"; }
};


1
2
3
    // If you want fixed notation with two decimal points
    cout << fixed << setprecision(2);
    cout.imbue(locale(cout.getloc(), new ForceCommas));


What do these two parts of the code exactly do?
The second one sets it to two decimal points but what is the other code used for?
Topic archived. No new replies allowed.