ZachDoty Posted February 15, 2005 Share Posted February 15, 2005 Hello,My friend is learning c++ and is making a test program, but he can't get it work right, here is the code.#include <iostream>#include <ctime>#include <stdlib.h>#include <string>#include <windows.h>using namespace std; int main( void );class Gun{ //data member declarations string color; bool draw; bool lock; int numOfBullets;public: Gun(string aColor); //constructor ~Gun(); //destructor //methods void drawn(); void locked(); int fire();};Gun::Gun(string aColor){ numOfBullets = 10; draw = false; lock = false; color = aColor; srand((unsigned)time(0)); //seeds the time (we need the rand() function in the fire() method)}Gun::~Gun(){}//draws the gunvoid Gun::drawn(){ if(lock) { cout<<"gun has been locked and therefore cannot be loaded.\n"; } draw = true; cout<<color<<"gun has been loaded.\n" <<endl;}//locks the gunvoid Gun::locked(){ if(!draw) { cout<< color << "gun has not been drawed.\n" <<endl; } if(draw) { lock = true; cout<<color<<"gun has been locked.\n" <<endl; } }//fires the gun if lockedint Gun::fire(){ if(!draw) { cout<< color << "gun has not been loadedand therefore could not fire.\n" << endl; return 0; } int score; score = rand() % (10 - 0 + 1) + 0; if(score == 0) cout<<color<< " missed the target!!!\n" <<endl; else cout<< color << " scored " << score << " points!!!\n" <<endl; return score;}//the main functionint main(void){ int go; char load; char lock; system("cls"); cout<<"Gun Control - TS Software V1.0\n\n"; cout<<"Welcome to gun control!\n\nToday you will learn how to aim, draw, and fire a gun at" " a target. Good Luck!\n"; Sleep( 1000 ); cout<<"\nGlad you agreed. Come on over.\n\n"; cout<<"Ok, now we are going to use the nice, pretty old, black shotgun.\n\n"; Gun shotgun("The old, black shot"); cout<<"To load the shotgun type in load. You will then be told that is loaded.\n"; cin>>load; cout<<"\n"; if (load = load) { shotgun.drawn(); } cout<<"Then you would have to lock it, type in lock.\n"; cin>>lock; if (lock = lock) { shotgun.locked(); } /*shotgun.draw(); shotgun.fire(); shotgun.loced();*/ return 0;}Though I don't know c or c++ I think that the main problem is the if statements, I have read a few tutorials recently and it looks like the if statement isn't working. Shouldn't the if statements have two equal signs? And even then it looks like it is comparing the variable against itself and that would always be true, well, those are just my ideas, I don't know enough to fix it, but I thought that someone here would!Thanks In Advance!Zach Doty Link to comment Share on other sites More sharing options...
zprog Posted February 15, 2005 Share Posted February 15, 2005 I didn't read through the whole code, but if you say the problem is around the if() statements, then you are essentially assigning load to load. You might have meant "load == load" and "lock == lock", but that will always return true, because 2 always equals to 2. Link to comment Share on other sites More sharing options...
ZachDoty Posted February 16, 2005 Author Share Posted February 16, 2005 you are essentially assigning load to load. You might have meant "load == load" and "lock == lock", but that will always return trueHow would I rewrite that if statement to work better? Link to comment Share on other sites More sharing options...
dman Posted February 16, 2005 Share Posted February 16, 2005 you are comparing the string read by cin stored in lock variable. You need to tell if that variable contains the word "lock". compare string variable against string literal looks like this:if (lock == 'lock')if (load == 'load')the code still doesn't work, but this maybe gets you to next step. Link to comment Share on other sites More sharing options...
The Unicorn Posted February 16, 2005 Share Posted February 16, 2005 Okej, first change the following:char load;char lock;tochar load[10];char lock[10];Then Changeif (load = load) {shotgun.drawn(); }//andif (lock = lock) {shotgun.locked(); }toif (strcmp(load, "load") == 0){shotgun.drawn(); }//andif (strcmp(lock, "lock") == 0) {shotgun.locked(); }The errors in the commented out code beneth is rather easy to fix but I haven't tried running the entire program. Link to comment Share on other sites More sharing options...
azagahl Posted February 17, 2005 Share Posted February 17, 2005 #include <stdlib.h>You can also use <cstdlib> srand((unsigned)time(0));Should be done in main probably, why reseed the random number generator every time you create a Gun? int go;Completely unused.char lock;You say "type in lock" later, and then you do cin>>lock.So this should be string, not a char (single letter).Same thing with load.cout<<"Welcome to gun control!\n\nToday you will learn how to aim, draw, and fire a gun at" " a target. Good Luck!\n";Use << endl to flush output; \n does not flush output.if (lock = lock)This assigns lock to lock, and then tests the value of lock.Maybe you want (lock == "lock") instead. Same problem with load.return 0;Returning a value from int main is optional. Link to comment Share on other sites More sharing options...
pZy Posted February 17, 2005 Share Posted February 17, 2005 return 0;Returning a value from int main is optional.Nope.Because main is returns an integer, it´s not optional....but VC++ is know for its OWN standards if (load = load)This really makes me laugh...there is NO C++ developer who didn´t do this mistake at least one time gd Link to comment Share on other sites More sharing options...
azagahl Posted February 19, 2005 Share Posted February 19, 2005 Nope.Because main is returns an integer, it´s not optional.You couldn't be more wrong. int main need not return a value in Standard C++. This works fine in MSVC++ .NET 2003, but not VC6. (VC6 has worse standards support.)...but VC++ is know for its OWN standardsActually I think MSVC++ .NET 2003 does a good job of supporting the Standards. Much better than VC6.The missing export keyword is the most glaring deviation. Link to comment Share on other sites More sharing options...
pZy Posted February 21, 2005 Share Posted February 21, 2005 int main need not return a value in Standard C++****, i looked it up and you are right.Although main() must be declared as int it doesnßt make sense for me that its return value is optional. VC6 has worse standards support.Will always remember things like this: for (int i = 0; i < 5; i++) cout << i << endl;for (int i = 0; i < 10; i++) cout << "\t" << i << endl;VC++6 - ERROR.Hm, would like to try if VC++ 2003 compiles this... Link to comment Share on other sites More sharing options...
azagahl Posted February 23, 2005 Share Posted February 23, 2005 Hm, would like to try if VC++ 2003 compiles this...It does.Also, template member functions are supported:class C{template <class T> foo() {}};Also static integral constants declared inside your class are supported:class C{ static int const i = 5;};Also, this is supported (no idea what its called )struct S {int x, y;}; //This works for POD (plain old data) onlyS *s1 = new S; //Create uninitialized instanceS *s2 = new S(); //Create instance and initialize its members to 0Also the wierd agressiveoptimize.h pragmas and stuff to optimize your executable size are no longer needed.Unfortunately VC++ is more bloated than ever Link to comment Share on other sites More sharing options...
joshuatree Posted July 27, 2005 Share Posted July 27, 2005 hey all, getting back to the original post, you didn't say why it wasn't working.? aside from the logic errors, there seems to be alot of redundancy. you could have save yourself some space and done an inline declaration on your constructor, and why is there 2 "int main()"?? I think you could get rid of the first one at the top. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now