I get a run time error saying something about memory locations when I run
this program.How can I make this program work using pointers???
//this program converst miles to km
#include<iostream>
#include "std_lib_facilities.h"
using namespace std;
//==========func declarations====================
void get_mi(double* mi);
void do_math(double* mi, double* km);
void show_math(double* mi, double* km);
//===============main func=========================
int main ()
{ int exit = 0;
do
{
double *mi = 0, *km = 0; //intialize miles and kilometers
get_mi(mi);//get the number of miles
do_math( mi, km);//convert mi to km
show_math(mi, km);//show math to user
cout<<"Enter 1 to repeat, anything else to quit." << endl;
cin >> exit;
}
while(exit == 1 );
return 0;
}
//==============func definitions===========================
void get_mi(double* mi)
{
cout<< "Enter the distance in miles to convert to km: " << endl;
cin >> *mi;
}
void do_math(double* mi, double* km)
{
*km = (*mi) * 1.609; // converts mi to km;
}
void show_math(double* mi, double* km)
{
cout << *mi << "miles is equal to " << *km << "kilometers." << endl;
}
For example you can write the following way
int main ()
{ int exit = 0;
do
{
double mi = 0, km = 0; //intialize miles and kilometers
get_mi(&mi);//get the number of miles
do_math( &mi, &km);//convert mi to km
show_math(&mi, &km);//show math to user
cout<<"Enter 1 to repeat, anything else to quit." << endl;
cin >> exit;
}
while(exit == 1 );