Hey Folks,
I've been into MUDing off and on now for years and I've begun to learn C in my spare time so I can eventually get under the hood and implement some of my own ideas. I've got some good C/C++ resources and I've been doing the exercises. However, I've yet to receive an articulate explanation of the difference between C and C++. I've read that all C code can be compiled in C++ but not vice versa. I also understand that the focus on OOP in C++ is a main difference.
Anyway, I'll get to the point: I've spent most of the day curled up by the fire, reading and doing exercises and I just completed a basic program from a C course that was designed to teach nested structures and passing variables between functions. I was pretty happy when I got it working but then I noticed that my compiler (Dev-C++ 4.9.9.2) uses the .cpp file extension by default when creating a new source file. I had accidentally written a C++ program when I was supposed to be learning C! I then tried to compile the source as a C program and I got a number of compiler warnings and errors. I will post the code below along with the errors in the hope that someone can tell me what's wrong and how to turn this working C++ program into a C program. Cheers.
Code:
#include <stdio.h>
struct address_tree
{
char street[100];
char city[25];
char post [25];
char state [25];
char country[25];
};
struct top_tree
{
char name[100];
char email[50];
char birth[10];
char phone[15];
address_tree add;
};
typedef struct top_tree entry;
entry input ()
{
entry en1;
printf ("Enter a new name.\n\n");
scanf("%[^\n]", en1.name);
fflush(stdin);
printf ("\nEnter a street address.\n\n");
scanf("%[^\n]", en1.add.street);
fflush(stdin);
printf ("\nEnter a city of residence.\n\n");
scanf("%[^\n]", en1.add.city);
fflush(stdin);
printf ("\nEnter a province.\n\n");
scanf("%[^\n]", en1.add.state);
fflush(stdin);
printf ("\nEnter a post code.\n\n");
scanf("%[^\n]", en1.add.post);
fflush(stdin);
printf ("\nEnter a country.\n\n");
scanf("%[^\n]", en1.add.country);
fflush(stdin);
printf ("\nEnter a birthdate in the form dd/mm/yy.\n\n");
scanf("%[^\n]", en1.birth);
fflush(stdin);
printf ("\nEnter a phone number.\n\n");
scanf("%[^\n]", en1.phone);
fflush(stdin);
printf ("\nEnter an email address.\n\n");
scanf("%[^\n]", en1.email);
fflush(stdin);
return en1;
}
void output (entry en1)
{
printf ("\nDatabase Entry\n\n");
printf ("Name: %s\n\n", en1.name);
printf ("Address:\n%s\n%s\n%s\n%s\n%s\n\n",
en1.add.street, en1.add.city, en1.add.state, en1.add.post, en1.add.country);
printf ("Phone: %s\n", en1.phone);
printf ("Email: %s\n", en1.email);
}
main ()
{
entry en1;
en1 = input ();
output (entry en1);
getchar ();
}
When compiled as a .c file this code generated the following errors in Dev-C++4.9.9.2:
\address practice prog.c:24: error: syntax error before "address_tree"
\address practice prog.c:24: warning: no semicolon at end of struct or union
\address practice prog.c:30: error: return type is an incomplete type
\address practice prog.c: In function `input':
\address practice prog.c:31: error: storage size of 'en1' isn't known
\address practice prog.c:61: warning: `return' with a value, in function returning void
\address practice prog.c: At top level:
\address practice prog.c:65: error: parameter `en1' has incomplete type
\address practice prog.c: In function `main':
\address practice prog.c:77: error: storage size of 'en1' isn't known
\address practice prog.c:81: error: syntax error before "entry"
Execution terminated