Program Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gonzal51

    Program Help

    Hi, I'm a student learning C++ and my professor isn't much help, her view is
    if you dont get it I ain't helping you....

    We are supposed to do a basic program as a logon system. I got as far as I
    could and came up with some errors I could not understand.

    Any Help would be appreciated.


    Using: MS Visual Studio .Net 2003
    Language: C++
    Program centers around: Classes


    **CODE**
    /*
    Name: Matthew Gonzalez
    Prog Name: gonzalez_PA10
    Date: 4/27/05
    Purpose: Classes
    */

    #include <iostream>
    using namespace std;

    //class
    class User
    {
    private:

    int userId;
    char firstName[20];
    char lastName[20];
    char password[20];
    public:

    void setUserData(cha r firstName[20],char lastName[20],char password[20]);
    void changePassword (char password[20],char newPass[20]);
    void displayUser(int userId,char firstName[20],char lastName[20], char
    password[20]);
    };
    //end class
    User::setUserDa ta(char firstName[20],char lastName[20],char password[20])
    {
    cout<<"Please Enter Your First Name: "<<endl;
    cin>>firstName[20];
    cout<<"Please Enter your Last Name: "<<endl;
    cin>>lastName[20];
    cout<<"Please Enter your Password: "<<endl;
    cin>>password[20];
    //end setuserdatafunc tion
    }
    User::changePas sword(char password[20],char newPass[20])
    {
    cout<<"Please enter new password"<<endl ;
    cin>>newPass[20];

    if(password[20]==newPass[20])
    {
    cout<<"Your password is the same. No Change will occur."<<endl;
    }
    else
    {
    if(password[20]!=newPass[20])

    newPass[20]=password[20];
    }
    }
    User::displayUs er(int userId,char firstName[20],char lastName[20], char
    password[20])
    {
    cout<<"The User id is: "<<userId<<endl ;
    cout<<"The First name is: "<<firstNam e[20]<<endl;
    cout<<"The Last name is: "<<lastName[20]<<endl;
    cout<<"The password is: "<<password[20]<<endl;
    //end display user function
    }

    int main()
    {
    //user object
    User logon;
    int userId;
    char firstName[20];
    char lastName[20];
    char password[20];
    char newPass[20];
    char answer;

    cout<<"Please enter a user Id: "<<endl;
    cin>>userId;
    logon.setUserDa ta(char firstName[20],char lastName[20],char password[20]);
    cout<<"Do you want to change your password? Enter Y or N: "<<endl;
    cin>>answer;
    //if
    if(answer=='y')
    {
    logon.changePas sword(char password[20],char newPass[20]);
    logon.displayUs er(int UserId,char firstName[20],char lastName[20],char
    password[20]);
    }
    else

    if(answer=='n')
    {
    logon.displayUs er(int UserId,char firstName[20],char lastName[20],char
    password[20]);
    }
    //endif

    return 0;
    }



    **END CODE**



  • Ioannis Vranos

    #2
    Re: Program Help

    gonzal51 wrote:
    [color=blue]
    > Hi, I'm a student learning C++ and my professor isn't much help, her view is
    > if you dont get it I ain't helping you....
    >
    > We are supposed to do a basic program as a logon system. I got as far as I
    > could and came up with some errors I could not understand.
    >
    > Any Help would be appreciated.
    >
    >
    > Using: MS Visual Studio .Net 2003
    > Language: C++
    > Program centers around: Classes
    >
    >
    > **CODE**
    > /*
    > Name: Matthew Gonzalez
    > Prog Name: gonzalez_PA10
    > Date: 4/27/05
    > Purpose: Classes
    > */
    >
    > #include <iostream>
    > using namespace std;
    >
    > //class
    > class User
    > {
    > private:
    >
    > int userId;
    > char firstName[20];
    > char lastName[20];
    > char password[20];[/color]


    Using std::string instead of char arrays would be better.

    [color=blue]
    > public:
    >
    > void setUserData(cha r firstName[20],char lastName[20],char password[20]);
    > void changePassword (char password[20],char newPass[20]);
    > void displayUser(int userId,char firstName[20],char lastName[20], char
    > password[20]);
    > };
    > //end class[/color]

    void User::setUserDa ta(char firstName[20],char lastName[20],char password[20])
    [color=blue]
    > {
    > cout<<"Please Enter Your First Name: "<<endl;
    > cin>>firstName[20];
    > cout<<"Please Enter your Last Name: "<<endl;
    > cin>>lastName[20];
    > cout<<"Please Enter your Password: "<<endl;
    > cin>>password[20];
    > //end setuserdatafunc tion
    > }[/color]

    void User::changePas sword(char password[20],char newPass[20])
    [color=blue]
    > {
    > cout<<"Please enter new password"<<endl ;
    > cin>>newPass[20];
    >
    > if(password[20]==newPass[20])
    > {
    > cout<<"Your password is the same. No Change will occur."<<endl;
    > }
    > else
    > {
    > if(password[20]!=newPass[20])
    >
    > newPass[20]=password[20];
    > }
    > }[/color]

    void User::displayUs er(int userId,char firstName[20],char lastName[20], char
    [color=blue]
    > password[20])
    > {
    > cout<<"The User id is: "<<userId<<endl ;
    > cout<<"The First name is: "<<firstNam e[20]<<endl;
    > cout<<"The Last name is: "<<lastName[20]<<endl;
    > cout<<"The password is: "<<password[20]<<endl;
    > //end display user function
    > }
    >
    > int main()
    > {
    > //user object
    > User logon;
    > int userId;
    > char firstName[20];
    > char lastName[20];
    > char password[20];
    > char newPass[20];
    > char answer;
    >
    > cout<<"Please enter a user Id: "<<endl;
    > cin>>userId;[/color]

    logon.setUserDa ta(firstName, lastName, password);
    [color=blue]
    > cout<<"Do you want to change your password? Enter Y or N: "<<endl;
    > cin>>answer;
    > //if
    > if(answer=='y')
    > {[/color]

    logon.changePas sword(password, newPass);
    logon.displayUs er(userId, firstName, lastName, password);

    [color=blue]
    > }
    > else
    >
    > if(answer=='n')
    > {[/color]

    logon.displayUs er(userId, firstName, lastName, password);
    [color=blue]
    > }
    > //endif
    >
    > return 0;
    > }[/color]


    Just fixed it to compile. I did not check or even run the code.

    Comment

    • gonzal51

      #3
      Re: Program Help

      I was wondering, (while I work on it) what were the problems I was having?


      "Ioannis Vranos" <ivr@remove.thi s.grad.com> wrote in message
      news:OvsHr7qTFH A.2908@TK2MSFTN GP10.phx.gbl...[color=blue]
      > gonzal51 wrote:
      >[color=green]
      > > Hi, I'm a student learning C++ and my professor isn't much help, her[/color][/color]
      view is[color=blue][color=green]
      > > if you dont get it I ain't helping you....
      > >
      > > We are supposed to do a basic program as a logon system. I got as far as[/color][/color]
      I[color=blue][color=green]
      > > could and came up with some errors I could not understand.
      > >
      > > Any Help would be appreciated.
      > >
      > >
      > > Using: MS Visual Studio .Net 2003
      > > Language: C++
      > > Program centers around: Classes
      > >
      > >
      > > **CODE**
      > > /*
      > > Name: Matthew Gonzalez
      > > Prog Name: gonzalez_PA10
      > > Date: 4/27/05
      > > Purpose: Classes
      > > */
      > >
      > > #include <iostream>
      > > using namespace std;
      > >
      > > //class
      > > class User
      > > {
      > > private:
      > >
      > > int userId;
      > > char firstName[20];
      > > char lastName[20];
      > > char password[20];[/color]
      >
      >
      > Using std::string instead of char arrays would be better.
      >
      >[color=green]
      > > public:
      > >
      > > void setUserData(cha r firstName[20],char lastName[20],char[/color][/color]
      password[20]);[color=blue][color=green]
      > > void changePassword (char password[20],char newPass[20]);
      > > void displayUser(int userId,char firstName[20],char lastName[20], char
      > > password[20]);
      > > };
      > > //end class[/color]
      >
      > void User::setUserDa ta(char firstName[20],char lastName[20],char[/color]
      password[20])[color=blue]
      >[color=green]
      > > {
      > > cout<<"Please Enter Your First Name: "<<endl;
      > > cin>>firstName[20];
      > > cout<<"Please Enter your Last Name: "<<endl;
      > > cin>>lastName[20];
      > > cout<<"Please Enter your Password: "<<endl;
      > > cin>>password[20];
      > > //end setuserdatafunc tion
      > > }[/color]
      >
      > void User::changePas sword(char password[20],char newPass[20])
      >[color=green]
      > > {
      > > cout<<"Please enter new password"<<endl ;
      > > cin>>newPass[20];
      > >
      > > if(password[20]==newPass[20])
      > > {
      > > cout<<"Your password is the same. No Change will occur."<<endl;
      > > }
      > > else
      > > {
      > > if(password[20]!=newPass[20])
      > >
      > > newPass[20]=password[20];
      > > }
      > > }[/color]
      >
      > void User::displayUs er(int userId,char firstName[20],char lastName[20],[/color]
      char[color=blue]
      >[color=green]
      > > password[20])
      > > {
      > > cout<<"The User id is: "<<userId<<endl ;
      > > cout<<"The First name is: "<<firstNam e[20]<<endl;
      > > cout<<"The Last name is: "<<lastName[20]<<endl;
      > > cout<<"The password is: "<<password[20]<<endl;
      > > //end display user function
      > > }
      > >
      > > int main()
      > > {
      > > //user object
      > > User logon;
      > > int userId;
      > > char firstName[20];
      > > char lastName[20];
      > > char password[20];
      > > char newPass[20];
      > > char answer;
      > >
      > > cout<<"Please enter a user Id: "<<endl;
      > > cin>>userId;[/color]
      >
      > logon.setUserDa ta(firstName, lastName, password);
      >[color=green]
      > > cout<<"Do you want to change your password? Enter Y or N: "<<endl;
      > > cin>>answer;
      > > //if
      > > if(answer=='y')
      > > {[/color]
      >
      > logon.changePas sword(password, newPass);
      > logon.displayUs er(userId, firstName, lastName, password);
      >
      >[color=green]
      > > }
      > > else
      > >
      > > if(answer=='n')
      > > {[/color]
      >
      > logon.displayUs er(userId, firstName, lastName, password);
      >[color=green]
      > > }
      > > //endif
      > >
      > > return 0;
      > > }[/color]
      >
      >
      > Just fixed it to compile. I did not check or even run the code.[/color]


      Comment

      • Ioannis Vranos

        #4
        Re: Program Help

        gonzal51 wrote:
        [color=blue]
        > I was wondering, (while I work on it) what were the problems I was having?[/color]

        ?

        Comment

        • Mihajlo Cvetanovic

          #5
          Re: Program Help

          gonzal51 wrote:[color=blue]
          > I was wondering, (while I work on it) what were the problems I was having?
          >[color=green]
          >>void User::setUserDa ta(char firstName[20],char lastName[20],char[/color][/color]

          When you omit return type (void in this case) the default type is int,
          so the compiler expects that you actually return something (return 0).
          Since you don't return anything compiler calls it an error.
          [color=blue][color=green]
          >> logon.setUserDa ta(firstName, lastName, password);[/color][/color]

          When you call a function you put arguments in argument list. But "char
          firstName[20]" is not an argument. "firstName" is an argument. To the
          compiler "char firstName[20]" looks like a declaration, but the whole
          line doesn't look like anything recognizable, that's why it's confused
          and reports an error.

          Comment

          • Ioannis Vranos

            #6
            Re: Program Help

            Mihajlo Cvetanovic wrote:
            [color=blue]
            > When you omit return type (void in this case) the default type is int,
            > so the compiler expects that you actually return something (return 0).
            > Since you don't return anything compiler calls it an error.[/color]


            I think you are probably talking about pre-standard C++. By omitting the return type, no
            int is implied, it is an error. :-)

            Comment

            • Mihajlo Cvetanovic

              #7
              Re: Program Help

              Ioannis Vranos wrote:[color=blue]
              >
              > I think you are probably talking about pre-standard C++. By omitting the
              > return type, no int is implied, it is an error. :-)[/color]

              Alas, VC71 assumes int and gives only warnings, C4183 for member
              functions and C4508 for global functions, but it wouldn't be an error to
              treat is as such :-)

              Comment

              • Ioannis Vranos

                #8
                Re: Program Help

                Mihajlo Cvetanovic wrote:
                [color=blue]
                > Alas, VC71 assumes int and gives only warnings, C4183 for member
                > functions and C4508 for global functions, but it wouldn't be an error to
                > treat is as such :-)[/color]


                Let's use some simple code:


                class SomeClass
                {
                public:
                void somefunc();
                }

                SomeClass::some func() {}


                int main()
                {
                }



                Do you get such warnings for this?


                For this

                class SomeClass
                {
                public:
                somefunc();
                }

                SomeClass::some func() {}


                int main()
                {
                }

                I get such a warning, but it is followed by bizarre errors and does not compile either. :-)

                Comment

                • Ioannis Vranos

                  #9
                  Re: Program Help

                  Ioannis Vranos wrote:
                  [color=blue]
                  > For this
                  >
                  > class SomeClass
                  > {
                  > public:
                  > somefunc();[/color]
                  };
                  [color=blue]
                  > SomeClass::some func() {}
                  >
                  >
                  > int main()
                  > {
                  > }
                  >
                  > I get such a warning, but it is followed by bizarre errors and does not
                  > compile either. :-)[/color]


                  I had omitted the ';' in the class definition. Yes you are right, it gives such a warning
                  and compiles. However this is not standard C++ behaviour, you may consider it as a system
                  extension (which probably would be better to be fixed sooner or later). :-)

                  Comment

                  • gonzal51

                    #10
                    Re: Program Help

                    I meant, while I load it into my visual.NET and physically look at it as
                    well as changes you've made (thanks!). Although I found that while it
                    compiles, I can enter an userId, and FirstName, but soon after it shows the
                    other outputs, i'm guessing its a problem with my SetUserData function. Such
                    is what I meant by working on it. :-)


                    "Ioannis Vranos" <ivr@remove.thi s.grad.com> wrote in message
                    news:%23nAosWtT FHA.1600@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                    > gonzal51 wrote:
                    >[color=green]
                    > > I was wondering, (while I work on it) what were the problems I was[/color][/color]
                    having?[color=blue]
                    >
                    > ?[/color]


                    Comment

                    • gonzal51

                      #11
                      Re: Program Help

                      Thanks for the info. I appreciate it.

                      "> >> logon.setUserDa ta(firstName, lastName, password);[color=blue]
                      >
                      > When you call a function you put arguments in argument list. But "char
                      > firstName[20]" is not an argument. "firstName" is an argument. To the
                      > compiler "char firstName[20]" looks like a declaration, but the whole
                      > line doesn't look like anything recognizable, that's why it's confused
                      > and reports an error.[/color]


                      Comment

                      Working...