First app without tutorial holding my hand...

Hey to all. This is my first posting showing some code that I have come up with without a tutorial, like the title says. It's pretty simple in that I'm only a week into C++.
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
#include <iostream>
using namespace std;

	double x = 0; // always assign an intial value to a variable to ensure they don't carry something from memory
	int y = 0;


/*---------------------------------------------------------------------------*/
double a; // call the variables within the function to avoid global variables
double tipCalculator (double a)
// multiplying an input by 15% (typical tip) and storing that data
{
	return .15 * x;
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
double b;
double billTally(double b)
{
	// the bill and the tip combined
	return tipCalculator(a) + x;
}
/*---------------------------------------------------------------------------*/

/*int divideBill(int y)
{
	cin >> y;
}*/

/*---------------------------------------------------------------------------*/
double c;
double billPerPerson(double c)
{
	// taking the final total and dividing it between the people present
	return billTally(b) / y;
}
/*---------------------------------------------------------------------------*/

int main () 
{
	
	// gives the "bill" a floating point to allow decimal points
	cout << "Please enter the bill to see the tip required" << endl;
	// user input. use of new line for clarity on inputing number
	cin >> x;
	// store user input into variable for later use
	cout << "The bill is $";
	// notice no new line so that x will be displayed directly behind it
	cout << x << endl;
	cout << "The tip to the waitress is $";
	// no new line again for the same reason
	cout << tipCalculator(a) << endl;
	// use function to determine percentage of bill, in this case 15%
	cout << "And the bill including the tip will be $";
	cout << billTally(b) << endl;
	// this is the tip and the bill added together for the final tally
	cout << "And now enter the number of people in your party. ";
	cin >> y;
	cout << "Your bill is going to be $";
	cout << billPerPerson(c);
	cout << " per person." << endl;
	//cout << a << endl; for debugging purposes
	//cout << b << endl; for debugging purposes
	//cout << c << endl; for debugging purposes
	return 0;
}


Like I said, it's pretty simple. But, I was proud of it none the less. The idea came from playing on my phone one day and noticing an app that was just like this one and I took what I knew so far and applied it to that principle. It is working like I expected. The only thing that I have a question about is forcing a double to display two decimal places regardless of the output. But in all honesty I haven't had time yet to research that on my own. I was more looking for some advice on the structure of the application itself in regards to habits forming along the lines of employment. I have given it a lot of thought being into my last semester of my Associate's in Applied Sciences and Technology and think my next path will be a Bachelor's in Business Information and Technology. And with this path there will be extensible programming within the course and I would like to get a jumpstart before getting there. I saw in a thread just a little bit ago someone (I believe Disch) recommending against using using namespace std; and will take that knowledge forward. If anyone could take a quick look and just give a small review on how I am doing so far it would be much appreciated. My next project will be an Ohm's law calculator and I will post as a work in progress to get feedback as it goes. Thanks to all for reading.
You almost have the right idea here with the function parameters, but you're using globals, and are completely ignoring the values you pass to the functions.

You're also doing a few other weird things, like making global copies of the function params right above the function body.

4
5
//  double x = 0;
//  int y = 0;  // get rid of these lines.  You don't want global variables 


9
10
11
12
13
14
//  double a; // get rid of this, you're making a global here.  You don't want that
double tipCalculator (double a)  // 'a' is defined here, so you don't need above line
{
    //return .15 * x;  // don't use x, use the value passed to the function:  a
    return .15 * a;
}


billTally and billPerPerson have the same problem as tipcalculator.


After that, you just need to put defintion for 'x' and 'y' in main() (read: not globally). You might also want to give them more descriptive names. Then you should be good to go.


Lastly, coming from someone who worked in the service industry... 15% is a sorry tip. People should tip 20% ;P 20% + change if the service was really good. 15% is for bad service.
Last edited on
In case you were wondering it's perfectly ok to call functions in line with other data passed to cout. For example lines 51 and 53 can be written like this:
cout << "The tip to the waitress is $" << tipCalculator(a) << endl;

As for you question about getting the output to display with two decimal places: http://www.cplusplus.com/reference/iostream/ios_base/precision/
Last edited on
Thanks for the responses, guys. It took me a bit to get back to this. Here is what I have so far.

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
#include <iostream>
#include <iomanip>
using namespace std;


/*---------------------------------------------------------------------------*/
double tipCalculator (double tipFromBill)
// multiplying an input by 30% (typical tip) and storing that data
{
	double bill;
	tipFromBill = .30 * bill;
	return (tipFromBill);
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
double billTally(double tipAndBill)
{
	// the bill and the tip combined
	double tipFromBill, bill;
	tipAndBill = tipFromBill + bill;
	return (tipAndBill);
}
/*---------------------------------------------------------------------------*/
double billPerPerson(double billPerPerson)
{
	// taking the final total and dividing it between the people present
	double tipAndBill, howManyPeople;
	billPerPerson = tipAndBill / howManyPeople;
	return (billPerPerson);
}
/*---------------------------------------------------------------------------*/

int main () 

{

	cout << "Please enter the bill to see the tip required" << endl;
	// user input. use of new line for clarity on inputing number
	double bill = 0;
	cin >> bill;
	// store user input into variable for later use
	cout << "The bill is $";
	// notice no new line so that x will be displayed directly behind it
	cout << fixed;
    cout << setprecision (2) << bill << endl;
	cout << "The tip to the waitress is $";
	// no new line again for the same reason
	cout << fixed;
	cout << setprecision (2) << tipCalculator << endl;
	//use function to determine percentage of bill, in this case 30%
	cout << "And the bill including the tip will be $";
	cout << fixed;
	cout << setprecision (2) << billTally << endl;
	// this is the tip and the bill added together for the final tally
	cout << "And now enter the number of people in your party. ";
	unsigned int howManyPeople;
	cin >> howManyPeople;
	cout << "Your bill is going to be $";
	cout << fixed;
	cout << setprecision (2) << billPerPerson << " per person." << endl;
	return 0;
}


I tried to clean it up the best I could. I also removed the global variables. I'm still not positive if I am doing the functions correctly. The setprecision statement was working correctly until I altered the code to remove the variables. Now it's giving me a warning that the address will always return as true and is not outputting the correct values. If you enter $34 under the bill, it says the tip is $1, and the bill total is $1. I can't figure out what I'm doing wrong... Could someone please take a look again and point me further in the right direction please? And thank you very much for the help so far.
Topic archived. No new replies allowed.