1
私のメニューでプッシュを選択した後、数値の値を入力してもプログラムはうまく動作しますが、何らかの文字値を入力するとプログラムが停止しません。 私は初心者ですので、誰かがこの問題を解決するのに役立つかもしれません。あなたは、文字Cコードで何らかの問題
char a[15];
char value;
に次の変数を変更work.Firstにあなたのコードのための二つのことを変更する必要がプラスあなたも関数にcharaterを渡す必要があります
#include <stdio.h>
#include <curses.h>
int a[15], top = -1;
void push (int value)
{
if (top == 14)
{
printf("Stack is full");
}
else{
top = top + 1;
a[top] = value;
}
}
void pop()
{
if (top == -1)
{
printf("Stack is empty");
}
else
{
top = top - 1;
}
}
void display()
{
int i;
if (top == -1)
{
printf("\n Nothing to display");
}
else
{
printf("\nArray is:\n");
for (i=0; i<=top; i++)
{
printf("%d\n", a[i]);
}
}
}
int main()
{
int choice, value;
do{
printf("\n1.Push :");
printf("\n2.POP :");
printf("\n3.Display :");
printf("\n4.Exit :");
printf("\nEnter your Choice:");
scanf("%d", &choice);
if(choice == 1)
{
printf("\nEnter Value to be inserted: ");
scanf("%d", &value);
push(value);
}
if(choice == 2)
{
pop();
}
if (choice == 3)
{
display();
}
}
while (choice !=4);
getch();
return 0;
}
あなたのpush関数は 'int'値をとりますなぜ私は' char'を押していますか? –
@MiteshPantこの行 'void push(int value)'私はint型からchar型に変わります。この部分では、私は文字と数字を一緒に入力する必要があるので、charが必要ですか? –
あなたは入力を '(char)input'のようにcharにキャストし、charをプッシュ・パラム型として –