DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Annu Sharma Guest

    Username and Password validation, plus switchcase


    I'm currently struggling with a problem. I have to make a program where I
    accept both username and password and if they are correct, the screen should
    then give 4 options:
    1. Item
    2. Quantity
    3. User Details
    4. Exit

    For this I know that you have to use switch case (for more than one condition).
    I also have to use the "do...while loop". I have written the following
    and if you can please correct me where I'm going wrong.....I'm honestly getting
    very lost!!

    #include<iostream.h>
    #include<string.h>
    void item();
    void quantity();
    void UserDetails();
    void exit();
    void main()
    {
    char str1[12];
    char str2[12];
    int Result;
    int Result2;
    char reply;
    int choice;
    do
    {
    cout<<"Enter username\n";
    cin>>str1;
    Result=strcmp(str1, "Joe");
    cout<<"Enter password\n";
    cin>>str2;
    Result2=strcmp(str2, "Bloggs");
    if(Result==0 && Result2==0)
    cout<<"Username and Password are valid\n";
    cout<<"1. ITEM\n";
    cout<<"2. QUANTITY\n";
    cout<<"3. USER DETAILS\n";
    cout<<"4. EXIT\n";
    cout<<"Please enter the choice (1,2,3,4)\n";
    cin>>choice;
    switch(choice)
    {
    case 1:
    {
    item();
    break;
    }
    case 2:
    {
    quantity();
    break;
    }
    case 3:
    {
    UserDetails();
    break;
    }
    case 4:
    {
    exit();
    break;
    }
    }
    void item()
    {
    int a,b,c;
    char name[12];
    cout<<"Enter item name\n";
    cin>>name;
    cout<<Enter item price\n";
    cin>>a;
    cout<<"Enter number of items\n";
    cin>>b;
    c=a*b;
    cout<<"Total price of items is: "<<c;
    }
    void quantity()
    {
    int a,b,c;
    char name[12];
    cout<<"Enter item name\n";
    cin>>name;
    cout<<"Enter quantity on hand\n";
    cin>>a;
    cout<<"Enter quantity ordered\n";
    cin>>b;
    c=a-b;
    cout<<"The quantity value is: "<<c;
    }
    void UserDetails()
    {
    char name[12];
    char address[30];
    char phone[9];
    int purchase;
    cout<<"Enter name\n";
    cin>>name;
    cout<<"Enter address\n";
    cin>>address;
    cout<<"Enter phone number\n";
    cin>>phone;
    cout<<"Enter number of items purchased\n";
    cin>>purchase;
    }
    else
    {
    cout<<"Username and Password are invalid\n";
    }
    cout<<"Do you want to continue (y/n)?\n";
    cin>>reply;
    }while(reply=='y');
    }


    I get one error message "Declaration syntax error" but I can't think of how
    to resolve this. I would really appreciate it if you could help me solve
    it.



  2. #2
    vector Guest

    Re: Username and Password validation, plus switchcase


    Annu,

    You need to remove the semicolon after the last statement unless your doing
    something else which I cannot think of that would cause you to have a semicolon
    after I while loop.


    >}while(reply=='y');<-------------remove semicolon,

    then recompile

    Try it,:)


    Vector

  3. #3
    Jim Annunziato Guest

    Re: Username and Password validation, plus switchcase


    Annu-- you have a few errors in your program, I rewrote it enough so that
    it will compile and run. There is still quite a bit you can do to the program
    to format it the way you would like. I tried to put comments where I found
    errors; here is the source code so you can compare it with yours. Also it
    is easier to read the code you write if you indent. Hope this helps you.


    #include<iostream.h>
    #include <stdlib.h>
    #include<string.h>


    void item();
    void quantity();
    void UserDetails();

    //void exit(); you can use the built in function exit in the
    // stdlib.h file"



    void main()
    {


    char str1[12];
    char str2[12];
    int Result;//normally a variable is all lower case unless it is
    //a constant.

    int Result2;

    //char reply; reply is not used anywhere in your program. you can
    // delete this line

    int choice;


    do
    {
    cout<<"Enter username\n";
    cin>>str1;
    Result=strcmp(str1, "Joe");
    cout<<"Enter password\n";
    cin>>str2;
    Result2=strcmp(str2, "Bloggs");
    }while ((Result !=0)&& (Result2 !=0));// you need to have a
    //while statement with
    // a do. do...while.



    //if(Result==0 && Result2==0) you do not need this if line now.

    cout<<"Username and Password are valid\n";
    cout<<"1. ITEM\n";
    cout<<"2. QUANTITY\n";
    cout<<"3. USER DETAILS\n";
    cout<<"4. EXIT\n";
    cout<<"Please enter the choice (1,2,3,4)\n";
    cin>>choice;

    switch(choice)
    {
    case 1: item();
    break;

    case 2: quantity();
    break;

    case 3: UserDetails();
    break;

    case 4: exit(0);

    }


    }




    void item()
    {
    int a,b,c;
    char name[12];

    cout<<"Enter item name\n";
    cin>>name; //you declared the char array "name", but you
    // didn't use it anywhere in the function.

    cout<<"Enter item price\n";
    cin>>a;
    cout<<"Enter number of items\n";
    cin>>b;
    c=a*b;
    cout<<"Total price of "<<name<<" is: "<<c;
    // I inserted name down here.
    }


    void quantity()
    {
    int a,b,c;
    char name[12];

    cout<<"Enter item name\n";
    cin>>name; // same as above variable declared but not
    // used

    cout<<"Enter quantity on hand\n";
    cin>>a;
    cout<<"Enter quantity ordered\n";
    cin>>b;
    c=a-b;
    cout<<"The quantity of " <<name <<" is: "<<c;
    // added name here again
    }


    void UserDetails()
    {
    char name[12];
    char address[30];
    char phone[9];
    int purchase;

    cout<<"Enter name\n";
    cin>>name;
    cout<<"Enter address\n";
    cin>>address;
    cout<<"Enter phone number\n";
    cin>>phone;
    cout<<"Enter number of items purchased\n";
    cin>>purchase;
    // this data is is brought in, but is not used anywhere
    // in the program.
    }















    "Annu Sharma" <una_00@yahoo.com> wrote:
    >
    >I'm currently struggling with a problem. I have to make a program where

    I
    >accept both username and password and if they are correct, the screen should
    >then give 4 options:
    >1. Item
    >2. Quantity
    >3. User Details
    >4. Exit
    >
    >For this I know that you have to use switch case (for more than one condition).
    > I also have to use the "do...while loop". I have written the following
    >and if you can please correct me where I'm going wrong.....I'm honestly

    getting
    >very lost!!
    >
    >#include<iostream.h>
    >#include<string.h>
    >void item();
    >void quantity();
    >void UserDetails();
    >void exit();
    >void main()
    >{
    >char str1[12];
    >char str2[12];
    >int Result;
    >int Result2;
    >char reply;
    >int choice;
    >do
    >{
    >cout<<"Enter username\n";
    >cin>>str1;
    >Result=strcmp(str1, "Joe");
    >cout<<"Enter password\n";
    >cin>>str2;
    >Result2=strcmp(str2, "Bloggs");
    >if(Result==0 && Result2==0)
    >cout<<"Username and Password are valid\n";
    >cout<<"1. ITEM\n";
    >cout<<"2. QUANTITY\n";
    >cout<<"3. USER DETAILS\n";
    >cout<<"4. EXIT\n";
    >cout<<"Please enter the choice (1,2,3,4)\n";
    >cin>>choice;
    >switch(choice)
    >{
    >case 1:
    > {
    > item();
    > break;
    > }
    >case 2:
    > {
    >quantity();
    >break;
    >}
    >case 3:
    >{
    >UserDetails();
    >break;
    >}
    >case 4:
    >{
    >exit();
    >break;
    >}
    > }
    >void item()
    >{
    >int a,b,c;
    >char name[12];
    >cout<<"Enter item name\n";
    >cin>>name;
    >cout<<Enter item price\n";
    >cin>>a;
    >cout<<"Enter number of items\n";
    >cin>>b;
    >c=a*b;
    >cout<<"Total price of items is: "<<c;
    >}
    >void quantity()
    >{
    >int a,b,c;
    >char name[12];
    >cout<<"Enter item name\n";
    >cin>>name;
    >cout<<"Enter quantity on hand\n";
    >cin>>a;
    >cout<<"Enter quantity ordered\n";
    >cin>>b;
    >c=a-b;
    >cout<<"The quantity value is: "<<c;
    >}
    >void UserDetails()
    >{
    >char name[12];
    >char address[30];
    >char phone[9];
    >int purchase;
    >cout<<"Enter name\n";
    >cin>>name;
    >cout<<"Enter address\n";
    >cin>>address;
    >cout<<"Enter phone number\n";
    >cin>>phone;
    >cout<<"Enter number of items purchased\n";
    >cin>>purchase;
    >}
    >else
    >{
    >cout<<"Username and Password are invalid\n";
    >}
    >cout<<"Do you want to continue (y/n)?\n";
    >cin>>reply;
    >}while(reply=='y');
    >}
    >
    >
    >I get one error message "Declaration syntax error" but I can't think of

    how
    >to resolve this. I would really appreciate it if you could help me solve
    >it.
    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links