• 13
• 18
• 19
• 27
• 9

# Newbie Question

This topic is 2764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Anyways, I started work on a money planner program (in C++) and ran into a few (probably) common newbie problems.

First off, can cin read anything else besides an intiger? I want it to read doubles, by the way.

Secondly, can you make a user-made function return a double? If so, how is this done? I am making a function that calculates total gas money needed and then outputs a total which is then given to another function which adds up all the money totals (gas price, extra money for emergency gas and such, trip money (to buy souvenirs, food and stuff like that) and more things later on) and then finally gives its output to cout.

While on that last topic, can I round doubles to the nearest tenth? I don't want an unreadable and extremely long number thrown at the user. I was thinking of making another function to do this, but is there an easier way?

##### Share on other sites
cin can take any input you want. I suggest using cin.getline or cin.readline (I haven't coded in C++ in years now, so I might forget some names here and there.

Once you have your string, you can convert it to any data type you want.

A function can return any data type.

eg:
double ReturnTotal(const double input)
{
double Total = //whatever code;
}

You can use cout to format the output of your double.

http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

cout << setprecision (2) << yourdoublevariable;

##### Share on other sites
cin can read doubles, as well as chars, strings, ints, and floats. All you need to do is cin >> myVariable; and have myVariable be the desired type (in this case just declare it as a double).

User made functions can return doubles. When you are writing the function, just use double as the return type:
double myFunction(){  //Some code  return myDoubleVariable;}

Should give you an idea about some of that, at the very least the decimal part:
Quote:
 A special "magic formula" for controlling how many decimal places are printed: cout.setf(ios::fixed); // specifies fixed point notation cout.setf(ios::showpoint); // so that decimal point will always be shown cout.precision(2); // sets floating point types to print to // 2 decimal places (or use your desired number)

##### Share on other sites
Quote:
 Original post by Daaarkcin can take any input you want. I suggest using cin.getline or cin.readline (I haven't coded in C++ in years now, so I might forget some names here and there.Once you have your string, you can convert it to any data type you want.A function can return any data type.eg:double ReturnTotal(const double input){ double Total = //whatever code; return Total;}You can use cout to format the output of your double.http://www.cplusplus.com/reference/iostream/manipulators/setprecision/cout << setprecision (2) << yourdoublevariable;

Thanks. Does cin.getline and cin.readline read strings too? I get errors when I try to read strings with just cin. Maybe I forgot to #include "string" (or "string.h", I'm not sure).

##### Share on other sites
What kind of errors? Post the compiler messages.

##### Share on other sites
Quote:
 Original post by DragonsouljWhat kind of errors? Post the compiler messages.

It was a while ago, so I forget now. I probably initialized the string as a integer.

Edit: Your link was broken. There was a quote at the end.

##### Share on other sites
Either way, that last link has some info on cin and cout as well as getline.

##### Share on other sites
Quote:
 Original post by SomeGuy49Thanks. Does cin.getline and cin.readline read strings too? I get errors when I try to read strings with just cin. Maybe I forgot to #include "string" (or "string.h", I'm not sure).

#include <string> <-- like that

Yes, getline reads strings. But you must #include <string> for getline to recognize strings.

With getline you capture the input into a string, and then you can do some sanity checking on the values to make sure you have good input before converting it into a double. But after reading your next posts, that might be a little advanced for you.

##### Share on other sites
Alright, this is the program, in all its glory. Is there any errors? It ran perfectly in my computer. Is there ways to make it simpler?

#include <iostream>
#include <string>
using namespace std;

double gascalc(double length, double mpg, double price)
{
double gasmoney;
//The following calculates money needed for gas.
gasmoney = length / mpg * price;
return gasmoney;
}

double maincalc(double gas, double extra, double spend)
{
double moneytotal;
moneytotal = gas + extra + spend;
return moneytotal;
}

int main()
{
cout << "(=)This is a money calculator for Canadian road trips(=)" << endl;
cout << "How long is the road trip (miles)?" << endl;
double milength;
cin >> milength;
cout << "What is the average (hwy/city average) MPG of the car?" << endl;
double MPG;
cin >> MPG;
cout << "What is the average Canadian gas price (in USD)?" << endl;
double gasprice;
cin >> gasprice;
cout << "How much money do you want to spend on things there (food included)?" << endl;
double spendmoney;
cin >> spendmoney;
//The following is a simple equation that finds how much money you should take
//as extra in case you want to buy more things than you originally thought.
double extramoney = gasprice / 2 + spendmoney * 1.2;
double gasmoney = gascalc(milength, MPG, gasprice);
double finaltotal = maincalc(gasmoney, extramoney, spendmoney);
cout << "It will cost: " << finaltotal << endl;
return 0;
}

##### Share on other sites
Looks good from here, but you will want to use the source tags next time, it will make it easier to read, as it preserves the indentation of the code.

The only thing I would do differently is declare the variables at the top of the function, and then space out sections of the code with blank lines just to break it up a bit.

You could eliminate a lot of lines just by skipping the variable assignments.

double moneytotal;
moneytotal = gas + extra + spend;
return moneytotal;

can be replaced with

return (gas + extra + spend);

#include <iostream>#include <string>using namespace std;double gascalc(double length, double mpg, double price){	return length / mpg * price;}double maincalc(double gas, double extra, double spend){	return gas + extra + spend;}int main(){	double milength;	double MPG;	double gasprice;	double spendmoney;	cout << "(=)This is a money calculator for Canadian road trips(=)" << endl;	cout << "How long is the road trip (miles)?" << endl;	cin >> milength;	cout << "What is the average (hwy/city average) MPG of the car?" << endl;	cin >> MPG;	cout << "What is the average Canadian gas price (in USD)?" << endl;	cin >> gasprice;	cout << "How much money do you want to spend on things there (food included)?" << endl;	cin >> spendmoney;	//The following is a simple equation that finds how much money you should take	//as extra in case you want to buy more things than you originally thought.	double extramoney = gasprice / 2 + spendmoney * 1.2;	double gasmoney = gascalc(milength, MPG, gasprice);	double finaltotal = maincalc(gasmoney, extramoney, spendmoney);	cout << "It will cost: " << finaltotal << endl;		return 0;}