Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:27:21+00:00 2026-06-14T22:27:21+00:00

So, this is probably a bit huge, and probably messy and incorrect (it’s really

  • 0

So, this is probably a bit huge, and probably messy and incorrect (it’s really beginner). I’m just learning C, and part of the work I’m required to do involves switching this program to a C++ program. The main thing I need to do is replace all structs with classes, and have all of the functions used in the code be class functions (members? If I recall…)

I have a pretty good grasp on a lot of basics, but the concept is to just sort of “modify” the code. I don’t see how to “modify” my previous work by just switching to classes. As is, I feel like the program needs to pretty much be re-written to use classes. Maybe I’m missing the simplicity here. I don’t want someone to do the work for me, I just want to know if there’s a simple way to format my structs to work as classes.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h> 

//Struct for friend array pointers.
typedef struct friendstruct{
        char *firstname;
        char *lastname;
        char *homephone;
        char *cellphone;
} frnd;
//Buffer for use in storing in the main program.
typedef struct bufferstruct{
        char firstname[20];
        char lastname[20];
        char homephone[20];
        char cellphone[20];
} frndbuff;
//Add friend function.
void addfriend(frnd friendarray[], frndbuff newfrnd, int count, int opened){
     friendarray[count].firstname = malloc(sizeof(newfrnd.firstname));  //Assign memory before copying string.
     friendarray[count].lastname = malloc(sizeof(newfrnd.lastname));
     friendarray[count].homephone = malloc(sizeof(newfrnd.homephone));
     friendarray[count].cellphone = malloc(sizeof(newfrnd.cellphone));
     strcpy(friendarray[count].firstname, newfrnd.firstname);
     //printf("%s", friendarray[count].firstname);
     //printf("%s", newfrnd.firstname);
     strcpy(friendarray[count].lastname, newfrnd.lastname);
     strcpy(friendarray[count].homephone, newfrnd.homephone);
     strcpy(friendarray[count].cellphone, newfrnd.cellphone);
     //friendarray[count].lastname = newfrnd.lastname;
     //friendarray[count].homephone = newfrnd.homephone;
     //friendarray[count].cellphone = newfrnd.cellphone;

     if(opened==0){
     printf("\nA new friend has been added to the phonebook.");
     }
}
//Deleteing friends.
int deletefriend(frnd friendarray[], frndbuff newfrnd, int count){
     int n = 0;
     int success = 0;
     while(n<count){
           if(strcmp(newfrnd.lastname,friendarray[n].lastname)==0){ //Comparing strings.
                 while(n<count-1){
                                 strcpy(friendarray[n].firstname, friendarray[n+1].firstname);
                                 strcpy(friendarray[n].lastname, friendarray[n+1].lastname);
                                 strcpy(friendarray[n].homephone, friendarray[n+1].homephone);//Removes previously used position.
                                 strcpy(friendarray[n].cellphone, friendarray[n+1].cellphone);
                                 //friendarray[n].lastname = friendarray[n+1].lastname;
                                 //friendarray[n].homephone = friendarray[n+1].homephone;
                                 //friendarray[n].cellphone = friendarray[n+1].cellphone;
                                 n++;
                 }
           success = 1;
           count = count - 1;
           break;
           }
           n++;
     }      
           if(success==1){
           printf("\nThe entry for %s has been removed from the phonebook.", newfrnd.lastname);
           }else{
           printf("\nThat entry was not found");
           }
           //printf("%i", count);
return count;
}
//Show friend by last namme. Identical to delete friend, without removal.
void showfriend(frnd friendarray[], frndbuff newfrnd, int count){
     int n = 0;
     int success = 0;
     while(n<count){
           if(strcmp(newfrnd.lastname, friendarray[n].lastname)==0){
           printf("\n\n%s %s %s (home) %s (cell)\n", friendarray[n].firstname, friendarray[n].lastname, friendarray[n].homephone, friendarray[n].cellphone);                      
           success = 1;
           break;
           }
     n++;
     }

           if(success==0){
           printf("\nThat entry was not found");
           }
}
//DIsplays entire phonebook.
void phonebook(frnd friendarray[], int count){
     int n = 0;
     //printf("%i", count); Used in debugging.
     while(n<count){
     printf("\n%s %s %s (home) %s (cell)\n", friendarray[n].firstname, friendarray[n].lastname, friendarray[n].homephone, friendarray[n].cellphone);
     n++;
     }
}
//Find friend based on last name.
void searchfriend(frnd friendarray[], frndbuff newfrnd, int count){
     int n = 0;
     int success = 0;
     while(n<count){
           if(strcmp(newfrnd.lastname, friendarray[n].lastname)==0){
           printf("\n\n%s %s %s (home) %s (cell)\n", friendarray[n].firstname, friendarray[n].lastname, friendarray[n].homephone, friendarray[n].cellphone); 
           success = 1;                     
           }
     n++;
     }

           if(success==0){
           printf("\nThat entry was not found");
           }
}                        

int main(){
    int option, option2;
    frndbuff currentfriend;
    frnd friendarray[50];
    int count = 0;
    int filecount = 0;
    int opened = 0;
    //Phonebook load previous to main loop.
    printf("\nDo you have a previously saved phonebook you'd like to load?\n1) Yes\n2) No\n");
    printf("\nChoose an option : ");
    scanf("%i", &option2);
        if(option2==1){

                  FILE *fileopen;
                  fileopen = fopen("phonebook.dat", "r"); //File open for reading.
                  if (fileopen != NULL){
                               filecount = 0;
                               opened = 1;
                               printf("\nYour previous phonebook has been loaded : ");
                               while(fscanf(fileopen, "%s %s %s (home) %s (work)\n",&currentfriend.firstname, &currentfriend.lastname, &currentfriend.homephone, &currentfriend.cellphone)==4){
                                                      printf("\n%s %s %s (home) %s (work)\n",currentfriend.firstname, currentfriend.lastname, currentfriend.homephone, currentfriend.cellphone);
                                                      addfriend(friendarray, currentfriend, filecount, opened);
                                                      filecount++;
                               }
                               count = filecount;
                  }else if(fileopen == NULL){
                               printf("\nA previous phonebook could not be found.");
                  }
    }

while(1==1){



    opened = 0;                
    printf("\n\nPhone Book Application\n1) Add Friend\n2) Delete Friend\n3) Show a Friend\n4) Show phone book\n5) Search by last name\n6) Quit\n");    
    printf("\n\nWhat option would you like to choose : ");
    scanf("%i", &option);  
    //Option ensuring.
    if(option<1 || option>6){
                printf("\nYou did not enter a valid option, please try again.");
                option = 6;
    }

    if(option==1){
                  printf("\nFirst Name : ");
                  scanf("%s", &currentfriend.firstname);
                  printf("\nLast Name : ");
                  scanf("%s", &currentfriend.lastname);
                  printf("\nHome Phone : ");
                  scanf("%s", &currentfriend.homephone);
                  printf("\nCell Phone : ");
                  scanf("%s", &currentfriend.cellphone);
                  //printf("%s", currentfriend.firstname); Debugging.
                  addfriend(friendarray, currentfriend, count, opened);


                  count++;
                  //printf("%i", count); Debugging.
                  //All options call the previously made functions and pass the buffer.
    }else if(option==2){
                  printf("\nEnter the last name of the friend you'd like to delete : ");
                  scanf("%s", &currentfriend.lastname);
                  count = deletefriend(friendarray, currentfriend, count);
    }else if(option==3){
                  printf("\nEnter the last name of the friend you'd like to view : ");
                  scanf("%s", &currentfriend.lastname);
                  showfriend(friendarray, currentfriend, count);
    }else if(option==4){
                  phonebook(friendarray, count);
    }else if(option==5){
                  printf("\nEnter the last name you'd like to search : ");
                  scanf("%s", &currentfriend.lastname);
                  searchfriend(friendarray, currentfriend, count);
    }else if(option==6){
                  option2 = 0;
                  printf("\nWould you like to save your phonebook to a file?\n1) Yes\n2) No");
                  printf("\n Choose an option : ");
                  scanf("%i", &option2);
                  if(option2==1){
                                 filecount = 0;
                                 FILE *filesave;
                                 filesave = fopen("phonebook.dat", "w"); //File open for writing.
                                 while(filecount<count){
                                 //File written in the same method it is read.
                                 fprintf(filesave, "%s %s %s (home) %s (work)\n",friendarray[filecount].firstname, friendarray[filecount].lastname, friendarray[filecount].homephone, friendarray[filecount].cellphone);
                                 filecount++;
                                 }
                  }
                  printf("\nThank you for using this Phone Book Application!");
                  break;
    }
}
//Files closed.
fclose(fileopen);
fclose(filesave);
getch();
return 0;
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:27:22+00:00Added an answer on June 14, 2026 at 10:27 pm

    The main difference with C++ is to use std::string instead of the raw character arrays. Then you don’t use malloc and free. You just let std::string take responsibility for the memory management.

    Similarly, you’d use a std::vector for the storage of the friends.

    You could also replace the i/o with C++ iostreams i/o, which is simpler and safer.

    Bjarne Stroustrup wrote a little introductory article showing how to transform a little C program into C++ (“Learning Standard C++ as a New Language”, C/C++ Users Journal pp 43-54 May 1999).

    You will just have to “unlearn” some C ways of doing things, and adopt more C++’ish ways. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a really straightforward answer - but a bit of advice would
I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
So this one is probably very simple, but I'm having a bit of trouble
I'm a bit of a n00b with jquery so this one is probably an
I'm probably over-thinking this/wasting time trying to avoid a bit of conditional code -
I know this probably really simple but Im not sure what im doing wrong...
This is probably a very basic R question...and feel a bit bad about asking...but
This is probably obvious, but I'm a bit of a newbie and have spent
This is probably a really simple question, but I don't write stored procedures often
Probably a bit similar to this: deserializing json with arrays and also I'm following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.