我編寫了一個堆棧示例,但編譯時顯示錯誤消息。使用C語言編譯堆棧程序時的錯誤消息
在switch case 4中:exit();這條線的問題是:認爲
64 9 C:\Users\pavilion 15\OneDrive\Documents\stack.cpp [Error] return-statement with a value, in function returning 'void' [-fpermissive]
這是編譯的代碼,當我看到錯誤消息。有人可以幫我解決這個問題嗎?
#include<stdio.h>
#include<conio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int);
int pop();
void display();
main(){
int choice,num;
while(1){
printf("Enter your choice\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("Exit\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Enter a number");
scanf("%d", &num);
push(num);
break;
case 2:
num = pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("invalid input");
}
}
}
void push(int element){
if(top == MAX-1){
printf("Stack OverFlow");
return;
}
top = top +1;
stack[top]=element;
}
void pop(){
int element;
if(top == -1){
printf("Stack is empty\n");
return;
}
element = stack[top];
top = top - 1;
printf("%d has been deleted", element);
return element;
}
void display(){
int i;
if(top==-1){
printf("stack is empty!");
return;
}
printf("\n\n");
for(i=top;i>0;i--)
printf("%d\n", stack[i]);
}
你不能從'void'函數返回一個值。 – redFIVE