C菜单语法错误 [英] C menu syntax error
问题描述
您好我的c菜单脚本上有语法错误,我无法弄清楚如何解决
错误:
./ menu.c:第2行:意外令牌附近的语法错误`('
'/ menuu.c:第2行:`void菜单(无效);
代码:
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:
#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;
}
我的尝试:
某处告诉我,我可以移除空洞,但这不起作用。
菜单();
顶部();
观察();
ping();
Muilt_ping();
What I have tried:
Somewhere told me i could remove the voids but this didn't work.
Menu();
Top();
Watch();
ping();
Muilt_ping();
推荐答案
使用
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>
然后只剩下一个错误。 wait()
函数需要一个参数。请参阅 wait(2) - Linux手册页 [ ^ ]。您也可以通过在shell中输入相应的 man 命令来显示该页面:
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
你可以删除空白的 一些 :
You can remove some of the voids:
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:
Menu();
没问题。
但是......如果我在这里尝试你的代码,它编译得很好,没有错误。
所以检查你的编译器选项并确保你使用的是C编译器并将其传递给正确的文件!
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!
为了编译它你必须包含正确的头文件,即:stdlib.h
和unistd.h
。
In order to compile it you have to include the proper headers, namely:stdlib.h
andunistd.h
.
这篇关于C菜单语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!