convert c to c++

I am just wondering how to convert following source code to c++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

struct addr {
  char name[30];
  char street[40];
  char city[20];
  char state[3];
  unsigned long int zip;
} addr_list[MAX];

void init_list(void), enter(void);
void delete(void), list(void);
int menu_select(void), find_free(void);

int main(void)
{
  char choice;

  init_list(); /* initialize the structure array */
  for(;;) {
    choice = menu_select();
    switch(choice) {
      case 1: enter();
        break;
      case 2: delete();
        break;
      case 3: list();
        break;
      case 4: exit(0);
    }
  }

  return 0;
}

/* Initialize the list. */
void init_list(void)
{
  register int t;

  for(t=0; t<MAX; ++t) addr_list[t].name[0] = '\0';
}

/* Get a menu selection. */
int menu_select(void)
{
  char s[80];
  int c;

  printf("1. Enter a name\n");
  printf("2. Delete a name\n");
  printf("3. List the file\n");
  printf("4. Quit\n");
  do {
    printf("\nEnter your choice: ");
    gets(s);
    c = atoi(s);
  } while(c<0 || c>4);
  return c;
}

/* Input addresses into the list. */
void enter(void)
{
  int slot;
  char s[80];

  slot = find_free();

  if(slot==-1) {
    printf("\nList Full");
    return;
  }

  printf("Enter name: ");
  gets(addr_list[slot].name);

  printf("Enter street: ");
  gets(addr_list[slot].street);

  printf("Enter city: ");
  gets(addr_list[slot].city);

  printf("Enter state: ");
  gets(addr_list[slot].state);

  printf("Enter zip: ");
  gets(s);
  addr_list[slot].zip = strtoul(s, '\0', 10);
}

/* Find an unused structure. */
int find_free(void)
{
  register int t;

  for(t=0; addr_list[t].name[0] && t<MAX; ++t) ;

  if(t==MAX) return -1; /* no slots free */
  return t;
}

/* Delete an address. */
void delete(void)
{
  register int slot;
  char s[80];

  printf("enter record #: ");
  gets(s);
  slot = atoi(s);

  if(slot>=0 && slot < MAX)
    addr_list[slot].name[0] = '\0';
}

/* Display the list on the screen. */
void list(void)
{
  register int t;

  for(t=0; t<MAX; ++t) {
    if(addr_list[t].name[0]) {
      printf("%s\n", addr_list[t].name);
      printf("%s\n", addr_list[t].street);
      printf("%s\n", addr_list[t].city);
      printf("%s\n", addr_list[t].state);
      printf("%lu\n\n", addr_list[t].zip);
    }
  }
  printf("\n\n");
}



Line 1 and 2 become #include <iostream> and #include <cstdlib> respectively.

Line 4 becomes const int MAX = 100; (optional... but do it)

The rest are just printf()'s to couts, and gets to cin...
I don't think you wrote this code. If you had written it, you'd know what it did and what the equivalent C++ code would look like.

And converting C to C++ is a little bit more than just changing a few words around.
What difference does it make whether he wrote the code? He just wants to know how to compile it with a C++ compiler.

'delete' is a reserved word in C++, so that function will have to be renamed.
I suppose you're right... I was just being overly suspicious, I guess. I assumed (unfairly) that this was an answer to a homework question that OP found on the internet (the answer, not the question) that was supposed to be submitted as C++ code. But I think I was being overzealous.
Topic archived. No new replies allowed.