【问题标题】:re-prompt user after invalid input in c在 c 中输入无效后重新提示用户
【发布时间】:2017-07-29 14:44:01
【问题描述】:

我正在用 c 语言编写这个程序,我需要在输入无效后重新提示用户。我找到了一个解决方案,只是发现如果用户在重新提示后输入另一个无效输入,那么它会继续。有人可以告诉我一个更好的解决方案吗?我会告诉你我有什么:

#include <stdio.h>
#include <ctype.h>

main()
{
    int ctr; // loop counter
    int custID[10] = {1, 3, 5, 9, 10, // ID array
                      6, 4, 7, 8, 2};
    double custBal[10] = {153.56, 1300.45, 684.45, 990.45, 45.54, // Balance array
                         1100.34, 594.45, 1340.45, 1000.00, 1134.00};
    int IDsearch; // For interaction
    int found = 0; // Search criteria
    int inner, outer, tempID, didSwap; // For sorting the arrays
    double tempBal;
    char ans;

    /* Firs step: Sort the arrays for efficiency */
    for(outer = 0; outer < 9; outer++) // <9 and not <10, because 9 numbers will be bubble sorted
    {                                  // the highest (10th) will  remain at the bottom
        didSwap = 0;                   // Turns one after the arrays sort
        for(inner = outer; inner < 10; inner++)
        {
            if(custID[inner] < custID[outer]) // Ascending sort
            {
                tempID = custID[inner];       // Must include both,
                tempBal = custBal[inner];     // otherwise the arrays wont be linked
                custID[inner] = custID[outer];
                custBal[inner] = custBal[outer];
                custID[outer] = tempID;
                custBal[outer] = tempBal;
                didSwap = 1;                 // Flag that a swap took place
            }
        }
        if(didSwap == 0)
        {
            break;
        }
    }
    /* Second step: Interacting with the program */
    printf("***Customer Balance Search***\n");
    do
    {
        printf("Which ID number do you want to check?\n");
        scanf(" %d", &IDsearch);

        for(ctr = 0; ctr < 10; ctr++)
        {
            if(IDsearch == custID[ctr])
            {
                found = 1;
                break;
            }
        }
        if(found)
        {
            if(custBal[ctr] < 1000)
            {
                printf("\nCustomer #%d has a balance of $%.2f.\n", custID[ctr], custBal[ctr]);
                printf("Credit is good!\n");
            }
            else
            {
                printf("\nCustomer #%d has a balance of %.2f.\n", custID[ctr], custBal[ctr]);
                printf("Credit is bad! No more credit!\n");
            }
        }
        else
        {
            printf("\nCustomer #%d was not found!\n", IDsearch);
            printf("Please enter a correct ID number!\n\n");
            continue;
        }
        printf("\nDo you want to search another ID number?\n");
        printf("Enter (Y)es or (N)o\n");
        scanf(" %c", &ans);
        ans = toupper(ans);
    }
    while((found != 1) || (ans == 'Y' && ans != 'N'));
    printf("\nExiting...\n\n");

    return (0);
}

【问题讨论】:

  • 格式化好吗?
  • @SouravGhosh 我是编程新手,也是stackoverflow 新手。你需要什么,请原谅我的无知。
  • 对。这次@WeatherVane 为您完成了这项工作,但请参阅How to Ask 页面以获取更多指导。谢谢。
  • 您可以通过选择所有代码并单击{}按钮来格式化问题的代码。
  • 请在do-while循环的开头设置found = 0;。在定义时初始化它是不够的。抱歉,我没有将此作为答案发布,因为这需要我彻底测试代码,我不会这样做。

标签: c arrays for-loop while-loop


【解决方案1】:

请重置found

do {
    found = 0; 
    // ...

在 do-while 循环的开始。定义时初始化是不够的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2016-03-08
    • 2022-01-21
    相关资源
    最近更新 更多