CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2010
    Posts
    1

    Unhappy I need some help!

    Okay, I need some help. This is a calculator program. The Calculator part works but, I am trying to make a password thing so that only me and my friends can access it. Here is what I though might have worked but, didn't.
    #include <iostream>

    using namespace std;

    int main(void)
    {
    {
    system("TITLE Calculator");
    system("COLOR 31");
    char cChar;
    double x;
    double y;
    char leave;
    char password;
    char cDoagain;


    do
    {
    {
    cout<<"Please input admin Password: \n";
    cin >> password;

    {
    if ( password == 31 ) {
    cout<<"Welcome, GeckoGamer!\n";
    system("CLS");
    cout<<"Welcome to Anthony's Calculator V. 1.1! (Working on new functions)\n";
    cout<<"Last update:22/10/10 19:53\n";
    cout<<" \n";
    cout<<"If you would like to leave now hit 1. If you would like to stay in the program \nhit 2.\n";
    cin >> leave;

    switch (leave)
    {
    case '1':
    cout << "Good-bye!\n";
    break;

    cout<<"Please input the first number you would like to use: \n";
    cin >> x;
    cout<<"Please input the operation you would like to use: \n" << " +,-,* or /\n";
    cin >> cChar;
    cout<<"Please input the second number you would like to use: \n";
    cin >> y;

    switch (cChar)
    {
    case '+':
    cout << "The answer is: \n" << x << " + " <<
    y << " = " << (x + y);
    break;
    case '-':
    cout << "The answer is: \n" << x << " - " <<
    y << " = " << (x - y);
    break;
    case '*':
    cout << "The answer is: \n" << x << " * " <<
    y << " = " << (x * y);
    break;
    case 'x':
    cout << "The answer is: \n" << x << " x " <<
    y << " = " << (x * y);
    break;
    case '/':
    if(y == 0){
    cout << "That is an invalid operation\n";
    }else{
    cout << "The answer is: \n" << x << " / " <<
    y << " = " << (x / y);
    }
    break;
    default:
    cout << "That is an invalid operation\n";
    break;
    }
    cout << "Would you like to start again? (y or n)\n";
    cin >> cDoagain;
    }
    }else {
    cout<<"Incorrect password! Please leave the program!\n";
    break;
    }
    cout << "Would you like to start again? (y or n)\n";
    cin >> cDoagain;
    }
    }while (cDoagain == 'Y' || cDoagain == 'y');
    system("PAUSE");
    return 0;
    }
    Please help me!

  2. #2
    Join Date
    Jun 2009
    Posts
    100

    Re: I need some help!

    Wow I have WAY to much free time. :-)

    There were several things wrong with your code. I highly recommend a C++ programming book. If you are following one then read more carefully.

    I had to redo most of the code to make it work.

    Yes I DID use the dreaded goto. I have been programming in assembly for so long that I didn't really see it as being a problem.

    It could be cleaned up but this was just a rough draft. My goal was to make it work and thats what I did.

    Don't expect people to do your work for you. Most of the time people will just give you hints. :-)

    Why it is NOT a good idea to use the system() function unless you really need it: http://www.cplusplus.com/forum/articles/11153/

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    using namespace std;
    
    char cChar;
    float x;
    float y;
    int leave;
    int password;
    char cDoagain;
    int runnable = 1;
    
    void login()
    
    {
    	
    jmp:
    
    cout<<"Please input admin Password: ";
    cin >> password;
    
    if (password == 31)
    
    {
    	
    system("CLS");
    cout<<"Welcome, GeckoGamer!\n";
    cout<<"Welcome to Anthony's Calculator V. 1.1! (Working on new functions)\n";
    cout<<"Last update:22/10/10 23:30\n";
    cout<<" \n";
    cout<<"If you would like to leave now hit 1. If you would like to stay in the program \nhit 2: ";
    cin >> leave;
    
    }
    
    else
    
    {
    	cout<<"Incorrect password! \n";
    	goto jmp;
    }
    
    if (leave == 1)
    	exit(0);
    
    
    }
    
    int main()
    
    {
    	
    system("TITLE Calculator");
    system("COLOR 31");
    
    login();
    
    while (runnable == 1)
    
    {
    
    cout<<"Please input the first number you would like to use: ";
    cin >> x;
    cout<<"Please input the operation you would like to use: \n" << " +,-,* or /\n" << ": ";
    cin >> cChar;
    cout<<"Please input the second number you would like to use: ";
    cin >> y;
    
    switch (cChar)
    
    {
    	
    case '+':
    	cout << "The answer is: \n" << x << " + " <<
    	y << " = " << (x + y);
    break;
    
    case '-':
    	cout << "The answer is: \n" << x << " - " <<
    	y << " = " << (x - y);
    break;
    
    case '*':
    	cout << "The answer is: \n" << x << " * " <<
    	y << " = " << (x * y);
    break;
    
    case 'x':
    	cout << "The answer is: \n" << x << " x " <<
    	y << " = " << (x * y);
    break;
    
    case '/':
    	
    	if(y == 0)
    		cout << "That is an invalid operation\n";
    
    	else
    	
    	{
    		cout << "The answer is: \n" << x << " / " <<
    		y << " = " << (x / y);
    	}
    	
    break;
    
    default:
    	cout << "That is an invalid operation\n";
    break;
    
    }
    
    cout << "\nWould you like to start again? (y or n)\n";
    cin >> cDoagain;
    
    if (cDoagain == 'n')
    	runnable = 0;
    
    }
    
    return 0;
    
    }
    Last edited by David2010; October 22nd, 2010 at 11:25 PM. Reason: Fixed typos

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: I need some help!

    Quote Originally Posted by David2010 View Post
    Yes I DID use the dreaded goto. I have been programming in assembly for so long that I didn't really see it as being a problem.
    Code:
    void login()
    {
        bool bDoAgain = true;
    
        while ( bDoAgain )
        {
            cout<<"Please input admin Password: ";
            cin >> password;
            if (password == 31)
            {
                  // code goes here
                  //...
                  bDoAgain = false;
            }
            else
                cout<<"Incorrect password! \n"
        }
    }
    No need for goto whatsoever.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured