【问题标题】:Password recovery program密码恢复程序
【发布时间】:2015-04-15 23:35:44
【问题描述】:

我应该使用函数和结构来创建一个程序,该程序允许用户输入密码列表和密码提示列表,其中只有一个是正确的。您应该将提示与您输入的可能密码相匹配,并从那里确定正确的密码。这是一个示例运行:
输入:
3
密码
秘密11
qwertyui
输出:
4
*******1
s*********
*e******
*******q
秘密11

这是我目前所拥有的:

#include <stdio.h>
#include <string.h>


struct Option {
    char password[50];
    int matches;
};
// Checks to see if the given password matches the given pattern.
// Returns false (0) if it does not, or true (1) if it does.
int is_match(char password[], char pattern[]){





}
int main(){
    int N,i;
    printf("Enter the N value\n");
    scanf("%d", &N);

    struct Option A[100];
    for(i=0; i<N; i++);{
        scanf("%s", &A[i].password);
    }

    int M;
    scanf("%d", &M);

    //struct Option A[100];
    int j;
    for(j=0; j<M; j++);{
        scanf("%s", &A[i].matches);
    }




    return 0;
}

我的程序在第一个 scanf 处停止,只允许我输入两个密码。这一步不知道怎么调试。

【问题讨论】:

  • 在没有经过严格检查的情况下查看您的程序告诉我,由于您忽略了scanf() 的返回值,因此您不能抱怨错误的结果。
  • 如何不忽略返回值?对不起,我现在在我的编程课上苦苦挣扎
  • 您需要检查,特别是当说明符是 "%d" -> if (scanf("%d", &amp;target) != 1) problem(); else ok(); 但您的问题是 @Ian 解释的分号。
  • 你不需要在你的结构中的某处有一个提示字段吗?匹配字段的用途是什么?
  • 我认为匹配字段是提示字段。如,加星标的单词是我们应该与我们输入的原始单词一起观看的单词

标签: c


【解决方案1】:

删除 for 循环条件后的分号:

   for(i=0; i<N; i++)
   {
        scanf("%s", &A[i].password);
   }

无论如何这都是一个开始。

【讨论】:

  • 谢谢,我犯了一个愚蠢的错误
【解决方案2】:

只需在每个scanf() 之前使用fflush(stdin);

【讨论】:

  • 这不亚于UNDEFINED BEHAVIORfflush() 只为输出流定义,它可能与MSVC 编译器一起使用,但标准没有规定,所以我和其他许多人永远不会这样做。
猜你喜欢
  • 2011-10-04
  • 2011-09-24
  • 2014-12-11
  • 2011-06-13
  • 2010-11-30
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
相关资源
最近更新 更多