| | |
inheritance
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2003
Posts: 58
Reputation:
Solved Threads: 0
#include <iostream.h>
#include <conio.h>
void gas();
void main()
{
float gal;
while(gal<=0)
gas();
cout<<"thanks for your help";
getch();
}
void gas()
{
float gal;
float total;
float miles;
float avg;
cout <<"enter the gallons of gas used (-1 to end): ";
cin >> gal;
cout <<"enter miles driven: ";
cin >> miles;
avg=miles/gal;
cout <<"the miles / gallon for this tank was "<<avg<<endl;
cout<<"\n";
}
------------------------------------------------------------------------------
i'm tring to get float gal value to be shared between gas and main.
so that if -1 is enter for gal the while loop will exit.
help would be aprecaited.
#include <conio.h>
void gas();
void main()
{
float gal;
while(gal<=0)
gas();
cout<<"thanks for your help";
getch();
}
void gas()
{
float gal;
float total;
float miles;
float avg;
cout <<"enter the gallons of gas used (-1 to end): ";
cin >> gal;
cout <<"enter miles driven: ";
cin >> miles;
avg=miles/gal;
cout <<"the miles / gallon for this tank was "<<avg<<endl;
cout<<"\n";
}
------------------------------------------------------------------------------
i'm tring to get float gal value to be shared between gas and main.
so that if -1 is enter for gal the while loop will exit.
help would be aprecaited.
•
•
Join Date: Jul 2003
Posts: 58
Reputation:
Solved Threads: 0
i have faced some problems.
1)when -1 is pressed it doesnt exit immediately it finishes the function gas() first.
2)i have to then find the average of all the averages entered. but avg varible over writes its result. so correct me if i'm wrong but wont i have to create a array for avg.
then i have to present the average of avg for the passed serveral cars when -1 is pressed.
i know its a bit confusing so help would be greatly appreciated. sorry for my ignorance i'm still new and learning.
1)when -1 is pressed it doesnt exit immediately it finishes the function gas() first.
2)i have to then find the average of all the averages entered. but avg varible over writes its result. so correct me if i'm wrong but wont i have to create a array for avg.
then i have to present the average of avg for the passed serveral cars when -1 is pressed.
i know its a bit confusing so help would be greatly appreciated. sorry for my ignorance i'm still new and learning.
•
•
Join Date: Oct 2003
Posts: 1
Reputation:
Solved Threads: 0
declare variable gal as global variable ( out of both the function above main() ) , remove local declaration of gal from both the function. you will be able to share same gal betn main and gas. I am showing you code snap
float gal = 0.0f;
main()
{
while( gal >= 0.0f)
{
gas()
}
}
gas()
{
// dont declare gal again in this function
....
cin<<gal; // same global gal
}
float gal = 0.0f;
main()
{
while( gal >= 0.0f)
{
gas()
}
}
gas()
{
// dont declare gal again in this function
....
cin<<gal; // same global gal
}
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- Inheritance (C++)
- Need example of how to use INHERITANCE! (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Inheritance & Derived Classes (C++)
Other Threads in the C++ Forum
- Previous Thread: Return Length of a string?
- Next Thread: problems wid cpp files
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





