Computing.Net > Forums > Programming > Need Help With C++ cin command

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need Help With C++ cin command

Reply to Message Icon

Name: NarrowPathPilgrim
Date: February 14, 2005 at 23:44:58 Pacific
OS: XP
CPU/Ram: 2.53 GH'z 512 MB
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: February 15, 2005 at 09:54:53 Pacific
Reply:

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


0

Response Number 2
Name: NarrowPathPilgrim
Date: February 15, 2005 at 11:59:44 Pacific
Reply:

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

My Computer Help Forum


0

Response Number 3
Name: Dark_Kartug
Date: February 15, 2005 at 15:16:09 Pacific
Reply:

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 be

if (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_Kartug

War sucks, but the sound is good and WE are the DJ's


0

Response Number 4
Name: BlueRaja
Date: February 15, 2005 at 18:34:40 Pacific
Reply:

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


0

Response Number 5
Name: BlueRaja
Date: February 15, 2005 at 18:57:25 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: NarrowPathPilgrim
Date: February 15, 2005 at 22:15:48 Pacific
Reply:

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


0

Response Number 7
Name: Don Arnett
Date: February 16, 2005 at 05:19:29 Pacific
Reply:

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!


0

Response Number 8
Name: Dark_Kartug
Date: February 16, 2005 at 15:15:54 Pacific
Reply:

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_Kartug

War sucks, but the sound is good and WE are the DJ's


0

Response Number 9
Name: BlueRaja
Date: February 16, 2005 at 17:36:59 Pacific
Reply:

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

BlueRaja.admin@gmail.com


0

Response Number 10
Name: BlueRaja
Date: February 16, 2005 at 17:37:54 Pacific
Reply:

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

BlueRaja.admin@gmail.com


0

Response Number 11
Name: clanmagic000
Date: February 16, 2005 at 17:43:09 Pacific
Reply:

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...


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Need Help With C++ cin command

needing help with an if command www.computing.net/answers/programming/needing-help-with-an-if-command/19984.html

Need help with C++? www.computing.net/answers/programming/need-help-with-c/4642.html

Need help with C++ program www.computing.net/answers/programming/need-help-with-c-program/19441.html