Yay! it worked ;D thanks a lot for your help, i kinda get classes a little bit better now.
Yay! it worked ;D thanks a lot for your help, i kinda get classes a little bit better now.
when i do this:
am i making a instance named ptrBottle of class Waterbottle??Code:Waterbottle *ptrBottle; ptrBottle = new Waterbottle;
and if so, when i call a function would i have write ptrBottle.DrinkWater()??
i've finished my waterbottle program:
any suggestions on making it better?Code:#include <iostream> using namespace std; class Waterbottle { public: string Brand; float height; int WaterLevelPercent; void DrinkWater(Waterbottle *change) { change->WaterLevelPercent -= 10; if(change->WaterLevelPercent == 0) { cout << "No more water!" << endl; } } }; int main() { Waterbottle *ptrBottle; ptrBottle = new Waterbottle; string prompt,prompt1; ptrBottle->WaterLevelPercent=100; cout << "Do you want to drink water?" << endl; cin >> prompt; if(prompt == "Yes" || "yes") { drink: ptrBottle->DrinkWater(ptrBottle); cout << "You have " << ptrBottle->WaterLevelPercent << " % left" << endl; } cout << "Do you want to drink again?" << endl; cin >> prompt1; if(prompt1 == "Yes" || "yes" && ptrBottle->WaterLevelPercent > 0) { goto drink; } else { cout << "You can't drink anymore water because there is no more!" << endl; } return 0; }
You still haven't learned anything.
First off, why do you pass a pointer to ptrBottle->DrinkWater? Why? ptrBottle->DrinkWater implicitly belongs to ptrBottle. It's ptrBottle's DrinkWater function. Other Waterbottle have their own DrinkWater functions. This makes no sense.
This won't do what you think. Try entering "No" and it will still evaluate as true. You must be explicit:Code:if(prompt == "Yes" || "yes")
On a further note, I suggest you use std::getline and not cin >>. It only causes problems.Code:if(prompt == "Yes" || prompt == "yes")
You are using goto. Don't. Learn how to formulate logic using loops instead.
This won't do what you want either for the same reason as above.Code:if(prompt1 == "Yes" || "yes" && ptrBottle->WaterLevelPercent > 0)
The DrinkWater function BELONGS to the object. Every object has its own DrinkWater*. It knows to modify its own member variable. The object is "implicit", meaning you don't need to pass it in.
*Actually, that's not technically true, but think of it that way for now. It will help you understand.
I was wondering if you guys could think up a exercise using classes and ill try to make a program for it?? it doesnt have to be fancy and stuff