Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having a Problem 1

Status
Not open for further replies.

C11

Programmer
Mar 30, 2001
2
US
I am a beginner in C and am trying to make my C script to work. When I try to compile it I get this error:

70,73: Parse Error, expecting `'}''
'else if (command=="email") printf("\nE-Mail Address: ")'

Could someone please help me? Any answers are greatly appreciated (The code is below.)

------------------------------

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

void main()
{

char name[1000];
char email[1000];
char address[1000];
char phone[1000];
char command[10];

int f;

printf(&quot;name - add the name of new person\n&quot;);
printf(&quot;email - add the email address of the new person\n&quot;);
printf(&quot;address - add the address of the new person\n&quot;);
printf(&quot;phone - add the phone number of the new person\n&quot;);
printf(&quot;create - create the person with all the new properties\n&quot;);
printf(&quot;exit - exit the program\n\n&quot;);

printf(&quot;Command: &quot;);
scanf(&quot;%s&quot;,command);

if (command==&quot;name&quot;)
printf(&quot;\nName: &quot;);
scanf(&quot;%s&quot;,name);

else if (command==&quot;email&quot;)
printf(&quot;\nE-Mail Address: &quot;);
scanf(&quot;%s&quot;,email);

else if (command==&quot;address&quot;)
printf(&quot;\nHome Address: &quot;);
scanf(&quot;%s&quot;,address);

else if (command==&quot;phone&quot;)
print(&quot;\nPhone Number: &quot;);
scanf(&quot;%s&quot;,phone);

else if (command==&quot;create&quot;)
f=fopen(&quot;address_book.dat&quot;,&quot;w&quot;);
fprintf(f,&quot;Name: %s E-Mail Address: %s Home Address: %s Phone Number: %s\n&quot;,name,email,address,phone);
fclose(f);

else if (command==&quot;exit&quot;)
exit();

}

 
Hi,

Some general comments follow as well as the reason you're getting the error.

>I am a beginner in C and am trying to make my C script to work. When I try to compile it I get this error:

>70,73: Parse Error, expecting `'}''
>'else if (command==&quot;email&quot;) printf(&quot;\nE-Mail Address: &quot;)'

>Could someone please help me? Any answers are greatly appreciated (The code is below.)

>------------------------------

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

>void main()
>{

This is an undefined definition of main, use either:

int main(void) or int main(int argc,char **argv)

>char name[1000];
>char email[1000];
>char address[1000];
>char phone[1000];

Wow, expecting a HUGE phone number? ;)

>char command[10];

>int f;

>printf(&quot;name - add the name of new person\n&quot;);
>printf(&quot;email - add the email address of the new person\n&quot;);
>printf(&quot;address - add the address of the new person\n&quot;);
>printf(&quot;phone - add the phone number of the new person\n&quot;);
>printf(&quot;create - create the person with all the new properties\n&quot;);
>printf(&quot;exit - exit the program\n\n&quot;);

>printf(&quot;Command: &quot;);
>scanf(&quot;%s&quot;,command);

scanf() is a tricky function. If you have to use if, at least limit how many characters the user can enter:

scanf(&quot;%9s&quot;,command);

>if (command==&quot;name&quot;)

You can't do this in C, use strcmp() instead:

if (strcmp(command,&quot;name&quot;)==0) {
/* ... */

strcmp() returns 0 if the strings match, > 0 if the 1st string is greater than the 2nd string and < 0 if the 1st string is less thant the 2nd string.

Don't forget to #include <string.h>.

>printf(&quot;\nName: &quot;);

fflush(stdout);

Since streams are line buffered and your printf() contains no newline at the end, you should add this so you can be sure that the user sees your prompt before he/she can enter any input.

>scanf(&quot;%s&quot;,name);

>else if (command==&quot;email&quot;)
>printf(&quot;\nE-Mail Address: &quot;);
>scanf(&quot;%s&quot;,email);

>else if (command==&quot;address&quot;)
>printf(&quot;\nHome Address: &quot;);
>scanf(&quot;%s&quot;,address);

>else if (command==&quot;phone&quot;)
>print(&quot;\nPhone Number: &quot;);
>scanf(&quot;%s&quot;,phone);

>else if (command==&quot;create&quot;)
>f=fopen(&quot;address_book.dat&quot;,&quot;w&quot;);

You should check to make sure that the open was successful before writing to the file:

if (f!=NULL) {

>fprintf(f,&quot;Name: %s E-Mail Address: %s Home Address: %s Phone Number: %s\n&quot;,name,email,address,phone);
>fclose(f);

You also need braces here, as you want to group all these statements together if the user asked to create the address book:

if (strcmp(command,&quot;create&quot;)==0) {
f=fopen(&quot;address_book.dat&quot;,&quot;w&quot;);
if (f!=NULL) {
fprintf(f,
&quot;Name: %s E-Mail Address: %s &quot;
&quot;Home Address: %s Phone Number: %s\n&quot;,
name,email,address,phone);
} else {
/* handle error */
}
}

s\n&quot;,name,email,address,phone);
>else if (command==&quot;exit&quot;)
>exit();

Exit needs an argument. 0, EXIT_SUCCESS and EXIT_FAILURE are portable argument values.

Also, use some logical indentation and braces, it will make your code much easier to read and debug (for you and future code maintainers).

Russ
bobbitts@hotmail.com
 
Ok, I get almost everything execept for the part about int main(void) or whatever. I don't understand why that was wrong.
 
The C standard only defines two definitions for main():

int main(void) or int main(int argc,char **argv)

These are the only definitions that are guaranteed to be portable across all ANSI/ISO compilers. So, if you use a definition like:

void main(void)

This might be a definition supported by your compiler (the standard allows implementations to define alternate definitions for main()), but it may or may not be a supported definition by another compiler that you try to port your code to.

When you use any other definition for main() other than the two above defined by the standard, you're no longer really writing C in the strict sense. You're writing XYZ compiler's non-standard version of C.

Here's the relevant section of C99:

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner.

9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as
char ** argv, and so on.

The very last part where it says &quot;in some other implementation-defined manner&quot; gives compiler writers the freedom to support definitions like void main(void), but since the standard doesn't say anything about this definition, no conforming compiler is obligated to accept a program that uses void main(void).

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top