1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Regarding C++ Problem Disscusion

  1. Jul 31, 2012 #1
    i made a c++ login program but There is Problem occured During Compairing a Given user name (string ) with a Pre defined String. Please give me some solution of it.
    program


    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    class converter
    {
    public:
    double i,j;
    char m;
    void acc()
    {
    cout<<"Enter ur choice(f for km and g for kg)";
    cin>>m;
    switch(m)
    {
    case 'f':
    {
    cout<<"Enter no. of km:";
    cin>>i;
    j=1000*i;
    cout<<i<<" km is equal to "<<j<<" m";
    break;
    }
    case 'g':
    {
    cout<<"Enter kg:";
    cin>>i;
    j=1000*i;
    cout<<i<<" kg is equal to "<<j<<" gm";
    break;
    }
    default:
    {
    cout<<"Wrong Choice";
    }
    }
    }
    };
    class calculator
    {
    public:
    double i,j,k;
    char c;
    void accept()
    {
    cout<<"Enter two no:";
    cin>>i>>j;
    cout<<"Enter ur choice(+,-,*,/):";
    cin>>c;
    switch(c)
    {
    case '+':
    {
    k=i+j;
    cout<<"Sum is:"<<k;
    break;
    }
    case '-':
    {
    k=i-j;
    cout<<"sub. is:"<<k;
    break;
    }
    case '*':
    {
    k=i*j;
    cout<<"mul. is :"<<k;
    break;
    }
    case '/':
    {
    k=i/j;
    cout<<"div. is :"<<k;
    break;
    }
    default:
    {
    cout<<"Wrong Choice";
    }
    }
    }
    };
    void main()
    {
    clrscr();
    int pass,pass1=9876,a,b=2,h;
    char d,user[5],user1[]="rahul";
    for(a=0;a<3;a++)
    {
    cout<<"Enter User Name:";
    for(h=0;h<5;h++)
    {
    cin>>user[h];
    }
    cout<<"\nPlease Enter ur Password:";
    cin>>pass;
    if(pass==pass1&&strcmp(user,user1)==0)
    {
    cout<<"Enter ur choice (c for converter and v for calculator :";
    cin>>d;
    switch(d)
    {
    case 'c':
    {
    converter c1;
    c1.acc();
    break;
    }
    case 'v':
    {
    calculator c2;
    c2.accept();
    break;
    }
    default:
    cout<<"Wrong choice";
    }
    break;
    }
    else
    {
    cout<<"wrong Password\n";
    cout<<"You have "<<b<<" chances Remaining.\n";
    if(b==0)
    {
    cout<<"\nSorry Your Account is Blocked.";
    }
    b--;
    }
    }
    getch();
    }
     
  2. jcsd
  3. Jul 31, 2012 #2
    without username using in this program it shows no error but as i use it with user name then it does't accept the right password.
    predefined user name- rahul
    predefined password- 9876
     
  4. Jul 31, 2012 #3

    uart

    User Avatar
    Science Advisor

    Hi Rahulbwn. The strcmp function requires null terminated strings. That is, strings for which the last characters is '\0' (or char(0) if you prefer).

    With the code char user1[]="rahul";, the compiler automatically generates the terminating null character.

    The code char user[5]; user[0]='r'; ...user[4]='l'; however doesn't result in a properly terminated string.
     
    Last edited: Jul 31, 2012
  5. Jul 31, 2012 #4

    Mark44

    Staff: Mentor

    One thing that jumps out at me is that you haven't included space for the terminating null character in your password string. If the password consists of five letters ("rahul"), you need to allocate storage for six characters, like this.
    Code (Text):

    char user[6];
     
    BTW, your code would be easier to read if (1) it was indented properly, and (2) you used [ code ] and [ /code ] tags (without the extra spaces).

    Here's how I would indent one of yyour (not "ur") functions.
    Code (Text):

    void acc()
    {
      cout<<"Enter ur choice(f for km and g for kg)";
      cin>>m;
      switch(m)
      {
        case 'f':
        {
          cout<<"Enter no. of km:";
          cin>>i;
          j=1000*i;
          cout<<i<<" km is equal to "<<j<<" m";
          break;
        }
        case 'g':
        {
          cout<<"Enter kg:";
          cin>>i;
          j=1000*i;
          cout<<i<<" kg is equal to "<<j<<" gm";
          break;
        }
        default:
        {
          cout<<"Wrong Choice";
        }
      }
    }

     
     
  6. Jul 31, 2012 #5
    Thanks for solving this problem.
    I have made some changes to this program.
    And it works perfectly. Now i want that as the user completes his task. The compiler will ask him press any key or 5 to go back to t
    he main menu. For this purpose i used here go to statement. But problem is that here sequence of goto statement gets reversed.
     
    Last edited: Jul 31, 2012
  7. Jul 31, 2012 #6

    Mark44

    Staff: Mentor

    Minor point: Your program will prompt the user to do something, not the compiler.
    I don't understand what you're saying about the "sequence of goto statement gets reversed." I'm sure that this code can be written without the use of goto statements.

    Show me the code you've written.
     
  8. Jul 31, 2012 #7
    ok i am posting code please check it show an error that is undefined label menu. i have mensioned by //please check here. comments in my program which shows an error.


    program



    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    class converter
    {
    public:
    double i,j;
    int m,k;
    void acc()
    {
    cout<<"\nWhat do you Want to convert:\n\n1 - for kilometers into meters.\n2 - for Kilograms into Grams.\n3 - for exit.\n\nEnter your choice:";
    cin>>m;
    switch(m)
    {
    case 1:
    {
    cout<<"\nEnter no. of Kilometers:";
    cin>>i;
    j=1000*i;
    cout<<"\n"<<i<<" km is equal to "<<j<<" m";
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    cout<<"Press 0 for go back to the main menu:"; //please check here.
    cin>>k;
    if(k==0)
    {
    goto menu;
    }
    else
    break;
    }
    case 2:
    {
    cout<<"\nEnter the no. of Kilograms:";
    cin>>i;
    j=1000*i;
    cout<<"\n"<<i<<" kg is equal to "<<j<<" gm";
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    break;
    }
    case 3:
    {
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    cout<<"\n\nThanks for using my software. \nI hope you like it & in future i will try to give \nmy best for the Develpment of Softwares.\n\n\n\n\t\t\t\t\t\t\tMr. Rahul\n\n\t\t\t\t\t\t\t(Software Developer)";
    break;
    }
    default:
    {
    cout<<"\nWrong Choice.";
    }
    }
    }
    };
    class calculator
    {
    public:
    double i,j,k;
    char c;
    void accept()
    {
    cout<<"\nEnter First no.:";
    cin>>i;
    cout<<"\nEnter Second no.:";
    cin>>j;
    cout<<"\nEnter the operation which you want to perform:\n\n+ for Addition.\n- for Subtraction.\n* for Multiplication.\n/ for Division.\ne for exit.\n\nEnter your choice:";
    cin>>c;
    switch(c)
    {
    case '+':
    {
    k=i+j;
    cout<<"\nSum of "<<i<<" and "<<j<<" is:"<<k;
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    break;
    }
    case '-':
    {
    k=i-j;
    cout<<"\nSubtraction of "<<i<<" and "<<j<<" is:"<<k;
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    break;
    }
    case '*':
    {
    k=i*j;
    cout<<"\nMultiplication of "<<i<<" and "<<j<<" is :"<<k;
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    break;
    }
    case '/':
    {
    k=i/j;
    cout<<"\nDivision of "<<i<<" and "<<j<<" is :"<<k;
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    break;
    }
    case 'e':
    {
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    cout<<"\n\nThanks for using my software. \nI hope you like it & in future i will try to give \nmy best for the Develpment of Softwares.\n\n\n\n\t\t\t\t\t\t\tMr. Rahul\n\n\t\t\t\t\t\t\t(Software Developer)";
    break;
    }
    default:
    {
    cout<<"\nWrong Choice.";
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    }
    }
    }
    };
    void main()
    {
    clrscr();
    int pass,pass1=9876,a,b=2,d,h;
    char user[6],user1[]="rahul";
    cout<<"\t\t\t\tSoftware Developer\n\n\t\t\t\tMr. Rahul\n\n\t\tWelcomes you in the world of Amazing Softwares.\n\n";
    cout<<"\n\n--------------------------------------------------------------------------------";
    cout<<"--------------------------------------------------------------------------------\n\n";
    for(a=0;a<3;a++)
    {
    cout<<"\nLogin Your Account.\n";
    cout<<"\nEnter Your Username:";
    for(h=0;h<5;h++)
    {
    cin>>user[h];
    }
    cout<<"\nPlease Enter Your Login Password:";
    cin>>pass;
    if(pass==pass1&&strcmp(user1,user)==0)
    {
    cout<<"\n\nYou have Sucessfully login your account.\n\n";
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    menu:cout<<"Main Menu:\n\n"; // please check here.
    cout<<"What Do You Want to do:\n\n1 - for converter.\n2 - for calculator.\n3 - for exit.\n\nEnter your choice:";
    cin>>d;
    switch(d)
    {
    case 1:
    {
    converter c1;
    c1.acc();
    break;
    }
    case 2:
    {
    calculator c2;
    c2.accept();
    break;
    }
    case 3:
    {
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    cout<<"\n\nThanks for using my software. \nI hope you like it & in future i will try to give \nmy best for the Develpment of Softwares.\n\n\n\n\t\t\t\t\t\t\tMr. Rahul\n\n\t\t\t\t\t\t\t(Software Developer)";
    break;
    }
    default:
    cout<<"\nWrong choice.";
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    }
    break;
    }
    else
    {
    cout<<"\nWrong Password\n\n";
    cout<<"You have "<<b<<" chances Remaining.\n";
    cout<<"\n\n--------------------------------------------------------------------------------\n\n";
    if(b==0)
    {
    cout<<"\nSorry Your Account is Blocked.";
    }
    b--;
    }
    }
    getch();
    }
     
  9. Aug 1, 2012 #8

    Mark44

    Staff: Mentor

  10. Aug 1, 2012 #9
    sir see i have mensioned the target.
    please see the line in my program

    menu:cout<<"Main Menu:\n\n";
     
  11. Aug 1, 2012 #10
    sir give me some solution of it. I m waiting for your replay since 6 hours. I have already targeted the label (menu ) for goto statement in my program (mensioned above ). Please suject me some solution.
     
  12. Aug 1, 2012 #11

    Mark44

    Staff: Mentor

    I didn't notice that you actually do have a jump target, because it was so difficult to see. Your code is not formatted well, so it's difficult to find things.

    If you post some more code, PLEASE USE THE CODE TAGS THAT I MENTIONED. Also, please make an attempt to INDENT YOUR CODE.

    You cannot do what you're trying to do; namely jumping from one function to another. It is illegal for control to jump over a declaration with an initializer into the scope of that declaration, with a goto.

    In your acc function, you are jumping into the main function, over a bunch of variable declarations with initializers. Aside from being illegal, it is also extremely poor programming practice to jump from one function to another, as you are doing. A better way to do things is to use a loop of some kind around your menu selection code.



    Code (Text):

    void acc()
    {
       cout<<"\nWhat do you Want to convert:\n\n1 - for kilometers into meters.\n2 - for Kilograms into Grams.\n3 - for exit.\n\nEnter your choice:";
       cin>>m;
       switch(m)
       {
         case 1:
         {
           cout<<"\nEnter no. of Kilometers:";
           cin>>i;
           j=1000*i;
           cout<<"\n"<<i<<" km is equal to "<<j<<" m";
           cout<<"\n\n--------------------------------------------------------------------------------\n\n";
           cout<<"Press 0 for go back to the main menu:"; //please check here.
           cin>>k;
           if(k==0)
           {
              [color="red"]goto menu;[/color]
           }
           else
             break;
         }
         // <omitted code>
         .
         .
         .
         }
         default:
         {
           cout<<"\nWrong Choice.";
         }
       }
     }
     

    void main()
    {
       clrscr();
       int pass,pass1=9876,a,b=2,d,h;
       char user[6],user1[]="rahul";
       cout<<"\t\t\t\tSoftware Developer\n\n\t\t\t\tMr. Rahul\n\n\t\tWelcomes you in the world of Amazing Softwares.\n\n";
       cout<<"\n\n--------------------------------------------------------------------------------";
       cout<<"--------------------------------------------------------------------------------\n\n";
       for(a=0;a<3;a++)
       {
         cout<<"\nLogin Your Account.\n";
         cout<<"\nEnter Your Username:";
         for(h=0;h<5;h++)
         {
           cin>>user[h];
         }
         cout<<"\nPlease Enter Your Login Password:";
         cin>>pass;
         if(pass==pass1&&strcmp(user1,user)==0)
         {
           cout<<"\n\nYou have Sucessfully login your account.\n\n";
           cout<<"\n\n--------------------------------------------------------------------------------\n\n";
           [color="red"]menu:[/color] cout<<"Main Menu:\n\n"; // please check here.
           .
           .
           .
     
     
  13. Aug 1, 2012 #12
    ok then is there any other way of jumping from one function to another. If yes, then please sujust me.
     
  14. Aug 2, 2012 #13

    Mark44

    Staff: Mentor

    Rather than jumping from one function to another, I would advise you to reorganize the logic in your program. Your menu logic should be in main() only, not in the other functions such as acc() and others. In other words, your acc() function should do the conversion of km to meters or kg to grams and then return to a menu loop in main(). Likewise, the accept() function should do its calculation and return to the menu loop in main().

    (BTW, what does acc stand for? Names for functions and variables should be self-documenting - a reader other than yourself should get some idea what a function does just by its name. The variables you are using are not well-named, either, as most of their names are single letters that don't suggest what they're being used for. )
     
  15. Aug 2, 2012 #14
    ok i will follow it. Can you please tell me about programing errors like formatting,var. Name etc. So that i will improve it. and in future i do't repeat it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Regarding C++ Problem Disscusion
  1. C++ problems (Replies: 12)

  2. C++ Problem (Replies: 6)

  3. Problem regarding C++ (Replies: 12)

  4. C++ problem (Replies: 3)

Loading...