![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 5
Rep Power: 0
![]() |
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;
}Thanks in advance! Zach Doty Last edited by NarrowPathPilgrim; Feb 15th, 2005 at 12:12 AM. Reason: change title |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2004
Posts: 5
Rep Power: 0
![]() |
I think that this part of the code is the problem, am I right?
cin>>lock;
if (lock = lock) {
shotgun.locked(); } |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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(); } |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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 oneHere's the real problem: cin>>load; // this is fine
cout<<"\n";
if (load = load) {if (load == "load") if (strcmp(load, "load") == 0) if (lock = lock) {Enjoy yourself. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
*Gets Massive Headache just reviewing the Code..*
Omg I can't learn that.
__________________
And there was much rejoicing... Yay.... |
|
|
|
|
|
#6 | ||||
|
Newbie
Join Date: Nov 2004
Posts: 5
Rep Power: 0
![]() |
Quote:
Quote:
Quote:
Quote:
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 |
||||
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
What are the errors? Perhaps we can help fix them.
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|