- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个任务,我想知道为什么当我编译并运行这段代码时,scanf("%d", &temp_pin)
没有将数据存储在地址中。我知道这一点,因为在 printf("%d", temp_pin)
的下一条语句中,我只获得了随机变量。我在前面的代码行中有另一个 scanf 语句可以工作,但我很困惑为什么这个语句不起作用。非常感谢!
#include <stdio.h>
int main()
{
//Deceleration of Variables
int option;
int pin [4]={1, 2, 3, 4};
int temp_pin [4];
int new_pin [4];
int temp_new_pin [4];
int correct;
int incorrect;
do
{
printf("Please enter which option you wish to operate\n\n");
printf("1. Enter your pin\n");
printf("2. Change your pin\n");
printf("3. Display the number of times the PIN was entered: \n Successfully\n Unsuccessfully\n");
printf("4. Exit Program\n\n");
scanf("%d", &option);
if (option == 1)
{
printf("\nPlease Enter Your PIN\n");
for (int i = 0; i < 4; i++)
{
scanf(" %d", &temp_pin);
printf(" %d", temp_pin);
if (temp_pin==pin)
{
printf("\nYour PIN is correct\n");
}
if (temp_pin != pin)
{
printf("\nYour PIN is incorrect\n");
}
}//End For
}//End If
} // end do
while(option != 4);
return 0;
}
最佳答案
如果我能提出一个建议,那就是将所有整数数组更改为整数。这将解决您所有的问题。但是,如果您的作业需要使用整数数组,请遵循以下代码。
目前您仅将 temp_pin 的第一个元素设置为等于用户输入。 scanf("%d", &temp_pin);
行实际上将一个整数存储到 temp_pin[0]
中。
由于引脚大小为 4,因此您需要读入 4 个单独的整数以存储在数组中。我建议使用可以设置 temp_pin[0]、temp_pin[1]、temp_pin[2]、temp_pin[3]
从逻辑上讲,我还建议对每个整数进行错误检查,因为整数的大小可以从 -32,768 到 32,767。目前,包含元素 3300,55,12,15
的整数数组将是一个有效的 pin。
您在此行 printf("%d", temp_pin);
中给出的值不是随机变量。这是临时引脚的地址。为了打印存储在 temp_pin 中的值,您需要遍历数组中的每个元素并单独打印它。
对于您的比较语句 if (temp_pin==pin)
,这永远不会导致 true,因为它正在比较地址。同样,正确的解决方法是同时遍历两个列表并比较每个元素。
关于c - Scanf 和地址运算符 (&) 在 for 循环内不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072182/
我的运动需要一些帮助。 Question of exercise 这是我的代码: #include main() { int price,new_price; char code; printf(
这个问题在这里已经有了答案: scanf() leaves the newline character in the buffer (7 个答案) 关闭 5 年前。 我有这段代码(由于逻辑是家庭作业
我这里有一段代码,在输入带空格的字符串时会出现一些不需要的行为。例如 print illegal_argument。当输入包含多个单词时,我希望它只注册第一个单词。 目前发生的是: christian
我的测试输入是 [1,2,3] [4,5,6] [7,8,9] [9,9] 或者它可以是单个 2d 坐标之前的任意数量的 3d 坐标,我的代码是 #include #include #includ
这个问题在这里已经有了答案: What does space in scanf mean? [duplicate] (6 个答案) 关闭 7 年前。 像这样在scanf中加一个空格的目的是什么 sc
#include int main() { int n; scanf("%d", &n); printf(n); return 0; } 这是我的代码,我很愚蠢,可能
程序要求用户输入 2 名学生的姓名、ID 等,将数据写入文件并从文件中读回数据。 #include #include #include #include #include int main(
这个问题在这里已经有了答案: abnormal behavior of scanf [duplicate] (3 个答案) 关闭 8 年前。 今天我遇到了一个问题,当我使用两次接受字符作为输入的 s
我正在用 c 编写一个程序来读取文件的内容。代码如下: #include void main() { char line[90]; while(scanf("%79[^\
main() { int d,a; printf("Enter the digit :"); scanf("%d",&d); printf("Enter another
为什么为 scanf 输入添加多个句点会跳过下一个 scanf 函数。示例输入:b. b. int main (void) { char b[7] = " "; printf("Thi
我想我已经尝试过任何方法(刷新 stdin、scanf 以使用换行符等),但没有任何效果如我所希望的那样。由于某种原因,第三个 scanf 在以下代码中修改了第二个 scanf 中的变量: #incl
这个问题在这里已经有了答案: Scanf skips every other while loop in C (10 个答案) 关闭 6 年前。 我想做一个计算器,只是一个带有循环和基本操作的简单计
如果没有初始化 ("mod=0") ,这段代码进入死循环。我不明白为什么这段代码会循环,即使我使用了 getchar();删除缓冲区。当我先输入“1”,然后再输入“a”时,就会出现无限循环。谁能帮助我
当我为 scanf() 输入一个值时 它只是跳过紧随其后的第二个、第三个和任何其他 scanf()。 这是我的代码: #define _CRT_SECURE_NO_WARNINGS #include
#include #include void sstring(); int main() { char ch1[10],ch2; printf("Ent
这是一个计算房间内人员年龄的简单程序。我正处于初始阶段,现在我看到我不知道哪些变量(我的意思是我在 scanf 之前声明的变量,然后是 scanf 中的占位符)用于 scanf;如何选择和应用正确的变
这个问题在这里已经有了答案: scanf() leaves the newline character in the buffer (7 个答案) 关闭 5 年前。 我知道 scanf() 的用法,
我已经阅读了扫描集的行为。通过研究和测试,我遇到了一个问题。 难道 scanf("%[^\n]") 的行为与 scanf("%s") 一样吗? scanf("%s") 正在消耗字符,直到在 stdin
我遇到了一个问题,当我使用 scanf 将字符串存储到 char 指针中时,我有 3 个输入 - 姓名、姓氏和年龄,姓氏的最后一个 char 值被替换为年龄输出以更好地解释。 Q-riosity v0
我是一名优秀的程序员,十分优秀!