Help Wanted

code help – en972

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
Ok, I'm writing this code (some of you have seen it and helped me).

All that you guys have told me works great so far, and because it, I have added more stuff. But now comes the time where I need help again *sigh*

Below I'll post the errors than the code (note, not all of the code is finished, so if you see some weird variables, like, "politician" don't freak out.

Oh, and i'm using Dev-Cpp power by wx widgets.

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\C++ Lists\kerry cons.cpp" -o "C:\Dev-Cpp\C++ Lists\kerry cons.exe" -g3 -I"c:\dev-cpp\include\c++\3.3.1" -I"c:\dev-cpp\include\c++\3.3.1\mingw32" -I"c:\dev-cpp\include\c++\3.3.1\backward" -I"c:\dev-cpp\lib\gcc-lib\mingw32\3.3.1\include" -I"c:\dev-cpp\include" -L"c:\dev-cpp\lib" -g3
C:/Dev-Cpp/C++ Lists/kerry cons.cpp: In function `int main()':

C:/Dev-Cpp/C++ Lists/kerry cons.cpp:27: error: syntax error before numeric
constant
C:/Dev-Cpp/C++ Lists/kerry cons.cpp:27: error: cannot declare static function
inside another function
C:/Dev-Cpp/C++ Lists/kerry cons.cpp:29: error: syntax error before numeric
constant

C:/Dev-Cpp/C++ Lists/kerry cons.cpp:29: error: cannot declare static function

inside another function
C:/Dev-Cpp/C++ Lists/kerry cons.cpp:31: error: syntax error before numeric
constant
C:/Dev-Cpp/C++ Lists/kerry cons.cpp:31: error: cannot declare static function
inside another function

C:/Dev-Cpp/C++ Lists/kerry cons.cpp:81: error: syntax error at end of input

Execution terminated


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


using namespace std;



int main()
{
string whitehouse ("whitehouse");
string user;
string kerry ("kerry");
string password;
string politician ("John Kerry");
string politician1 ("Richard Nixon");
string politician2 ("Al Gore");
string john;


/*those are variables that will be used to acess diffrent stuff. You won't
need to mess with these*/


cout<< "Computer starting up..." <<endl;
static void Sleep(unsigned long 3 milliseconds); //wait 3 seconds
cout<< "Done" <<endl;
static void Sleep(unsigned long 3 milliseconds);
cout<< "Opening briefing..." <<endl;
static void Sleep(unsigned long 3 milliseconds);
cout<< "Done" <<endl;


cout<< endl; //space things out

cout<< "John Kerry" <<endl;
cin>> politician;

if (john == politician)
{
cout<< "Enter User: " <<endl;
cin>> user;

if (user == whitehouse)
{
cout<< "Enter Password: " <<endl;
cin>> password;
}

else
{
cout<< "Access Denied, Program Terminated..." <<endl;
}
}
else
{
cout<< "Not a valid folder or file" <<endl;
}
if (password == kerry)
{
cout<< "Access Granted, Loading..." <<endl;

cout<< endl; //start a new line to space things out

cout<< "Type the name of a folder below to access it." <<endl;
cout<< endl;
cout<< "Documents" <<endl;
cout<< endl;
cout<< "Media" <<endl;
cout<< endl;
cout<< "Temp" <<endl;
}
else
{
cout<< "Access Denied, Program Terminated..." <<endl;
}

system ("PAUSE");
return 0;


Thanks alot. God bless

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

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
quote:
static void Sleep(unsigned long 3 milliseconds);

Should be:
Sleep(3);

Or at least:
Sleep(unsigned long 3);

"static void" is for declaring a function, not using it. "unsigned long" is just the type, and probably isn't needed. The computer doesn't know what the word "milliseconds" means .

You can make your code more efficient, BTW, but I'll tell you how after we've fixed the errors.

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

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
HOLY MACK TRUCK! I CAN READ THAT! And it makes sense! C/C++ to boot!
I might have a chance at C/C++ still!

------------------
There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it so complicated that there are no obvious deficiencies.

--C. A. R. Hoare
[B][/B]

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
Thanks CPU freak. I just got home from church, so after I eat I'll check it out

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

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
The Sleep(3); works. though I got an error that says sleep has not been declared. Also it still says syntax error. A little help?

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

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
quote:
though I got an error that says sleep has not been declared.

Apparently it's a function from the windows library.

Add to the top of your program:
#include <windows.h>

If you still get errors, list them here so we can see them.

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

Seven7
Member

Posts: 50
From: USA
Registered: 03-16-2005

Why are you writting your own "Sleep" function?!
Just use the function that is provided to you.
Why reinvent the wheel?

However, IF you feel that you need to write your own:

"static void Sleep(unsigned long 3 milliseconds);"
This is an error.

"static void Sleep(unsigned long milliseconds = 3);"
Now you can use;
Sleep(); // By default, pause 3 ms.
or
Sleep(1000); // pause 1000 ms.

Also, you dont need to have static functions
in your program. Static functions are not a good
idea to do. I would suggest not getting into a habit
of using static functions.

j.

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
Wow, thanks man.

I have 2 questions.

1. Why cant I use using namespace std; everyone says that is not good

2. How can I make my code easier to read and more efficiant


(By the way, all the errors are gone and it works so far)

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

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
quote:
. Why cant I use using namespace std; everyone says that is not good

Dunno. I use it.

quote:
2. How can I make my code easier to read and more efficiant

Declaring all your strings seems kinda redundant.

 string kerry ("kerry"); 
if (password == kerry)
{...}

can be replaced with:

if(password == "kerry")
{...}

But that may actually be bad advice if you want to change your strings later.

Or better yet, store your password as an MD5 hash in a database. But that may be a bit out of your abilities right now .

So far, so good .

Edit: 800th post! Woohoo!

------------------
"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 April 18, 2005).]

CoolJ

Member

Posts: 354
From: ny
Registered: 07-11-2004
I agree, I don't see anything wrong with:
using namespace std;

One suggestion might be to start thinking about what could be classes/objects.

I see two classes that are begging to be made from your code...

class System - the system that is being accessed

this class could have methods such as Boot(), Logon(), Logoff()
could also have a list of
valid users
active users
accessible folders

class User - the user logged into the system
contains info about the user...

CoolJ

Member

Posts: 354
From: ny
Registered: 07-11-2004
Please ignore this post if your not interested in an example of making a System class and User class. I just wanted to show how you might do this.
Here's the following list of users/passwords for the system logon:

jkerry wannabeprez1
gbushjr igotsaddam1
hclinton me4prez08


**COPY & PASTE STARTING HERE ****

#include <time.h>
#include <map>
#include <vector>
#include <string>
using namespace std;

// i had to create this sleep function, you might have to just use yours
void sleep(unsigned int seconds)
{
clock_t endtime = (clock_t)(1000 * seconds) + clock();
//wait until time has passed
while (endtime > clock());
}


// class: User
// description: User who logs onto the system .
class User
{
// variables
public:

string name;
string password;
string title;
int security_lvl;

// methods
// constructor
User(string username, string userpass)
{ name = username;
password = userpass;
};

};


// class: System
// description: A computer system users can log on to.
class System
{
// system variables
double version; // system version
string id; // system id
map<string,string> users; // users w/ passwords
vector<string> active_users; // users currently logged on

// methods
public:
void Boot();
User *LogonPrompt();
User *Logon(string username, string password); // returns User object if successfull
void Logoff(User *user);
};


// method: System::Boot
// description: Boot up computer system..
void System::Boot()
{
// initial system info. could be passed in or read from file.
version = 7.5456;
id = "CIA Server B911Z";

// load valid users list. could be kept in file.
users["jkerry"] = "wannabeprez1";
users["gbushjr"] = "igotsaddam1";
users["hclinton"] = "me4prez08";

// system boot prompts..
cout<< "Computer starting up..." <<endl;
sleep(3); //wait 3 seconds
cout<< "Done" <<endl;
sleep(3);
cout<< "Opening briefing..." <<endl;
sleep(3);
cout<< "Done" <<endl;
cout<< endl; //space things out
};


// method: System::LogonPrompt
// description: Prompt the user for logon information..
User *System::LogonPrompt()
{
string username;
string password;

// prompt for user name
cout<< "Enter User: " <<endl;
cin>> username;

// prompt for password
cout<< "Enter Password: " <<endl;
cin>> password;

cout<< endl; //space things out

// return user if successful
return (Logon(username,password));
};


// method: System::Logon
// description: Attempts to logon to the system
User *System::Logon(string username, string password)
{
User *user = NULL;

// if found username is a valid system user
if (users.find(username) != users.end())
{
// check if the user's password matches
if (users[username] == password)
{
// create the user object initialized with username and password.
user = new User(username,users[username]);
}
}

// returns pointer to user object if successful,
// otherwise returns NULL
return (user);
};

// method: System::Logoff
// description: Logs user off the system
void System::Logoff(User *user)
{
if (user)
{
cout << user->name << ", Logged Off Successfully..." << endl;
cout<< endl; //space things out

sleep(3);
}

return;
};


// main
int main()
{
System Sys;
User *curUser = NULL;

// system boot
Sys.Boot();

// while logon unsuccessful
while (curUser == NULL)
{
// prompt user for logon, returns User object if successfull logon
curUser = Sys.LogonPrompt();

if (curUser == NULL)
{
cout<< "Access Denied, Logon Terminated..." <<endl;
cout<< endl; //space things out

sleep(3);
}
}

// logon successful
cout<< "Access Granted, " << curUser->name << ", Loading..." <<endl;
cout<< endl; //space things out

sleep(3);
// continue......
// ...

// logoff user
if (curUser)
Sys.Logoff(curUser);


// clean up befor exiting
if (curUser)
{
delete curUser;
curUser = NULL;
}
return 0;
}

**COPY & PASTE ENDING HERE ****

[This message has been edited by coolj (edited April 18, 2005).]

Seven7
Member

Posts: 50
From: USA
Registered: 03-16-2005
Hi,

quote:

Why cant I use using namespace std; everyone says that is not good


For all practical purposes, it’s ok for you to use a name space.
There have been some papers and books published suggesting that
using name space in a header file is not good practice (it can
introduce compiling and linking issues). This is especially if you are
using third party name spaces. For example the "Boost" name space.
Again, you don't need to worry about the name space issue in your
program.

FYI:
using name space std:
cout << "Hello World" << endl;

The code above is equivalent to:

std::cout << "Hello World" << std::endl;

or
#include <string>
using name space std;
string YourName;

The code above is equivalent to:

#include <string>
std::string YourName;

These two examples apply to header files only. You can have name spaces
in you .cpp file, just not in the header files. BUT dont worry about it.


FYI-2:
One example of using the "Sleep(...)" function is if you were going
to spawn a new thread this is where you would introduce a "Sleep(...)"
function because you want a slow down your "main" thread to give time for
the newly create thread to initialize (“catch up”). In general, apply a
"Sleep(...)" function on processes and not on functions. Keep in mind
here, I said: "In general". Another example of using the "Sleep(...)"
function is when you are initializing a port for listening. You need time,
for example, server to start listening for connections *before* it can
handle accepting clients. In this example you need to "Sleep(...)" at a
particular point to give time for the initialization of a port *before*
allowing connections otherwise the server will not be able to accept
clients. (for those that understand networking programming, backlog does
not guarantee a connection.)

j.

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
Correct me if I'm wrong, but it's inadviseable to create an infinite loop as a sleep function anymore. It'll throw your CPU into 100%, and isn't very thread aware.

A proper sleep function should tell the OS to sleep the thread, so the OS can give resources to other threads. So I advise using the built-in sleep functions.

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

Seven7
Member

Posts: 50
From: USA
Registered: 03-16-2005
CobraA1,

The "Sleep(...)" function does not put a thread into an infinite loop,
nor does my above post even suggest such a thing. You make the
"main" thread "Sleep" for several milliseconds so the spawned
thread has time to "catch up", (of course there are other ways of doing
but this is one example). I’m not sure how you got 'infinite loop'
in the above post??

"Sleep(90000000000000000000000000000000000000000000000000000)" In theory
this will work right? but its not an infinate loop. Just dumb.

My point to the "FYI" is an example of the good usage of the "Sleep(..)"
function. Most people see these type of function as "eye-candy" thinking
they can just use it because it is "cool". This type of practice leads
to sloppy and poor programming habits. It's ok to play around with them,
but I would not get into a habit of using it just because its a
"cool thing to do".

j.

[This message has been edited by Seven7 (edited April 19, 2005).]

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
I was talking about coolj's code . . .

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

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
K, thanks guys.

I have my new code. Will post soon. theres some stuff to do...

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

CoolJ

Member

Posts: 354
From: ny
Registered: 07-11-2004
quote:
Originally posted by CobraA1:
I was talking about coolj's code . . .

Yeah..ignore my sleep function..thanks for pointing that out!