C++


Ad: Buy Girls Und Panzer Merch from Play Asia!

Kashisu

-san
Kouhai
hey guys, so quarter ends tmw and all programs are due in my C++ class. I have them all done except for one, where we have to write a class called gas station.....so i was wondering if someone who knows c++ could help me out and tell me what's wrong with this?? it won't return the right values, just some random number**i'm pretty sure the thing that runs it is accurate**

sorry this is so long, but thanks!
and yes, i kno im quite dumb when it comes to c++
CODE
class GasStation
{

public:

int gallonsLeft;
int totalGallonsSold;
double mySellPrice;
double myCost;
double myProfit;
int myGallonsSold;
double tempProfit;
double fillerProfit;
double myGrossProfit;

GasStation()
{
}
void GasStation::Reset(void)
{
mySellPrice=0;
myCost=0;
myProfit=0;
myGallonsSold=0;
}

void GasStation::setGallonsSold(int c)//sets the number of gallons sold
{
myGallonsSold=c;
}
void GasStation::setSellPrice(double a)//sets the selling price
{
mySellPrice=a;
}
void GasStation::setCost(double B)//sets the cost per gallon that the station pays
{
myCost=b;
}
void GasStation::findGallonsLeft()//finds the number of gallons left when starting with 200
{
gallonsLeft=gallonsLeft-myGallonsSold;
}
void GasStation::findTotalGallonsSold()//finds the total number of gallons sold
{
totalGallonsSold=200-gallonsLeft;
}
void GasStation::findTempProfit()//finds the profit from one sale
{
tempProfit=myGallonsSold*mySellPrice;
}
void GasStation::findFillerProfit()//junk that i need for gross profit
{
fillerProfit=999999-tempProfit;
}
void GasStation::findGrossProfit()//finds the gross profit
{
myGrossProfit=999999-fillerProfit;
}
void GasStation::findNetProfit()//finds the net profit
{
myProfit=myGrossProfit-200*myCost;
}
int GasStation::getTotalGallonsSold()//returns the total number of gallons sold
{
return totalGallonsSold;
}
double GasStation::getGrossProfit()//returns the gross profit
{
return myGrossProfit;
}
double GasStation::getNetProfit()//returns the net profit
{
return myProfit;
}
};


edit: remember to use code tags.
 
you're using uninitialized values. you have a ton of variables that your functions are using without them being set to something.

ie:

CODE
int x;
int y = 4

return x - y; <--- returns a random number because x is uninitialized
 
oops sorry
sad.gif


i thought the variables got initialized in the .cpp file?? that was like the .h file.....
well the .cpp file looks like this:
CODE #include "GasStation.h"
#include <iostream>
using namespace std;

void main()
{

double mySellPrice;
double myCost;
double myProfit;
int myGallonsSold;
int sort;
int karen;
karen=0;

GasStation petro;

for(karen=karen;karen!=999999;karen++)
{
cout<<"Do you want to:"<<endl
<<"1. Reset all values:"<<endl
<<"2. Input the number of gallons sold: "<<endl
<<"3. Set a sell price for the gasoline: "<<endl
<<"4. Set the cost of gas per gallon for the station: "<<endl
<<"5. Find out the total number of gallons sold "<<endl
<<"6. Find the gross profit from the total gas sales:"<<endl
<<"7. Find the net profit from the total gas sales: "<<endl
<<"8. exit"<<endl;
cin>>sort;

if(sort==1)
{
petro.Reset();
}
if(sort==2)
{
cout<<"Please enter the number of gallons sold: ";
cin>>myGallonsSold;
petro.setGallonsSold(myGallonsSold);
}
if(sort==3)
{
cout<<"Please enter a selling price for the gasoline: ";
cin>>mySellPrice;
petro.setSellPrice(mySellPrice);
}
if(sort==4)
{
cout<<"Please enter the cost per gallon the station incurs: ";
cin>>myCost;
petro.setCost(myCost);
}
if(sort==5)
{
cout<<"The total number of gallons sold so far is "<<petro.getTotalGallonsSold();
}
if(sort==6)
{
cout<<"The gross profit so far is: "<<petro.getGrossProfit();
}
if(sort==7)
{
cout<<"The net profit so far is: "<<petro.getNetProfit();
}
if(sort==8)
{
karen=999998;
}
cout<<endl;

}
}
 
C++ is a programing code like Delphi and Borland Pascal. I had Borland Pascal Class at my school.
 
i did Java and Visual at college, im pretty sure VB is close to C++, and i can't really see any errors there. What exactly are u trying to do?
 
it was just a class that find like accumulated inputs and stuff and calculates them with the class functions.... but thanks everyone!! i finished it and turned it in today!!

@daft.... thanks so much!! i finally figured out what you meant by initializing variables in the class, lol
smile.gif
 
Strictly speaking yes. But usally people mean to assign a value to the variable before you actually use it.

But it is always safer and a better design practice to assign a value right when you declare the variable. That way you are sure you wont forget to initialize it when you actually use it.

QUOTE
oh...... i see!
so wat are Delphi and Borland Pascal


Delphi, Pascal and C are all programming language. C being one of the most used today, Pascal being one of the ancestor of C and was used a lot (as in everywhere) about 10 years ago. Delphi is a kind of modern Pascal variation made by Delphi. It added new capabilities to Pascal to make it a modern programming language. The only problem is its not a standard language. So you need to buy a compiler (the program used to make the program you coded) from Delphi. No one else makes one.

Hope this little history class was interesting!

I never taught my very first post on this forum would be about C++ and not anime!!
tongue.gif
 
QUOTE (ultima2096 @ Oct 14 2005, 06:37 PM) i did Java and Visual at college, im pretty sure VB is close to C++, and i can't really see any errors there. What exactly are u trying to do?
VB isn't an Object Oriented Language. I believe it's classified as an Imperative or Functional Language while C++ is Object Oriented...If I said they were similar my professors would kill me
unsure.gif
But there was a time when I thought the same thing
wink.gif
 
QUOTE (Chiba @ Oct 15 2005, 12:36 PM)VB isn't an Object Oriented Language. I believe it's classified as an Imperative or Functional Language while C++ is Object Oriented...If I said they were similar my professors would kill me
unsure.gif

Actually, VB is object oriented (it has class, inheritance, etc). VB 6 and before were capable of objects, but not all people were using those features. VB .Net (latest version) is completely object oriented.

The confusino comes from the fact that Basic and QBasic (the ancestor VB were absolutely not object oriented)

But you are right saying many people would kill you for saying VB and C are similar! They are based on different programming principle. VB is for rapid application developpement. Its fast to write an application in VB. But your are wasting system ressource (meaning yuo will need a faster machine to do the job). In C, you can optimize and code a lot. But I can take more time to have an application working right.

Its an old debate. Fast developpemnt or fast program.
 
Oh, awesome, I was unaware it went OO. Mwuahahahahahaha, now to go piss off my programming language concepts professor and make him fall out of his rolly chair
laugh.gif
 
QUOTE (Chiba @ Oct 15 2005, 05:36 PM) VB isn't an Object Oriented Language. I believe it's classified as an Imperative or Functional Language while C++ is Object Oriented...If I said they were similar my professors would kill me
unsure.gif
But there was a time when I thought the same thing
wink.gif

like i said i've never used C++, so i assumed it was the same as VB... No one needs to killed.
The only thing i really remeber doing in VB (it was a while ago) was making space invaders, where one of then would randomly go diagonally at a certain point, never did figure out what i did wrong.
is there quite alot you who study computer programming then at college/uni?
 
QUOTE (ultima2096 @ Oct 15 2005, 10:17 PM)is there quite alot you who study computer programming then at college/uni?
Its my last year of my bachelor in computer ingeneering.
And I have to say, I'm really looking forward to finishing. Right now I'm also working for a computer board company and its so much fun than being in a classroom!!

Anyway, so programming is kind of something I do a lot. I specilize in hadware interfacing. As in: "I want to have this chip talk to this other chip on a board".
 
Playasia - Play-Asia.com: Online Shopping for Digital Codes, Video Games, Toys, Music, Electronics & more
Back
Top