#include <iostream.h>

void askforprices();
double rate (double old_price, double new_price);


int main()

{
	askforprices();


	return 0;
}


void askforprices()
{
	
	double old_price, new_price, rate;

	cout << "Please enter old price\n";
	cin >> old_price;

	cout<<"Please enter new price\n";
	cin >> new_price;

	rate = rate(old_price, new_price); //points to this location for an error


}

double rate(double old_price, double new_price)

{

	double rate;

	rate = (new_price - old_price)/old_price;

	return rate;

}

<< moderator edit: added code tags: [co[u][/u]de][/co[u][/u]de] >>

It uses deprecated header names and it doesn't output the answer.

Also, you have a variable and a function with the same name. That's making the compiler confused. Rename your variables and such so that they do not collide.

yea i think i got it working if you can please check if i did it properly i would greatly appreciate it. Here is what i came up with.

#include <iostream.h>

void askforprices();
double rate (double old_price, double new_price);


int main()

{
	askforprices(); 


	return 0;
}


void askforprices()
{
	
	double old_price, new_price, newrate;

	cout << "Please enter old price\n";
	cin >> old_price;

	cout<<"Please enter new price\n";
	cin >> new_price;

	cout<< "Your New Rate is:" <<(new_price - old_price)/old_price;

	newrate = rate(old_price, new_price);


}

double rate(double old_price, double new_price)

{

	double rate;

	rate = (new_price-old_price)/old_price;

	return rate;

}

<< moderator edit: added code tags: [co[u][/u]de][/co[u][/u]de] >>

yea i think i got it working if you can please check if i did it properly i would greatly appreciate it. Here is what i came up with.

Does it do what you expect? Perhaps you could tell us what you expect it to do. If this is what you want:

Please enter old price
120
Please enter new price
150
Your New Rate is:0.25

Then it would appear to be correct.

And learn to use code tags: [co[u][/u]de][/co[u][/u]de] .

The thing is im not sure if my formula is correct? oldprice - newprice/ oldprice = rate of inflation?

Here is the actual description of what the program is suppose to do.

write a program to gauge the rate of inflation for the past year. The program asks for the price of an item (such as a hot dog or a one carat diamond) both one year ago and today. It estimates the inflation rate as the diffeerence in price divided by the year ago price. Your porgram should allow the user to repeat this as often as the user wishes. Define a function to compute the rate of inflation. The inflation rate should be a value of type double giving the rate as a percent.

Your porgram should allow the user to repeat this as often as the user wishes. Define a function to compute the rate of inflation. The inflation rate should be a value of type double giving the rate as a percent.

So use your function to do the work, rather than doing the work and merely having the function exist as well.

Take a stab at a simple loop that will "allow the user to repeat this as often as the user wishes". Then post what you have.

The calculation does appear correct to me.

#include <iostream.h>

void askforprices();
bool again();
double rate (double old_price, double new_price);


int main()
{

	askforprices(); 
	again();


	

	return 0;
}

bool again()
{
	bool go,check;
	go = true;

while(go)
{
	
	askforprices();
	go = again();

	return 0;
}


void askforprices()
{
	
	double old_price, new_price, newrate;

	cout << "Please enter old price\n";
	cin >> old_price;

	cout<<"Please enter new price\n";
	cin >> new_price;

	cout<< "Your New Rate is:"<<(new_price - old_price)/old_price<<"\n";				// or cout<< "Your New Rate is:"<<(new_price - old_price);
//	cout<<(new_price - old_price)/old_price;

	newrate = rate(old_price, new_price);


}

double rate(double old_price, double new_price)

{

	double rate;

	rate = (new_price - old_price)/old_price;

	return rate;

}

<< moderator edit: added code tags: [co[u][/u]de][/co[u][/u]de] >>

And learn to use code tags: [co[u][/u]de][/co[u][/u]de] .

And what might stop the infinite recursion?

bool again()
{
   bool go = true;
   while ( go )
   {
      askforprices();
      go = again();
   }
   return 0;
}

I am not sure. I never messed with the bool type before. My professor just gave us that as a hint?

so if possible i would really like some advice/help with this program.

Poor hint. This method needs improvement, but it's a better hint.

#include <iostream>
using namespace std;

double rate(double old_price, double new_price)
{
   return(new_price - old_price) / old_price;
}

bool askforprices()
{
   double old_price, new_price;
   char reply;

   cout << "Please enter old price\n";
   cin >> old_price;

   cout<<"Please enter new price\n";
   cin >> new_price;

   cout<< "Your New Rate is:" << rate(old_price, new_price) << "\n";

   cout << "Continue? [y/n]";
   return cin >> reply && reply == 'y';
}

int main()
{
   while ( askforprices() )
      ;
   return 0;
}

Dave you replaced my function

double rate(double old_price, double new_price)

{

    double rate;

    rate = (new_price - old_price)/old_price;

    return rate;

}

with return(new_price - old_price) / old_price;?
it's a lot clean i didn't know you could do that.

Edited 3 Years Ago by pyTony: fixed formating

This article has been dead for over six months. Start a new discussion instead.