开发者

Object debugging. Class of Char Arrays

开发者 https://www.devze.com 2023-03-04 14:19 出处:网络
I hate giving out my code like this but I\'m getting some errors about my UserName class. Error code is as follows:

I hate giving out my code like this but I'm getting some errors about my UserName class.

Error code is as follows:

    232 prototype for `void UserName::setFirstName(char*)' does not match any in class `UserName'

    23 void UserName::setFirstName(char) 

    In member function `void UserName::setFirstName(char*)': 

    233 invalid conversion from `char*' to `char' 

    233 At global scope: 

    235 prototype for `void UserName::setLastName(char*)' does not match any in class `UserName
24 void UserName::setLastName(char) 
[Build Error]  [journey.o] Error 1  

I've tried a number of different ways and this is my final resting place as of now. Any insight or constructive criticism would be much appreciative.

#include <iostream>
#include "journey.h"
using namespace std;

char   firstName[26],
       lastName[31],
       fullName[57];
string confer; //name change confirm
char   nameChoice;
int    menuChoice,
       defLife;
bool   defNumORchar;
const int null = 0;

class UserName
{
      private:
              char firstName[26],
                   lastName[31],
                   fullName[57];

      public:             
             void setFirstName(char);
             void setLastName(char);
             char getName();
};

void journey(int life, bool numORchar)
{

    numcharChange(numORchar);
    clear();

    UserName user1; //defining user(s)

    cout << "What is your first name young coder?\n";
    cin >> firstName;
    cout << "What is your last name, " << firstName << "?\n";   
    cin >> lastName;
    defLife = life; //sets default (admin setting)
    defNumORchar = numORchar; //sets default (admin setting)

    clear();

    cout << "Are you sure your name is " << firstName << ' ' << lastName << "? " << confer << endl;
    cin >> nameChoice; //confirm name 

    if (nameChoice == 'N' || nameChoice == 'n' || nameChoice == '0') //all menu pref covered
    {
        clear();

        cout << "What is your first name young coder?\n";
        cin >> firstName;
        cout << "\n\nWhat is your last name, " << firstName << "?\n";
        cin >> lastName;

        clear();
    }
    else 
    {
         clear(); 

         user1.setFirstName(firstName[26]); //sets names
         user1.setLastName(lastName[31]);

         journyMenu(life, numORchar); //calls menu function
    }
}

//MENU
void journyMenu(int life, bool numORchar)
{ 
    cout << "********************************************************************************";
    cout << "*          STARTING OUT WITH C++ : EARLY OBJECTS PERSONAL TUTOR                *";
    cout << "********************************************************************************";
    cout << " " << firstName << ", please choose from the following choices:\n";
    cout << "* 1. Main Chapter Menu                                                    开发者_Go百科     *"; 
    cout << "* 2. Current life left                                                         *";
    cout << "* 3. Programming Credits                                                       *";
    cout << "* 4. Administrator Settings                                                    *";
    cout << "********************************************************************************";
    cout << endl << endl << "Choice:\n"; 
    menuComp(life, numORchar);
}

/* This function computes the choice for menu function then calls menu function again so user can make another selection */
void menuComp(int life, bool numORchar)
{           
    cin >> menuChoice;

    if (menuChoice == 1){ //Main Menu
    menu(life, numORchar); }
    else if (menuChoice == 2) //Life Left
    {
         clear();
         cout << "You currently have " << life << " lives left. Once lives reach \"0\", program restarts. \n\n Please make another choice:\n\n";
         journyMenu(life, numORchar);
    }
    else if (menuChoice == 3) //Credits
    {
         clear();
         cout << "PERSONAL TUTOR PROGRAMMING CREDITS:\n\n ~Casey Gardiner~ \n ~Doug Korody~ \n ~Jacob Armitage~ \n";  
         journyMenu(life, numORchar); 
    }
    else if (menuChoice == 4) //admin section
    {
         clear();
         admin(life, numORchar);
    }
}

int lifeBot(int life, bool numORchar)
{
       if (life > 0){
       return life; } //back to current function
       else
       //call end-of program function
       {
       char killChoice;

       clear();

       cout << "YOU HAVE REACHED \"0\" LIVES....\n\n";
       cout << "GoodBye!! Better luck next time!\n";
       cout << "Care to try again, " << firstName << "?" << confer << endl;
       cin >> killChoice;
       if (killChoice == 'Y' || killChoice == 'y' || killChoice == '1')
       {
                      life = 10;
                      clear();
                      journey(life, numORchar); 
       }
       else if (killChoice == 'N' || killChoice == 'n' || killChoice == '0'){
       exit(1); } //exit program
       }
} 

int admin(int life, bool numORchar)
{
    int input,
        adminChoice;

    char input2;

    adminMenu();
    cout << "Choice: ";
    cin >> adminChoice;

    if (adminChoice == 1)
    {
               clear();
               adminMenu();

               cout << "You currently have " << life << " lives. Please enter new value: \n\n";
               cin >> input;
               life = input;
    }
    else if (adminChoice == 2)
    {
               clear();
               adminMenu();

               cout << "Your current setting is " << confer << ". Would you like to reverse this?\n" << confer << endl << endl;
               cin >> input2;

               if (input2 == 'Y' || input2 == 'y' || input2 == '1')
               {
                    clear();

                    if (numORchar == false)
                    {
                         numORchar = true; 
                    }
                    else if (numORchar == true)
                    {
                         numORchar = false; 

                    }
               numcharChange(numORchar);      
               }    
    }
    else if (adminChoice == 3) //Save Settings
    {
         clear();
         defLife = life;
         defNumORchar = numORchar; //new default
         journyMenu(life, numORchar); //new default
    }
    else if (adminChoice == 4) //Exit w/o save
    {
         clear();
         journyMenu(defLife, defNumORchar); 
    }

    clear();
    if (life == defLife && numORchar == defNumORchar){
    journyMenu(life, numORchar); }
    else {
    admin(life, numORchar); }

}


void adminMenu()
{        
    cout << "********************************************************************************";
    cout << "*                  PERSONAL TUTOR - ADMINISTRATOR PANEL                        *";
    cout << "********************************************************************************";
    cout << "* Welcome to the Administrator Section of the program. Please choose from the  *";
    cout << "* following options:                                                           *";
    cout << "*                                                                              *";
    cout << "* 1. Change number of lives (default = 10)                                     *";
    cout << "* 2. Change default input values                                               *";
    cout << "* 3. Change current user information                                                                              *";
    cout << "*                                                                              *";
    cout << "* 4. Save                                                                      *";
    cout << "* 5. Exit without saving                                                       *";
    cout << "********************************************************************************" << endl << endl;
}

int numcharChange(bool numORchar)
{
    //Sets pref for menu options
    if (numORchar == 0){
    confer = "[Y/N]"; }
    else if (numORchar == 1){
    confer = "[1 = YES/0 = NO]"; }

    return 0;
}     

void UserName::setFirstName(char fn[26]){
     firstName[26] = fn; }

void UserName::setLastName(char ln[31]){
     lastName[31] = ln[31]; }

char UserName::getName()
{
     fullName[57] = firstName[26] + lastName[31]; 
     return fullName[57];
}


Your problem is here:

void UserName::setFirstName(char fn[26]){
     firstName[26] = fn; 
}

Arrays in C++ cannot be assigned - you need to use strcpy:

void UserName::setFirstName(char fn[]){
     strcpy( firstName, fn ); 
}

Better yet, replace all your arrays with std::string.

Edit: And in your class definition:

void setFirstName(char);

should be:

void setFirstName(char[] );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号