Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 14th, 2005, 11:40 PM   #1
NarrowPathPilgrim
Newbie
 
Join Date: Nov 2004
Posts: 5
Rep Power: 0 NarrowPathPilgrim is on a distinguished road
Need Help With cin Command

Hello, I have a friend who is having problems with some 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

Last edited by NarrowPathPilgrim; Feb 15th, 2005 at 12:12 AM. Reason: change title
NarrowPathPilgrim is offline   Reply With Quote
Old Feb 15th, 2005, 2:10 AM   #2
NarrowPathPilgrim
Newbie
 
Join Date: Nov 2004
Posts: 5
Rep Power: 0 NarrowPathPilgrim is on a distinguished road
I think that this part of the code is the problem, am I right?
cin>>lock;
if (lock = lock) {
shotgun.locked(); }
NarrowPathPilgrim is offline   Reply With Quote
Old Feb 15th, 2005, 3:59 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
2 things i see wrong with the code to start with, when comparing things in a if statment use == not singal = as singla is an assignment operator and so will asign the value of lock to lock and always evaluates to true, but then again

if (lock == lock )

will always be true as you are comparing the same variable to each other.

change the code thus i think (might need to be corrected...)

if (load.Equals("load")) {//comparing a string to a string
         shotgun.drawn(); }
     cout<<"Then you would have to lock it, type in lock.\n";
     cin>>lock;
     if (lock.Equals("lock")) {//String comparison again(not sure if the command Equals is correct though.
         shotgun.locked(); }
Berto is offline   Reply With Quote
Old Feb 15th, 2005, 7:43 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
There are quite a few problems with this:
    char load[5]; // you need to specify the amount of characters
    char lock[5]; // same with this one

Here's the real problem:
     cin>>load; // this is fine
     cout<<"\n";
     if (load = load) {
This is wrong - it works like this:
if (load == "load")
Unfortunately, because load is an array, you can't do this. What you actually need to do use the strcmp function to compare the two strings:
if (strcmp(load, "load") == 0)
strcmp is a function that compares two strings. It returns 0 when the two strings are equal.

     if (lock = lock) {
The same applies to this.

Enjoy yourself.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 15th, 2005, 9:46 AM   #5
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
*Gets Massive Headache just reviewing the Code..*

Omg I can't learn that.
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Feb 15th, 2005, 2:05 PM   #6
NarrowPathPilgrim
Newbie
 
Join Date: Nov 2004
Posts: 5
Rep Power: 0 NarrowPathPilgrim is on a distinguished road
Arrow

Quote:
when comparing things in a if statment use == not singal = as singla is an assignment operator and so will asign the value of lock to lock and always evaluates to true
thanks for the info! I had noticed that if I entered something wrong (other than "load") at the prompt it still continued as if I had entered the correct word.

Quote:
change the code thus i think (might need to be corrected...)

if (load.Equals("load")) {//comparing a string to a string shotgun.drawn(); } cout<<"Then you would have to lock it, type in lock.\n"; cin>>lock; if (lock.Equals("lock")) {//String comparison again(not sure if the command Equals is correct though. shotgun.locked(); }
I couldn't get that code to work, sorry.

Quote:
What you actually need to do use the strcmp function to compare the two strings:
if (strcmp(load, "load") == 0)
using that gave me a bunch of errors also.

Quote:
char load[5]; // you need to specify the amount of characters
Hmmm, even using this gave me errors.

Thanks for what you have done so far! You have given me many ideas! It does look like the if statement is the main problem! BTW, if you want to know, I am using "Bloodsheds C++ Compiler" to compile it.
Thanks Again
Zach Doty
NarrowPathPilgrim is offline   Reply With Quote
Old Feb 16th, 2005, 7:58 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
What are the errors? Perhaps we can help fix them.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 17th, 2005, 3:48 AM   #8
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
yeah i looked up the string.Equals(string) function and its poo you can compare string using

stringA == stringB;

--------------


I got java and C++ confussed with each other.
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC