C anf查看stdin缓冲区,并查看4。它查看下一个字符,即换行符,并发现它不是数字的一部分(如%d所指定),因此它完成了格式字符串并将文件指针保留在换行符上。它将字符“4”转换为整数4,并将其置于trip_num中并返回
下次您调用scanf时,它会在换行时选择它停止的位置。这次的格式字符串是“%c”,因此它只从缓冲区中获取下一个字符,即当前换行符(“\n”),将其放入C anf查看stdin缓冲区,并查看4。它查看下一个字符,即换行符,并发现它不是数字的一部分(如%d所指定),因此它完成了格式字符串并将文件指针保留在换行符上。它将字符“4”转换为整数4,并将其置于trip_num中并返回,c,C,下次您调用scanf时,它会在换行时选择它停止的位置。这次的格式字符串是“%c”,因此它只从缓冲区中获取下一个字符,即当前换行符(“\n”),将其放入dest\u code,然后返回。在这种情况下,如果希望scanf函数跳过空白,则必须在第二个scanf(目标代码)的“%c”格式之前添加空格,以明确说明这一点。然后,scanf将跳过所有空格(包括该换行符),直到遇到它放置在dest\u code中的非空格字符 TL;DR:将第二个scanf调用更改为scanf(“%c”、&dest\u code
dest\u code
,然后返回。在这种情况下,如果希望scanf
函数跳过空白,则必须在第二个scanf
(目标代码)的“%c”格式之前添加空格,以明确说明这一点。然后,scanf
将跳过所有空格(包括该换行符),直到遇到它放置在dest\u code
中的非空格字符
TL;DR:将第二个
scanf
调用更改为scanf(“%c”、&dest\u code)
。并修复其他人指出的其他错误,这样其他错误就不会出现。这可能有帮助,也可能没有帮助,但之前已经说过,您可能需要将getchar()放入while循环。您可能还需要FGET从键盘抓取标准输入
而(1){
这可能有帮助,也可能没有帮助,但前面已经说过,您可能需要将getchar()放入while循环中。您可能还需要fgets从键盘获取stdin 而(1){
在输入字符之前使用“fflush(stdin)”,即在字符的“printf”语句之前。它将清空输入缓冲区,因此您可以扫描所需的字符。或者只需在“%c”命令之前提供一个空格。例如-------scanf(%c),&area_code);------在输入字符之前使用“fflush(stdin)”,即在字符的“printf”语句之前。它将刷新输入缓冲区,因此您可以扫描所需的字符。或者只需在“%c之前提供一个空格命令。如-------scanf(“%c”和区号)---------不是你问题的答案,但当trip_num==trip时,你说它无效,然后接受它。
char area_code,s,M,L,N,p,K,R,C,U,W,O;
我看到了11个你根本没有在代码中使用的变量。我不确定我在哪里接受了。s M L N…是从printf这就是用户在scanf区域代码中要输入的内容,区域代码是我要存储单个字符的地方,您可能需要对其进行格式化。看起来函数体的结尾是for循环的结尾。这真的让我大吃一惊。此外,您正在使用ut初始化它们:check=error\u dest(type\u num,cont\u num,dest\u code,check);
@Thao从scanf(“%d”,&trip\u num)
:用户输入6(trip
),现在trip\u num==trip。如果(trip\u num
为false,程序将打印“无效trips。请输入不超过6次行程。”在while循环的顶部,它检查triptrip\u num>trip
.trip\u num==trip,因此它退出while循环并输入for循环并打印“请输入目标代码:”继续。这不是你问题的答案,但当trip_num==trip时,你说它无效,然后接受它。char area_code,s,M,L,N,P,K,R,C,U,W,O;
我看到了11个你根本没有在代码中使用的变量。我不确定我在哪里接受了它。s M L N…是从printf中选择的变量,这就是e user将在scanf area_code输入,而area_code是我存储单个字符的地方,您可能需要对其进行格式化。函数体的结尾看起来像是for循环的结尾。这真的让我很反感。此外,您正在使用check
和cont_num
而不初始化它们:check=error\u dest(type\u num,cont\u num,dest\u code,check);
@Thao从scanf(“%d”)开始(&trip\u num)
:用户输入6(trip),现在trip\u num==trip。如果(trip\u num
为false,程序将打印“无效trips。请输入不超过6次trips。”在while循环的顶部,它检查trip是否trip\u num>trip
。trip\u num==trip,因此它退出while循环并输入for循环,然后打印“请输入目的地代码:”并继续。我不确定您指的是什么。@Thao Nguyen使用printf(“%c”,区域代码);要查看区域代码是什么,我不确定你指的是什么。@Thao Nguyen使用printf(“%c”,区域代码);要查看区域代码是什么,唯一的问题是我们的教授严格要求不使用她没有介绍的材料/函数,这是我尝试使用scanf()的唯一原因工作。唯一的问题是我们的教授严格要求不使用她没有介绍的材料/功能,这是我试图让scanf()工作的唯一原因。是的,我想我知道你要去哪里了,我想我知道你要去哪里了
/*
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;
}
}
int c = 0;
while (c != '\n' && c != EOF)
{
c = getchar();
}
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);