Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Imaginary log-in program trouble (http://www.go4expert.com/forums/imaginary-log-program-trouble-t6851/)

Lief Webster 16Oct2007 05:31

Imaginary log-in program trouble
 
Hello, I'm quite new to the world of C++ and was wondering if someone could help me on a little dilemma. I recently typed out some code for a program that would log you into and imaginary program and then exit (if unclear, look at the code, which will be a lot more unclear :) ). Whenever I run it, of course, I get some errors, one of which crops up with the char yesOrNo. Could someone please help me in pointing out flaws I have made and assist me in making the whole thing turn from a misunderstandable piece of jargon into something that'll actually work. (Don't expect anything too fancy - switch loops are still foreign to me.)

Code:

//Log in program
//v.1.0

#include <iostream>

using namespace std;

//Function Declarations
void LogInDefault();
void NewLogIn();
char ContinueQ();
void ContinueAct();

//Global variables
string username , password , password2 , usernameTrue , passwordTrue;
int newUserQuestion;
char secPrompt;

//Entry point for program

int main()
{
        void LogInDefault();

        void NewLogin();
       
        char ContinueQ();
       
        void ContinueAct();

        return 0;

        int a;
        cin >> a;
}

void LogInDefault()
{
        cout << "Welcome to LoggerIn Pro (v.1.0.5)!";
       
        cout << "\n\n\tUsername:";
        cin >> username;

        cout << "\n\tPassword:";
        cin >> password;

        cout << "\n\n\t\t(New user? Press 1.)";
        cin >> newUserQuestion;
}

void NewLogin()
{
               
        if ( newUserQuestion == 1)
        {
                cout << "Please set up a user name and password.\n";
                cout << "Enter desired user name:\n\t";
                cin >> username;

                username = usernameTrue;

                cout << "Enter desired password:\n\t";
                cin >> password;
               
                cout << "Re-enter desired password:\n\t";
                cin >> password2;

                password2 , password = passwordTrue;
        }

        if ( password == password2 || username == username )
        {
                cout << "\n\n\tThank you for logging in, "
                    << username  << "" << endl;
        }
}

char ContinueQ()
{
        char yesOrNo;
       
    cout << "\n\t\tDo you wish to proceed to logging in now?"
            << "\n\t\t([Y]es or [N]o)";
        cin >> yesOrNo;

        return yesOrNo;
}

void ContinueAct()
{
        if ( yesOrNo )
        {
                if ( yesOrNo == Y )
                {
                        void LogInDefault();
                }
       
                else if ( yesOrNo == N )
                {
                        cout << "\n\t\tAre you sure?"
                            << "\n\t\t([Y]es or [N]o)";
                        cin >> jessica;
               
                        if ( secPrompt == Y )
                        {
                                cout << "\n\t\tGoodbye!";
                        }

                        else if ( secPrompt == N )
                        {
                                void LogInDefault();
                        }
                }
       
        }
       
        else
        {
                if ( usernameTrue == username || passwordtrue == password )
                {
                        cout << "\n\t\tThank you for logging in."
                            << "\nGoodbye!";
                }

                else
                {
                        cout << "Log In failed. Goodbye!\n";
                }
        }
}


DaWei 16Oct2007 06:03

Re: Imaginary log-in program trouble
 
Imginary is wonderful. Magic is wonderful. Unfortunately, C/C++ do not adhere to these viewpoints. They demand sytactical and semantic correctness. This might seem strange to you, but you should really consider expressing what your program is to do and how it is failing to meet your expections.

Verbatim quotes of any error messages would not be amiss. I will grant that these messages might drano your mind, but they can be quite clear and relevant.

I will grant you n00b status, but I will not grant you unthinking status unless you insist. I would opine that you don't want to be relegated to the latter category.

Lief Webster 16Oct2007 06:49

Re: Imaginary log-in program trouble
 
Okay, I'll try to sum up exactly what I want it to do.
To start with, it should print:

Welcome to Logger In Pro (v.1.0.5)!
Username:
Password:

(New user? Press 1.)


then, if you are a new user (which you are),

Please set up a username and password.
Enter desired username:
Enter desired password:
Re-enter desired password:


[check that the two passwords are the same]

Thank you for logging in.

Do you wish to proceed to logging in now?
([Y]es or [N]o)


[yes, I know; rather redundant]

If you say you want to leave, it asks you if you're sure. If you say yes, it exits. Otherwise it sends you back to the first function; the main logging in one. Please understand that there isn't actually a real program that you log in to: it's just meant to 'log you in' and then exit.

Lief Webster 16Oct2007 06:58

Re: Imaginary log-in program trouble
 
(I didn't know I couldn't edit, so..)

When I try to run the program, an error message pops up showing an error with the line of code that says

if ( yesOrNo == Y )


"C:/Documents and Settings/Sam/My Documents/C++/Log_in_program.cpp:92: `Y'
undeclared (first use this function)"

is the reason it spits out.

I thought if the user had entered the character for yesOrNo (either Y or N) then it would be the same in the next function because it's a global variable. How would I go about forwarding the input from function to function?

DaWei 16Oct2007 07:09

Re: Imaginary log-in program trouble
 
Y would be a variable. You would need to set it to some appropriate value. If you mean the character, 'Y', then you would need to express it as 'Y', not as Y. If you mean the string "Y", then you would need to express it as "Y'. Note the distinguising quotes, versus double quotes.

You cannot use an evaluation such as "Y" == "Y" in C. In C++ you can only use it if "Y" is a class that is a string entity. If it's a "C" string, you'll have to use "strcmp", or roll your own comparison.

These are basic (very basic) concepts. You really need to hit the books for another pass at how these things work. Variables may be initialized to literals, but they are not literals.

Lief Webster 17Oct2007 02:14

Re: Imaginary log-in program trouble
 
Thanks. I got the code fixed (partially). Now it doesn't bring up any error messages; it just flashes the DOS-like window up for a split second and then the program terminates. Shouldn't it at least wait for me to input some text?

(Here's the exact code I tried to compile:)

Code:

//Log in program
//v.1.0

#include <iostream>

using namespace std;

//Function Declarations
void LogInDefault();
void NewLogIn();
char ContinueQ();
void ContinueAct();

//Global variables
string username , password , password2 , usernameTrue , passwordTrue;
int newUserQuestion;
char secPrompt , yesOrNo , Y , N;

//Entry point for program

int main()
{
        void LogInDefault();

        void NewLogin();
       
        char ContinueQ();
       
        void ContinueAct();

        return 0;

        int a;
        cin >> a;
}

void LogInDefault()
{
        cout << "Welcome to LoggerIn Pro (v.1.0.5)!";
       
        cout << "\n\n\tUsername:";
        cin >> username;

        cout << "\n\tPassword:";
        cin >> password;

        cout << "\n\n\t\t(New user? Press 1.)";
        cin >> newUserQuestion;
}

void NewLogin()
{
               
        if ( newUserQuestion == 1)
        {
                cout << "Please set up a user name and password.\n";
                cout << "Enter desired user name:\n\t";
                cin >> username;

                username = usernameTrue;

                cout << "Enter desired password:\n\t";
                cin >> password;
               
                cout << "Re-enter desired password:\n\t";
                cin >> password2;

                password2 , password = passwordTrue;
        }

        if ( password == password2 || username == username )
        {
                cout << "\n\n\tThank you for logging in, "
                    << username  << "" << endl;
        }
}

char ContinueQ()
{
       
    cout << "\n\t\tDo you wish to proceed to logging in now?"
            << "\n\t\t([Y]es or [N]o)";
        cin >> yesOrNo;

        return yesOrNo;
}

void ContinueAct()
{
        if ( yesOrNo )
        {
                if ( yesOrNo == Y )
                {
                        void LogInDefault();
                }
       
                else if ( yesOrNo == N )
                {
                        cout << "\n\t\tAre you sure?"
                            << "\n\t\t([Y]es or [N]o)";
                        cin >> secPrompt;
               
                        if ( secPrompt == Y )
                        {
                                cout << "\n\t\tGoodbye!";
                        }

                        else if ( secPrompt == N )
                        {
                                void LogInDefault();
                        }
                }
       
        }
       
        else
        {
                if ( usernameTrue == username || passwordTrue == password )
                {
                        cout << "\n\t\tThank you for logging in."
                            << "\nGoodbye!";
                }

                else
                {
                        cout << "Log In failed. Goodbye!\n";
                }
        }
}


DaWei 17Oct2007 07:15

Re: Imaginary log-in program trouble
 
Programs do not wait for you to view their output. Theyperform their tasks, then they terminate. This is not a strange phenomenon, it is perfectly normal. If it were not so, you would have forty-leben programs hanging around your desktop until you clicked the big 'X" on them all (assuming that you have a typical GUI interface).

If you want a program to hang around while you fart around reading the output, then add an operation that prevents the program from finishing until the user has made some overt act to cause it to finish. One simple way to do this is to require the program to receive user input in order to complete its tasks.

One way to do this is to make the last act of the program a statement such as "getchar ();" or "cin.get ();". This is not foolproof; in many instances, you will approach this statement with crap that has been left in the buffer because you did not use it all. The statement will then consume one of these trash characters and proceed to terminate.

You will need to clear any shit in the buffer before you resort to one of these methods.

You may discover some methods (system ("pause"); or the like) that seem to handle the problem. Those are a fake. They are not portable. Your program will not behave properly when compiled on a different system using a different OS. If you are satisfies with being a schlock programmer whose programs are not widely usable, then by all means resort to the poor solutions. Otherwise, learn to empty the input buffer in a widely workable way. It won't be easy. C and C++ are both notoriously deficient in this respect.

Lief Webster 17Oct2007 07:59

Re: Imaginary log-in program trouble
 
Thanks, I got it fixed. (I usually add
int a;
cin >> a;
right before return 0; in main() to solve this problem)

I think I'll just scrap the whole thing and restart, but this time write out some pseudo code so I know what the heck I'm doing in advance.


All times are GMT +5.5. The time now is 15:26.