Regarding C++ Problem Disscusion

  • Context: Comp Sci 
  • Thread starter Thread starter rahulbwn
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around a C++ login program that encounters issues when comparing a user-provided username with a predefined string. Participants explore the problem's technical aspects, including string handling and program flow, while suggesting potential solutions and improvements.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the strcmp function requires null-terminated strings, indicating that the way the username is stored may not be correct.
  • Another participant suggests that the user array should be defined with an additional character to accommodate the null terminator, proposing char user[6] instead of char user[5].
  • A participant expresses confusion regarding the use of goto statements in their program, questioning the reversal of sequence and suggesting that the code could be structured without them.
  • There is mention of an error related to an undefined label 'menu' in the code, indicating a potential issue with the goto statement implementation.
  • Some participants provide feedback on code readability, suggesting proper indentation and formatting for clarity.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper string handling in C++, but there is no consensus on the best approach to structuring the program, particularly regarding the use of goto statements and error handling.

Contextual Notes

Limitations include potential misunderstandings about string termination in C++, the implications of using goto statements, and the need for clearer program flow. The discussion does not resolve these issues definitively.

Who May Find This Useful

Readers interested in C++ programming, particularly those dealing with string manipulation, user input handling, and program control flow.

rahulbwn
Messages
8
Reaction score
0
i made a c++ login program but There is Problem occurred 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()
{
count<<"Enter ur choice(f for km and g for kg)";
cin>>m;
switch(m)
{
case 'f':
{
count<<"Enter no. of km:";
cin>>i;
j=1000*i;
count<<i<<" km is equal to "<<j<<" m";
break;
}
case 'g':
{
count<<"Enter kg:";
cin>>i;
j=1000*i;
count<<i<<" kg is equal to "<<j<<" gm";
break;
}
default:
{
count<<"Wrong Choice";
}
}
}
};
class calculator
{
public:
double i,j,k;
char c;
void accept()
{
count<<"Enter two no:";
cin>>i>>j;
count<<"Enter ur choice(+,-,*,/):";
cin>>c;
switch(c)
{
case '+':
{
k=i+j;
count<<"Sum is:"<<k;
break;
}
case '-':
{
k=i-j;
count<<"sub. is:"<<k;
break;
}
case '*':
{
k=i*j;
count<<"mul. is :"<<k;
break;
}
case '/':
{
k=i/j;
count<<"div. is :"<<k;
break;
}
default:
{
count<<"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++)
{
count<<"Enter User Name:";
for(h=0;h<5;h++)
{
cin>>user[h];
}
count<<"\nPlease Enter ur Password:";
cin>>pass;
if(pass==pass1&&strcmp(user,user1)==0)
{
count<<"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:
count<<"Wrong choice";
}
break;
}
else
{
count<<"wrong Password\n";
count<<"You have "<<b<<" chances Remaining.\n";
if(b==0)
{
count<<"\nSorry Your Account is Blocked.";
}
b--;
}
}
getch();
}
 
Physics news on Phys.org
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
 
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:
rahulbwn said:
i made a c++ login program but There is Problem occurred During Compairing a Given user name (string ) with a Pre defined String. 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
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();
}

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:
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:
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";
    }
  }
}
 
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:
rahulbwn said:
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 the main menu.
Minor point: Your program will prompt the user to do something, not the compiler.
rahulbwn said:
For this purpose i used here go to statement. But problem is that here sequence of goto statement gets reversed.
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.
 
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()
{
count<<"\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:
{
count<<"\nEnter no. of Kilometers:";
cin>>i;
j=1000*i;
count<<"\n"<<i<<" km is equal to "<<j<<" m";
count<<"\n\n--------------------------------------------------------------------------------\n\n";
count<<"Press 0 for go back to the main menu:"; //please check here.
cin>>k;
if(k==0)
{
goto menu;
}
else
break;
}
case 2:
{
count<<"\nEnter the no. of Kilograms:";
cin>>i;
j=1000*i;
count<<"\n"<<i<<" kg is equal to "<<j<<" gm";
count<<"\n\n--------------------------------------------------------------------------------\n\n";
break;
}
case 3:
{
count<<"\n\n--------------------------------------------------------------------------------\n\n";
count<<"\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:
{
count<<"\nWrong Choice.";
}
}
}
};
class calculator
{
public:
double i,j,k;
char c;
void accept()
{
count<<"\nEnter First no.:";
cin>>i;
count<<"\nEnter Second no.:";
cin>>j;
count<<"\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;
count<<"\nSum of "<<i<<" and "<<j<<" is:"<<k;
count<<"\n\n--------------------------------------------------------------------------------\n\n";
break;
}
case '-':
{
k=i-j;
count<<"\nSubtraction of "<<i<<" and "<<j<<" is:"<<k;
count<<"\n\n--------------------------------------------------------------------------------\n\n";
break;
}
case '*':
{
k=i*j;
count<<"\nMultiplication of "<<i<<" and "<<j<<" is :"<<k;
count<<"\n\n--------------------------------------------------------------------------------\n\n";
break;
}
case '/':
{
k=i/j;
count<<"\nDivision of "<<i<<" and "<<j<<" is :"<<k;
count<<"\n\n--------------------------------------------------------------------------------\n\n";
break;
}
case 'e':
{
count<<"\n\n--------------------------------------------------------------------------------\n\n";
count<<"\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:
{
count<<"\nWrong Choice.";
count<<"\n\n--------------------------------------------------------------------------------\n\n";
}
}
}
};
void main()
{
clrscr();
int pass,pass1=9876,a,b=2,d,h;
char user[6],user1[]="rahul";
count<<"\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";
count<<"\n\n--------------------------------------------------------------------------------";
count<<"--------------------------------------------------------------------------------\n\n";
for(a=0;a<3;a++)
{
count<<"\nLogin Your Account.\n";
count<<"\nEnter Your Username:";
for(h=0;h<5;h++)
{
cin>>user[h];
}
count<<"\nPlease Enter Your Login Password:";
cin>>pass;
if(pass==pass1&&strcmp(user1,user)==0)
{
count<<"\n\nYou have Sucessfully login your account.\n\n";
count<<"\n\n--------------------------------------------------------------------------------\n\n";
menu:count<<"Main Menu:\n\n"; // please check here.
count<<"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:
{
count<<"\n\n--------------------------------------------------------------------------------\n\n";
count<<"\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:
count<<"\nWrong choice.";
count<<"\n\n--------------------------------------------------------------------------------\n\n";
}
break;
}
else
{
count<<"\nWrong Password\n\n";
count<<"You have "<<b<<" chances Remaining.\n";
count<<"\n\n--------------------------------------------------------------------------------\n\n";
if(b==0)
{
count<<"\nSorry Your Account is Blocked.";
}
b--;
}
}
getch();
}
 
sir see i have mensioned the target.
please see the line in my program

menu:count<<"Main Menu:\n\n";
 
  • #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.
 
  • #11
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:
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;[/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";
       menu:[/color] cout<<"Main Menu:\n\n"; // please check here.
       .
       .
       .
 
  • #12
ok then is there any other way of jumping from one function to another. If yes, then please sujust me.
 
  • #13
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. )
 
  • #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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 14 ·
Replies
14
Views
5K
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K