/*
Program to calculate trip and plan flights
*/
#define TRIP 6
#define DEST 1
#include <stdio.h>
int error_dest(int type_num, int cont_num, int dest_code, int check);
int main(void)
{
int check, type_num, cont_num, index, i, dest_code, trip_num, row, col;
int travelint[TRIP][DEST], travelarea[TRIP];
char area_code, S, M, L, N, P, K, R, C, U, W, O;
trip_num = 7;
while (trip_num > TRIP)
{
printf("Please enter the number of trips:");
scanf("%d", &trip_num);
if ( trip_num < TRIP)
{
printf("Valid trip number. Please proceed to enter destination code.\n");
}
else
{
printf("Invalid trips. Please enter no more then 6 trips.\n");
}
}
/*********************************************************************************/
for (i=0; i < trip_num ; i++) /*destination code input*/
{
printf("Please enter destination code:");
scanf("%d", &dest_code); /*input of destination code*/
check = error_dest(type_num, cont_num, dest_code, check);
if (check == 2)
{ travelint[i][0]=dest_code; }
else
{
while (check == 1)
{
printf("Please enter destination code:");
scanf("%d", &dest_code); /*input of destination code*/
check = error_dest(type_num, cont_num, dest_code, check);
if (check == 2)
{ travelint[i][0]=dest_code; }
}
}
printf("Please select from the following that best describes your destination:\n");
printf("S Small city - population under 50,000\n");
printf("M Medium city - population between 50,000 and 500,000\n");
printf("L Large city - pop. over 500,000\n");
printf("N Natural formation like a mountain, a lake, a cave, a geyser, a fjord, a canyon, etc.\n");
printf("P Designated park or reserve such as a wildlife refuge, a national park, a bioreserve, or a protected marine area\n");
printf("K Man made landmark like the Great Wall of China, the Taj Mahal, or Stonehenge\n");
printf("R State or province or region of a country\n");
printf("C Whole country\n");
printf("U Multiple countries like traveling through Europe\n");
printf("W Ocean voyage\n");
printf("O Any other type of destination - such as visiting the sites of the seven wonders of the world\n");
printf("Please enter the Area Letter code:");
scanf("%c", &area_code);
}
/*******************************************************************************/
/*print for destination_code*/
for (row = 0; row < trip_num; row++)
{
for (col=0; col < DEST; col++)
printf("Trip[%d] = %d\n", row+1, travelint[row][col]);
}
return 0;
}
error_dest(type_num, cont_num, dest_code, check)
{
cont_num = dest_code / 10000; /*math for error check*/
type_num = dest_code/1000 - cont_num*10;
if ( (cont_num <= 7) && (cont_num > 0) && (type_num <= 5) && (type_num >=0) )
{ /* loop for checking destination code*/
check = 2 ;
return check;
}
else
{
printf("%d is a invalid code\n", dest_code);
check = 1;
return check;
}
}
在scanf上有一些奇怪的原因(“%c”,&amp; area_code);它只是向前运行并打印dest_code数组而不让我输入任何字符,我不确定我到底做错了什么。
答案 0 :(得分:3)
如果您只想抓取一个字符,或许最好使用getchar()而不是scanf()?
答案 1 :(得分:1)
您可以在scanf之后打印area_code
,我想它可能是'\ n',这是您输入的dest_code
行的最后一个字符。
答案 2 :(得分:1)
基本上发生的事情是:您在屏幕上打印“请输入行程次数”消息。用户键入4,然后按Enter键,这意味着stdin
缓冲区如下所示:“4 \ n”。然后使用“%d”格式字符串调用scanf
。 scanf
查看stdin
缓冲区,看到4.它查看下一个字符,即换行符,并且看到它不是数字的一部分(如%d指定),所以它是完成格式字符串并将文件指针保留在换行符处。它将char'4'转换为整数4并将其放在trip_num
中并返回。
下次拨打scanf
时,它会在换行符处停止。这次的格式字符串是“%c”,所以它只是抓取缓冲区中的下一个字符,当前是换行符(“\ n”),将它放在dest_code
中,然后返回。如果您希望scanf
函数在这种情况下跳过空格,则必须通过在第二个scanf
(目标代码)的“%c”格式之前添加空格来明确告诉它。然后scanf
将跳过所有空格(包括该换行符),直到遇到它放在dest_code
中的非空白字符。
TL; DR:将第二次scanf
来电更改为scanf(" %c", &dest_code)
。并修复其他人指出的其他错误,以免其他错误出现。
答案 3 :(得分:0)
在从stdin
:
int c = 0;
while (c != '\n' && c != EOF)
{
c = getchar();
}
然后您可以使用scanf
阅读您的角色,或将其替换为getchar
。
答案 4 :(得分:0)
这可能会有所帮助,也可能没有帮助,但之前声明您可能需要将getchar()放入while循环中。您可能还需要fgets从键盘中获取stdin。
,而(1){
printf("Enter Message Type:");
fflush(stdout) ;
// scan msg.hdr from received message.
scanf("%d", &(msg.m_hdr));
while(getchar() != '\n'){}
printf("Enter your Message:");
fflush(stdout);
// grab data from keyboard
fgets(msg.m_data, sizeof(msg.m_data), stdin);
答案 5 :(得分:0)
在输入字符之前,即在字符的“ printf ”语句之前,使用“ fflush(stdin)”。 它会清除输入缓冲区,因此您可以扫描所需的字符。或者只是在“%c ”命令之前添加空格。喜欢---------- scanf(“%c”,&amp; area_code); ---------------