Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello, I have a friend who is having problems with some c++ code that he wrote, I don't know enough to help but I thought that someone here would!
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 gun
void 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 gun
void 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 locked
int 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 function
int 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;
}
He thought that the problem might be the cin command
Thanks in advance!
Zach Doty

OK, are we supposed to just guess what the problem is or are you going to tell us?
What is the program supposed to do that it isn't doing?
Michael J

Well, I think the problem is in this part of the code (spicificly the if statements), but I am not sure, do you see anything wrong in it?
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(); }Thanks Again
Zach Doty

Well im far from being a pro at any programming language really...but isnt the variable load and the variable lock of type BOOL and not enum or something similar? Hence the if statements if(load = load) and if(lock = lock) should be expecting true or false.
So in my noob opinion it should beif (load = true) {
shotgun.drawn(); }
cout<<"Then you would have to lock it, type in lock.\n";
cin>>lock;
if (lock = true) {
shotgun.locked(); }Hope i didnt make a fool outta myself :p
Cheers
Dark_KartugWar sucks, but the sound is good and WE are the DJ's

I haven't taken a look at the code, or really read anything other than "I think the problem is in this part of the code (spicificly[sic] the if statements)", and already I can see a major problem:
The "=" operator is called the assignment operator. It takes whatever is to the left of it and gives it the value of whatever is to the right of it. So the statement "load = load" tells the program to take the variable load and assign it whatever value has been given to the variable load - if this seems pointless to you, then you're starting to catch on. This value is then passed on to whatever called it - in this case, the "if" operator. So the statement
if (load = load)
is eqivalent to
if (load)
and, since, in C, zero represents a boolean false and every other number represents a boolean true, the statement can once again (simply for readibility, for those new to the language) be rewritten as
if (load != 0)
I don't know if that's the effect you want, but that's what the program is going to see it as.A typical beginner error is to confuse the assignment operator, "=", with the conditional operator, "==". This operator simply checks for equality between the variable/condition immediately left and immediately right of it - it then return a 1 (true) or 0 (false) accordingly.
However, I doubt the intent was to compare the variable load with the variable load: the following statement
if (load == load)
would *always* evaluate to true (because load would always equal itself).I'm still not sure what you were trying to accomplish with this code, but my advise would be to learn basic boolean logic before attempting to program using OOP.
BlueRaja.admin@gmail.com

A few more things I noticed while quickly glancing over the code:
1. bool types, unlike what Dark said, are not given values of true and false. Although true and false are considered reserved keywords in C++, they're not technically part of the language, so using them may or may not work with your compiler. You should use "1" in place of "true" and "0" in place of "false".
By the way, Dark, the local variables lock and load in main() are type char, not bool.2. ...I'm not sure what the quotes in the middle are there for in the following line:
cout<<"Welcome to gun control!\n\nToday you will learn how to aim, draw, and fire a gun at"
" a target. Good Luck!\n";3. score = rand() % (10 - 0 + 1) + 0;
Uh...why not just say rand()%11?
Note, however, that if this were a serious project, this would not be an acceptable way to generate a random number, as simply taking the modulus of the pseudorandom number focuses entirely on the lower bits of the integer, which are typically much less random than the higher bits (this, of course, depends on your compiler's implementation of the rand() function).4. Ohhhhh
I think I see what he's trying to do now - he wants to compare the variable load with the string "load". If that's the case, he should use (for reasons already mentioned by me) if(load=="load")The overall goal still eludes me, however...
Hope that helps.
BlueRaja.admin@gmail.com

Yes, I am trying to compare the variable load with the string "load", but using "if (load == "load")" doesn't work for me, and as to the overall goal, well, its just a test project that a friend is making.
Thanks For Your Help
Zach Doty

the biggest problem is that the variable load is a single character but you are trying to store a string in it.
load and lock should both be character arrays of sufficient size to hold the value that you expect input to them. Better yet, just create one array for input and reuse it for both input statements.
Also, if you type the word 'load' followed by hitting <enter>, will cin store the newline in the array? If so, you need to account for that.
Be sure to come back and let us know if our suggestions helped!

Thanks for pointing that out Blue...i completly missed the char assignment (i only skimmed the code to the point where I first encountered load and lock variables...in that case the BOOL assignment).
Also I'll make sure to keep the true/false thing in mind.
Also one thing that Don mentioned...I know that C doesnt originally support strings like the string header file and function in C++ (at least i read that somewhere)...but if Zach is using C++ and has string class support then wouldnt it be easier to use the string object rather then a char array? Or what would be the benefit of char array in this case? Just out of curiosity
Cheers
Dark_KartugWar sucks, but the sound is good and WE are the DJ's

"wouldnt[sic] it be easier to use the string object rather then[sic] a char array<i/>"
-ya.BlueRaja.admin@gmail.com

oops..guess I should check my post before I hit submit *sigh* -- we need an edit button -_-
BlueRaja.admin@gmail.com

Ok, i compiled this. I fixed all the errors, but its not doing what i suppose its ment to do.
Notice in the if (load = load) statement, you can type anything in and it will still load. I wont release the version that i have fixed yet...

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |