C程序:Do-while循环再次运行程序不能正常工作

时间:2015-11-02 20:23:16

标签: c loops do-while

您好我的程序旨在对文本文件中的记录进行排序,但我的主要问题是获取主要功能,询问用户是否要再次运行程序来读取和写入另一个文件。我的程序现在,当用户输入1再次运行时,跳过第一个问题进入要读取的程序。这是为什么?我很感激帮助!这是我的主要功能:程序仅在第一次运行时编译。

int main()
{
    int    fieldCount = 0;
    int    lineCount = 0;
    char file[STR_LEN];
    char   filepath[STR_LEN];
    char** fields = NULL;
    char** lines = NULL;
    int    recCount = 0;
    Person **sortedRecs = NULL;
    int x;

do {
    printf("Enter path of the file to read: ");
    gets(filepath);
    printf("Enter path to copy to: ");
    gets(file);
    fields = readFieldsDynamic(filepath, &fieldCount);
    lines = readLinesDynamic(filepath, &lineCount);
    recCount = getPersons(fields, fieldCount, &sortedRecs);
    if (recCount && lines && sortedRecs && (recCount <= lineCount)) {
        writeRecsToFile(file, sortedRecs, recCount, lines, lineCount);
        printf("Sorted records are written in %s\n", file);
    }
    if (fields) {
        freePointerToChars(fields, fieldCount);
    }
    if (lines) {
        freePointerToChars(lines, lineCount);
    }
    if (sortedRecs) {
        freeRecs(sortedRecs, recCount);
    }
    printf("Enter 1 to run program again: ");
    scanf("%d%*c", &x);
    } while (x == 1);
    return 0;
 }

3 个答案:

答案 0 :(得分:1)

您需要在%c之前保留一个空格。

<ul class="navbar-nav mr-auto" >
    <li class="nav-item active pl-5">
        <a class="nav-link" href="/">Home
        <span class="sr-only">(current)
        </span>
        </a>
    </li>

    <li class="nav-item active pl-5">
        <a class="nav-link" href="/Home/Dashboard">Dashboard
        </a>
    </li>

    <li class="nav-item active pl-5">
        <a class="nav-link" href="/Visitor/VisitorAnalysis">Visitors Analysis
        </a>
    </li>

    <li class="nav-item dropdown pl-5">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Users
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
            <a class="dropdown-item" href="/Users">Users List
            </a>
            <a class="dropdown-item" href="/Users/Create">User Create
            </a>
        </div>
    </li>
</ul>

<form class="form-inline my-2 my-lg-0 pl-5">
    @if (User.Identity.IsAuthenticated)
    {
        <ul style=" list-style-type: none;">
            <li class="dropdown">
                <a href="#" data-toggle="dropdown" style="text-decoration:none">
                    <span class="float-right">

                        @User.FindFirst("name").Value
                        <img src="~/images/Laura_Bush_portrait.jpg" class=" rounded-circle ml-3" style="width:26px;height:26px;" />

                    </span>

                    <b class="caret">
                    </b>
                </a>
                <ul class="dropdown-menu dropdown-menu-right" style="margin-top: 33%;min-width:7rem; display: none;">
                    <li>
                        <a href="/AzureAD/Account/SignOut" style="margin-left:4%;">Sign out
                        </a>
                    </li>

                </ul>
            </li>
        </ul>
    }

    else
    {
        <div class='quick-area'>
            <div class='pull-right'>
                <ul style=" list-style-type: none;">
                    <li class="nav-item">
                        <a asp-area="AzureAD" asp-controller="Account" asp-action="SignIn" style="color:rgb(22,53,71)">Sign in
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    }
</form>

答案 1 :(得分:0)

你可以做的是添加一个while循环来“消耗”stdin流中剩下的所有新行,以防止下一个getchar不阻止真实用户输入。

while ((ch=getchar()) != EOF && ch != '\n')
    ;

另请注意不要在代码中使用gets。请改为fgets

答案 2 :(得分:0)

此答案基于您的输出,如下所示:

Enter path of the file to read: /my/input/here/
Enter path to copy to: /blah/copy/here/
Enter 1 to run program again: 1
Enter path of the file to read: 
Enter path to copy to: help/I/cant/type/in/the/above/field

我的理解是你的程序可能会在循环之间传递换行符。

我在C ++中遇到过类似的问题,并在输入修复后放置cin.get()。我不确定C等价物。