刚开始学C。
如果我在 Dev-C++ 中编译以下代码,程序运行正常。
如果我在 Xcode 3.2.6 中编译,它看起来像屏幕截图中的那样。
我在 Xcode 中尝试了不同的编译器设置,但行为仍然相同。
对此有什么想法吗?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char artist[30];
char album[30];
int tracks;
char albumsingle;
float price;
printf("Please enter CD informations below. \n \n");
printf("enter cd artist: ");
scanf("%[^\n]", artist);
printf("enter cd title: ");
fflush(stdin);
scanf("%[^\n]", album);
printf("enter no. of tracks: ");
fflush(stdin);
scanf("%d", &tracks);
printf("enter a for album s for single: ");
fflush(stdin);
scanf("%c", &albumsingle);
printf("enter price: ");
fflush(stdin);
scanf("%f", &price);
printf("\n\n\nartist: %s\n", artist);
printf("album: %s\n", album);
printf("track no.: %d\n", tracks);
if (albumsingle == 'a'){
printf("cd type: album\n");
} else {
printf("cd type: single\n");
}
printf("price: %.2f EUR\n\n", price);
system("PAUSE");
return 0;
}
最佳答案
我猜它与 system("PAUSE");
语句有关。 Xcode 在 OSX 上使用,它是 UNIX 的变体,没有命令 pause
。
为什么不让用户手动按下回车键呢?像这样:
printf("Press the ENTER key to continue.\n");
int c;
do
{
c = fgetc(stdin);
} while (c != '\n' && c != EOF);
它具有适用于大多数系统的优势。
关于c - xcode 和 Dev-C++ 的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9080610/