Click here to Skip to main content
14,924,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have a syntax error on my c menu script that i cant figure out how to resolve

Error:
./menu.c: line 2: syntax error near unexpected token `('
'/menu.c: line 2: `void Menu(void);

Code:
C++
#include <stdio.h>
void Menu(void);
void Top(void);
void Watch(void);
void ping(void);
void Muilt_ping(void);
 
int main(int argc, char** argv)
{
    Menu();
    return 0;
}
 
void Menu(void)
{
    int choice;
 
    do
    {
        printf("-------------------Menu--------------------\n");
        printf("|          1. Run Top as child            |\n");
        printf("|          2. Run Watch as child          |\n");
		printf("|          3. Ping Google as child        |\n");
		printf("|    4. Ping both the Google and Bing     |\n");
        printf("|                as child                 |\n");
        printf("|          5. Exit                        |\n");
        printf("-------------------------------------------\n\n");
        scanf("%d",&choice);
 
        switch(choice)
        {
            case 1: Top();
                break;
            case 2: Watch();
                break;
			case 3: ping();
				break;
			case 4: Muilt_ping();
				break;
            case 5: printf("Exiting program!\n");
                exit(0);
                break;
            default: printf("Invalid choice!\n");
                break;
        }
 
    } while (choice != 5);
 
}
 
void Top(void)
{
        int ch;
		int pid= fork() ;
        if(pid!=0) {
                wait();
                printf ( " Parent PID = %d, The PPID = %d, \n ",getpid(),getppid());
                printf( "Child process has finished. \n ");
        }else {
        printf ( " Child PID = %d , The PPID is %d \n",getpid(),getppid());
        sleep(2);
        execl ( "/usr/bin/top",".",(char*)0);
        
 
    /* Flushes input buffer from the newline from scanf() */
    while ( (ch = getchar()) != '\n' && ch != EOF) ;
 
    printf("\n\nPress ENTER to continue.");
    while ( (ch = getchar()) != '\n' && ch != EOF)
        ;
 
    return;
}  
}     

void Watch(void)
{
    int ch;
	int pid= fork() ;
        if(pid!=0) {
                wait();
                printf ( " Parent PID = %d, The PPID = %d, \n ",getpid(),getppid());
                printf( "Child process has finished. \n ");
        }else {
        printf ( " Child PID = %d , The PPID is %d \n",getpid(),getppid());
        sleep(2);
        execl ( "/usr/bin/watch",".",(char*)0);
        
 
    /* Flushes input buffer */
    while ((ch = getchar()) != '\n' && ch != EOF) ;
 
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF);
 
    return;
}
}

void ping(void)
{
	
    int ch;
	int pid= fork() ;
        if(pid!=0) {
                wait();
                printf ( " Parent PID = %d, The PPID = %d, \n ",getpid(),getppid());
                printf( "Child process has finished. \n ");
        }else {
        printf ( " Child PID = %d , The PPID is %d \n",getpid(),getppid());
        sleep(2);
        execl( "/bin/ping", "ping", "-c 5", "www.google.com", (char*)NULL);
		
	return;
}
}
void Muilt_ping(void)
{
		int ch;
		int pid= fork() ;
        if(pid!=0) {
                wait();
                printf ( " Parent PID = %d, The PPID = %d, \n ",getpid(),getppid());
                printf( "Child process has finished. \n ");
				if(fork()) {
					wait();
					printf ( " Parent PID = %d, The PPID = %d, \n ",getpid(),getppid());
					printf( "Child process has finished. \n ");
				}else {
				printf ( " Child PID = %d , The PPID is %d \n",getpid(),getppid());
				sleep(2);
				execl( "/bin/ping", "ping", "-c 5", "www.google.com", (char*)NULL);

		}
		}		
		
		else {
        printf ( " Parent PID = %d, The PPID = %d, \n ",getpid(),getppid());
        sleep(2);
        execl( "/bin/ping", "ping", "-c 5", "www.bing.com", (char*)NULL);
		}return;
		}


What I have tried:

Somewhere told me i could remove the voids but this didn't work.
Menu();
Top();
Watch();
ping();
Muilt_ping();
Posted
Updated 22-Mar-16 0:32am
Comments
Jochen Arndt 22-Mar-16 5:54am
   
I can't see any error there. Which compiler are you using and how it is started?
Member 12267258 22-Mar-16 6:01am
   
Linux based system fixed it now i used gcc to compile it
Jochen Arndt 22-Mar-16 6:09am
   
That is what I have tried with your code (up to main with empty Menu() function):
gcc -Wall -c menu.c

It compiles without any errors or warnings. But there are some with the remaining code.

[EDIT]
There must be some more header files included:

stdlib.h
unistd.h
sys/types.h
sys/wait.h

and the wait() function requires passing a parameter.
[/EDIT]
Member 12267258 22-Mar-16 6:22am
   
Yeah i see that now i was trying to use something in Mobaxterm to compile it but used gcc instead.

Using
gcc -Wall -c menu.c

the error shown in your question does not occur. But there are some other errors and warnings. Most of them can be avoided by including additional header files:
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/wait.h>

Then there is only one error left. The wait() function requires a parameter. See wait(2) - Linux man page[^]. You may also show that page by entering the corresponding man command at the shell:
man 2 wait
   
v2
Comments
Member 12267258 22-Mar-16 6:36am
   
Thank you for this i will amend my code but it looks like it was the compiler in mobaxterm which i think tried to turn it into an exe -_-
Jochen Arndt 22-Mar-16 6:46am
   
I don't know MobaxTerm but it seems to be a SSH/X-Term client to connect and login to a Linux machine. Then it is not related to a compiler. Commands entered in the shell are executed on the Linux system. Then there will be no exe generated. Even if that would be the case, the executable is generated when linking (which is the last build step) but the error occurred during compilation.

Anyway thank you for accepting my answer and fine to hear that compiling is working now.
You can remove some of the voids:
C++
void Menu()
   {
   int choice;
   ...
void Top()
   {
   int ch;
   ...
But you need the one at the start of the declaration.
And you are right not to try and include them when you call the functions:
C++
Menu();
Is fine.
But...If I try your code here, it compiles fine, with no errors.
So check your compiler options and make sure you are using a C compiler and passing it the right file!
   
Comments
Member 12267258 22-Mar-16 6:29am
   
Yeah i see that now i was trying to use something in Mobaxterm to compile it but used gcc instead.
In order to compile it you have to include the proper headers, namely: stdlib.h and unistd.h.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900