[SOLVED] I have no idea...

Started by
7 comments, last by NUCLEAR RABBIT 18 years ago
Hello, I was creating a little DataBase kind of Program, but it seems I've ran into a problem. When I select an option from the menu, no matter what i select, it does the samething, it creates a new account. Please help.
/**$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*$
$*%    PROGRAMMER NAME: BRANDON WALL   *$%
%*$    DATE:   4-19-06                 %*$
*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$**/ 

// Shimmer Inc. DataBase

#include <string>
#include <iostream>
#include <windows.h>
using namespace std;


int menu()
{
    
        //Gets user's user name
        int createAccount = 0;
        {
        cout << "\t\t\t+ Welcome to Shimmers Inc. +\n\n"
         << "***************************************************\n"
         << "**                                               **\n"
         << "**   Navigation:                                 **\n"
         << "**   1) Create New Shimmer Account               **\n"
         << "**   2) Log In With Existing Shimmer Account     **\n"
         << "**   3) Exit                                     **\n"
         << "**                                               **\n"
         << "***************************************************\n\n"
         << "Please Make Your Selection: ";
    cin >> createAccount;
        }
    
}

int main(int createAccount)
{
    
        //Declaire Variables
    string userName;
    string password;
    string name;
    string email;
    string logName;
    string logPass;
    
    //Declaires the menu function
    menu();
    
    if(createAccount == 1)
    {
                    //Clears screen for users info
                    system("cls");
                    
                    //username
                    //Gets users new account info
                    cout << "1) Please enter Your New Username (One Word): ";
                    cin >> userName;
                    
                    
                    //password
                    cout << "\n2) Please enter Your Account Pssword: ";
                    cin >> password;
                    
                    
                    //name
                    cout << "\n\nFirst Name: ";
                    cin >> name;
                    
                    //email
                    cout << "\nEmail Address: ";
                    cin >> email;
                    
                    //Thank you ending
                    cout << "\nThank You.";
                    Sleep(2000);
                    
                    //clear screen for loading message
                    system("cls");
                    
                    //Loading message
                    cout << "\t\t\t\tL";
                         Sleep(1000);
                    cout << "O";
                         Sleep(1000);
                    cout << "A";
                         Sleep(1000);
                    cout << "D";
                         Sleep(1000);
                    cout << "I";
                         Sleep(1000);
                    cout << "N";
                         Sleep(1000);
                    cout << "G\n\n";
                         Sleep(2000);
                    //Waits for user to cont.
                    system("cls");
                    
                    //Gives final Summary
                    cout << "Thank Your for Choosing Shimmer!\n"
                         << "Heres Your Account Info: \n\n"
                         << "Account Name: "
                         << userName
                         << "\nPassword: "
                         << password
                         << "\n\nPlease Write your Account Information Down Incase you Forget.\n\n"
                         << "Please Log In when you are Transfered to the Menu.\n\n\n";
                    system("pause");
                    system("cls");
                    menu();
                    
    }
    
    else if(createAccount == 2)
    {
                     //Tells user to enter their account name
                     cout << "Account Name: ";
                     cin >> logName;
                     
                     //Tells user to enter their account password
                     cout << "Password: ";
                     cin >> logPass;
                     
                     if(logName != userName && logPass != password)
                     {
                                cout << "\n\nAccess Denied.";
                                //Delays for another try
                                Sleep(1000);
                                menu();
                     }
                     else if(logName == userName && logPass == password)
                     {
                          cout << "Welcome\n\n";
                          
                          //waits for user to exit
                          system("pause");
                     }
    }
    
    else if(createAccount == 3)
    {
                     cout << "\n\nOk, Bye.\n\n";
                     system("pause");
    }
    
    else 
    {
         
         cout << "\n\nThats not an Option. Bye\n\n";
         system("pause");
    }    
    return 0;
                    
}

[Edited by - NUCLEAR RABBIT on April 20, 2006 10:49:03 AM]
Advertisement
This shouldn't compile as posted.

You are not returning anything from menu() and createAccount is not declared in main() (but you use it).

You should get an error about createAccount on the first if statement in the main function about it not being declared. You should also get, either a warning or an error about menu not returning anything.

That should solve your issue(s).

Edit: Just noticed, main should be:
int main()
{
}

As a minimum.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

You make createAccount a local variable in menu(). So it disappears after the function is over.

Also, main() shouldn't take "int createAccount" as a parameter.

Instead, create a variable in the main() method called "createAccount", and have "menu" return the int choice (return createAccount;)

Good luck!
Hi bud,

Right so i have got it working here for you:
/**$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*$$*%    PROGRAMMER NAME: BRANDON WALL   *$%%*$    DATE:   4-19-06                 %*$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$**/ // Shimmer Inc. DataBase#include <string>#include <iostream>#include <windows.h>using namespace std;void menu( int& output ){        cout << "\t\t\t+ Welcome to Shimmers Inc. +\n\n"         << "***************************************************\n"         << "**                                               **\n"         << "**   Navigation:                                 **\n"         << "**   1) Create New Shimmer Account               **\n"         << "**   2) Log In With Existing Shimmer Account     **\n"         << "**   3) Exit                                     **\n"         << "**                                               **\n"         << "***************************************************\n\n"         << "Please Make Your Selection: ";    cin >> output;}int main(int createAccount){    int menuSelection = 0;        //Declaire Variables    string userName;    string password;    string name;    string email;    string logName;    string logPass;        //Declaires the menu function    menu(menuSelection);        if(menuSelection == 1)    {                    //Clears screen for users info                    system("cls");                                        //username                    //Gets users new account info                    cout << "1) Please enter Your New Username (One Word): ";                    cin >> userName;                                                            //password                    cout << "\n2) Please enter Your Account Pssword: ";                    cin >> password;                                                            //name                    cout << "\n\nFirst Name: ";                    cin >> name;                                        //email                    cout << "\nEmail Address: ";                    cin >> email;                                        //Thank you ending                    cout << "\nThank You.";                    Sleep(2000);                                        //clear screen for loading message                    system("cls");                                        //Loading message                    cout << "\t\t\t\tL";                         Sleep(1000);                    cout << "O";                         Sleep(1000);                    cout << "A";                         Sleep(1000);                    cout << "D";                         Sleep(1000);                    cout << "I";                         Sleep(1000);                    cout << "N";                         Sleep(1000);                    cout << "G\n\n";                         Sleep(2000);                    //Waits for user to cont.                    system("cls");                                        //Gives final Summary                    cout << "Thank Your for Choosing Shimmer!\n"                         << "Heres Your Account Info: \n\n"                         << "Account Name: "                         << userName                         << "\nPassword: "                         << password                         << "\n\nPlease Write your Account Information Down Incase you Forget.\n\n"                         << "Please Log In when you are Transfered to the Menu.\n\n\n";                    system("pause");                    system("cls");                    menu(menuSelection);                        }        else if(menuSelection == 2)    {                     //Tells user to enter their account name                     cout << "Account Name: ";                     cin >> logName;                                          //Tells user to enter their account password                     cout << "Password: ";                     cin >> logPass;                                          if(logName != userName && logPass != password)                     {                                cout << "\n\nAccess Denied.";                                //Delays for another try                                Sleep(1000);                                menu(menuSelection);                     }                     else if(logName == userName && logPass == password)                     {                          cout << "Welcome\n\n";                                                    //waits for user to exit                          system("pause");                     }    }        else if(menuSelection == 3)    {                     cout << "\n\nOk, Bye.\n\n";                     system("pause");    }        else     {                  cout << "\n\nThats not an Option. Bye\n\n";         system("pause");    }        return 0;                    }


You have quite alot of issues here though.

1. The C++ standard says that main should take 1 of two forms:

int main();

or

int main( int argc, char* argv[] );

2. When the menu is selected and the creation of the account is made, for example, the menu is reshown but when you select it the application exits. To keep this application alive you need to do some sort of looping, until a certain option is chosen to exit the application.

3. The formatting of the code is pretty bad, but thats not so important.

I hope that helps mate,

Dave
Quote:Original post by Dave
Hi bud,

Right so i have got it working here for you:
*** Source Snippet Removed ***

You have quite alot of issues here though.

1. The C++ standard says that main should take 1 of two forms:

int main();

or

int main( int argc, char* argv[] );

2. When the menu is selected and the creation of the account is made, for example, the menu is reshown but when you select it the application exits. To keep this application alive you need to do some sort of looping, until a certain option is chosen to exit the application.

3. The formatting of the code is pretty bad, but thats not so important.

I hope that helps mate,

Dave


I have a few question.

1) what does the & in the menu() do? I took it out and it didnt work properly, but then i put it back and it worked fine.

2) can you put any variable in a functions ()'s and it becomes about of that function?
ex: menu(menuSelection);

3) How was the code formatted bad? Not to be defensive, but so I can work on it and improve on that.

BTW, thanx for pointing those errors out :)
The '&' is for passing a 'reference' of the variable 'menuSelection'. This is so that the variable in main, is the same varaible in menu. So when it is changed in menu, it is also changed in main.

Quote:Original post by SelethD
The '&' is for passing a 'reference' of the variable 'menuSelection'. This is so that the variable in main, is the same varaible in menu. So when it is changed in menu, it is also changed in main.


OOoOOoOoO! Thank you very much!
Im not quite sure your what your asking in your 2nd question, but I think this is what your wanting to know.

If I have a function...

void menu(int &selection)
{
do something in here
}

and a main function

void main(void)
{
int value;
}

can the variable 'value' be passed into the () of menu? yes
like so

void main(void)
{
int value;
menu(value);
}

this is valid, because the function 'menu' can have its own name for a variable, but its still the same variable thats sent to menu from main.

Does that make any sense?
Dude, I was trying to create something the other day, if I knew this then, I might of been successful... haha. Thanks for the help everybody.

This topic is closed to new replies.

Advertisement