Help Wanted

c++ help on game – en972

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
I'm makeing a puzzle game in c++ . This is a basic form of it

I'll post the code before i explain whats wrong

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
string password;
string kerry;

cout<< "Enter Password: " <<endl;
cin>> password;

if (password == kerry)
cout<< "Access Granted, Loading..." <<endl;
cout<< "Type the name of a folder below to access it." <<endl;
cout<< "Documents" <<endl;
cout<< "Media" <<endl;
cout<< "Temp" <<endl;

else
cout<< "Access Denied, Program Terminated..." <<endl;

system("PAUSE");
return 0;
}


(1) it doesn't read the password, it always throws away access granted

(2) [error] syntax error before 'else'

------------------
Keep the holy day sacred.......halllllllukan

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
you don't have a scope.

if (password == kerry)
cout<< "Access Granted, Loading..." <<endl;

so the IF statement is just doing that first line.. (then there is no else after that, so it assumes its finished, and shows the other lines below normally

cout<< "Type the name of a folder below to access it." <<endl;
cout<< "Documents" <<endl;
cout<< "Media" <<endl;
cout<< "Temp" <<endl;

else
cout<< "Access Denied, Program Terminated..." <<endl;


so basically fix it with


if (password == kerry)
{
cout<< "Access Granted, Loading..." <<endl;
cout<< "Type the name of a folder below to access it." <<endl;
cout<< "Documents" <<endl;
cout<< "Media" <<endl;
cout<< "Temp" <<endl;
}
else
cout<< "Access Denied, Program Terminated..." <<endl;

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

CoolJ

Member

Posts: 354
From: ny
Registered: 07-11-2004
also your string should be set to something
string kerry("mypass1");
en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
wow, thanks guys, I am still getting used to surrounding everything in braces

string kerry ("mypass1"), ooooh. Ok, I get it.

------------------
Keep the holy day sacred.......halllllllukan

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
I still always get access denied

------------------
Keep the holy day sacred.......halllllllukan

graynod

Member

Posts: 17
From: richmond, VA, USA
Registered: 09-28-2004
This might sound nutty, but what are you entering? It looks like your're expecting a match for "kerry", is that the input you're entering? Just a thought....

May God bless you as you study and learn.

------------------
graynod -> "my head was feeling scared but my heart was feeling free" -- the Pixies

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
kerry is the variable the string is stored in. "mypass1" is the string.

If you want "kerry" to be the password, try

string kerry("kerry");

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

[This message has been edited by CobraA1 (edited March 04, 2005).]

graynod

Member

Posts: 17
From: richmond, VA, USA
Registered: 09-28-2004
Yah, I didn't write what I meant clearly at all, shouldn't have used the double quotes I guess. What I wonder is, if say the password the user enters is the string "password", and the value stored in kerry is "mypass1", then the test will fail; is this what's happening when en972 tests it? Just a thought! May God bless you all.

------------------
graynod -> "my head was feeling scared but my heart was feeling free" -- the Pixies

CapnStank

Member

Posts: 214
From: Sask, Canada
Registered: 12-16-2004
First off don't you need to load string.h? or
#include <string>
using namespace std;

2nd off I set strings/characters like:
string kerry = "blahblah";

works the same for me.

------------------
Jesus is f'ing metal!

FredSmith
Junior Member

Posts: 2
From:
Registered: 03-06-2005
I'd also strongly recommend replacing using namespace std with

using std::cout;
using std::endl;

etc.

string s("test") is better because it calls the constructor, string s = "456453" calls the empty constructor then the = operator.

system("PAUSE") is not C++, it's system specific. I'd avoid such calls if I could.

You're obviously in at the deep end here, you should buy a beginners book like 'teach yourself C++ in 24 hours' and work through it before thinking about writing any games. The world is full of really bad games from people who didn't learn to program first. I admire your enthusiasm, but you'd do better to get some basic syntax under your belt before attempting anything remotely complex. :-)

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
Oh hey, um, wow I almost forgot about this. Thanks for the help I'll pick it back up again. I've really been using GUI's. Oh and Thanks for suggestions on books.. But I have this friend at church, very exellant programmer, hes teaching me now. Thanks alot for all your help, espically you fredsmith

------------------
Keep the holy day sacred.......halllllllukan