Nami550985

Hi guys,

I am very new to using C++ and am currently using the Visual C++ Express 2005 with SP1 Vista compiler.

Here is the code that I am attemting to compile:

Code Snippet

#include <stdafx.h>

char *pass ="1234";

int attempts;

char userpass[80];

int main()

{

//cout << "Please Enter Your Password: ";

//cin.getline(userpass, 80, '\n' );

attempts= 0;

while (*userpass != *pass && attempts >=3);

}

{

cout << "Please Enter your password:";

cin.getline(userpass, 80, '\n' );

attempts ++;

// adds one to the attempt

do puts ("You have been locked out of the system");

// when attempts becomes three you are locked out of the system;

//{

cout << "Access Denied";

// acess denied is displayed when the user enters the incorrect password

cin.getline( userpass, 80 , '\n' );

//}

puts ("Access Granted");

getchar();

}

And here is the error I am getting:

Code Snippet

error C2447: '{' : missing function header (old-style formal list )

Could someone offer a helping hand

Thank you!



Re: Visual C++ Language Problem with function headers!

Sarath.

Problems with your code are marked as inline comments.

while (*userpass != *pass && attempts >=3); // You've put a semicolon for the while loop. Do you want to run this loop forever by using *userpass will only point to the first element in the array. You've to use strcmp or other string comparison functions for comparing strings.

} // Here the scope of main ends

{ // this block is declared globally and not attached to any function which is not allowed and also not logical

cout << "Please Enter your password:";

cin.getline(userpass, 80, '\n' );

attempts ++;

do puts ("You have been locked out of the system"); // Why you've put a do statement without a while if you want to execute only once, what's the use of do statement

I suggest you to read and try some good samples from your favorite C++ book or from some good websites.





Re: Visual C++ Language Problem with function headers!

Jonathan Caves - MSFT

This is a great summary of the potential problems and I have just one more comment to add - are you sure you are compiling this as a C++ file ... not as a C file Also if you want to learn modern C++ this is a really great book




Re: Visual C++ Language Problem with function headers!

TilakGopi

Hi,

Following is the refinement of ur old code.I think this is the way, u wanted ur program to work.


char *pass ="1234";

int attempts;

char userpass[80];

int main()
{
//cout << "Please Enter Your Password: ";

//cin.getline(userpass, 80, '\n' );


attempts= 0;
bool bValidPassword = false;

while ((!bValidPassword) && attempts <3 )
{
cout <<
"\nPlease Enter your password:";
cin.getline(userpass, 80,
'\n' );
attempts ++;
bValidPassword = (strcmp(userpass,pass) == 0);
if(!bValidPassword)
{
cout <<
"\nAccess Denied";
}
}
if(!bValidPassword )
{
puts (
"\nYou have been locked out of the system");
}
else

{
puts (
"\nAccess Granted");
}
getchar();
}


And please follow Sarath's and Jonathan caves suggestions.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C++ Language Problem with function headers!

Marius Bancila

I also suggest a list of good C++ books.






Re: Visual C++ Language Problem with function headers!

Nami

Hi guys,

thanks alot for your info andit actually helped quite a bit. However after refining the code, i am now getting the error:

Code Snippet

error C2871: 'std' : a namespace with this name does not exist

error C2065: 'cout' : undeclared identifier

error C2065: 'cin' : undeclared identifier

error C2228: left of '.getline' must have class/struct/union type is ''unknown-type'

error C3861: 'strcmp': identifier not found

Oh and here is what the code looks like in C++ Express Edition:

Code Snippet

// Initial.cpp : Defines the entry point for the console application.

//

#include <stdafx.h>

using namespace std;

char *pass ="1234";

int attempts;

char userpass[80];

int main()

{

//cout << "Please Enter Your Password: ";

//cin.getline(userpass, 80, '\n' );

attempts= 0;

bool bValidPassword = false;

while ((!bValidPassword) && attempts <3 )

{

cout << "\nPlease Enter your password:";

cin.getline(userpass, 80, '\n' );

attempts ++;

bValidPassword = (strcmp(userpass,pass) == 0);

if(!bValidPassword)

{

cout << "\nAccess Denied";

}

}

if(!bValidPassword )

{

puts ("\nYou have been locked out of the system");

}

else

{

puts ("\nAccess Granted");

}

getchar();

}

The main problem is that apparently, in C++ Express the "namespace std;" is not a legal comment. So what must I use in turn

Thanks again for all the help!





Re: Visual C++ Language Problem with function headers!

TilakGopi

Hi,

include string.h and iostream as follows.

#include <stdafx.h>

#include<string.h>

#include<iostream>

using namespace std;

Thanx,

Ch.T.Gopi Kumar






Re: Visual C++ Language Problem with function headers!

Nami

Okay guys, so i got the program up and running for most parts, however, i need to enable it so that if the username correct, it would automatically ask for the password.

Here's the code:

Code Snippet

#include <stdafx.h>

#include <string.h>

#include <iostream>

using namespace std;

char *user ="abc";

char *pass ="1234";

int attempts;

char username[80];

char userpass[80];

int main()

{

//cout << "Please Enter The Login Password: ";

//cin.getline(userpass, 80, '\n' );

bool bValidUser = false;

bool bValidPassword = false;

attempts =0;

while ((!bValidUser) && attempts <3)

{

cout << "\nPlease enter the username:";

cin.getline (username, 80, '\n' );

attempts ++;

bValidUser = (strcmp(username,user) == 0);

if(!bValidUser)

{

cout << "\nAccess Denied";

}

}

if(!bValidUser)

{

puts ("\nYou have been locked out of the system.");

}

else

{

puts ("\nAccess Granted");

}

getchar();

while(!bValidUser);

{

do (!bValidPassword);

}

attempts= 0;

while ((!bValidPassword) && attempts <3)

{

cout << "\nPlease enter the login password:";

cin.getline(userpass, 80, '\n' );

attempts ++;

bValidPassword = (strcmp(userpass,pass) == 0);

if(!bValidPassword)

{

cout << "\nAccess Denied";

}

}

if(!bValidPassword)

{

puts ("\nYou have been locked out of the system");

}

else

{

puts ("\nAccess Granted");

}

getchar();

}

However, I am getting a syntax error for the part of the code highlited in yellow. The syntax error is related to the closed bracket " } "

Thank you again.





Re: Visual C++ Language Problem with function headers!

Brian Kramer

I would strongly suggest you find a book on C programming and read about the basic programming constructs.

In the meantime, I am providing a link that shows examples of while, and do...while. Note that there is no while...do. Also note that after while is a statement block. A statement block that is empty, i.e. does nothing, is indicated by a semicolon exactly as you're trying to do. This can be an infinite loop if the conditional evaluates to true.

http://www.geocities.com/learnprogramming123/Ckeywords.htm





Re: Visual C++ Language Problem with function headers!

Sarath.

while(!bValidUser);

{

do (!bValidPassword);

}

I dint get what you are trying to do with this code

you should know about the basic syntax and keywords before using it.

Refer MSDN to learn about it.

I doubt you are trying to do something like this

if(bValidUser)

{

.......// statements on valid user

}

Puttnig a semicolon will make the while loop infinite with it's conditional expression. This is what I said in my very first post.

Learn about while and do-while before using it.