%[^ \ n]转换说明符是否与结构不兼容?

时间:2018-10-23 14:42:14

标签: c

My Code im trying to solve

因此,我试图在结构中使用此说明符,但是当我输入带有空格和点的用户输入时,代码将中断并结束。例如,我试图输入“ LA”。在name.middleInitial中,但是当我这样做时,我的代码一直在中断。

我尝试将Dim ws As Worksheet, lRow As Long Dim x As Long Set ws = ThisWorkbook.ActiveSheet lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row Dim lCol As Long With ws For x = 1 To lRow If .Cells(x, 3).Value = "RRR" Then lCol = Cells(x, Columns.Count).End(xlToLeft).Column 'Find the last column number Range(Cells(x, 6), Cells(x, lCol)).Cut Cells(x, 4) 'Cut from row x and Column F (Column F = 6) to row x and column "lCol". Then paste the range into row x and column 4. End If Next x End With End Sub 与普通字符变量一起使用,它可以工作,但不适用于结构吗?

1 个答案:

答案 0 :(得分:0)

scanf与结构没有特殊关系。
@酷家伙是对的:空格会有所帮助。 拥有代码,而不是代码的图片以及确切的错误说明,将很有帮助。
最简单的复制错误的代码通常是调试的第一步。
fgets()更加有用和安全。
以下基本代码显示scanf与结构一起使用。


#include <stdio.h>
#include <stdlib.h>

struct myinfo
{
    char firstName[20];
    char address[20];
    unsigned int age;
};
typedef struct myinfo Myinfo;
// -----------------------------

void print_myinfo(Myinfo* m);

// -----------------------------

int main()
{
    Myinfo m;

    printf("Enter first name: ");
    scanf(" %[^\n]s", m.firstName);

    printf("Address: ");
    scanf(" %[^\n]s", m.address);

    printf("Age: ");
    scanf("%d", &m.age);

    Myinfo* ptr_myinfo = &m;
    print_myinfo(ptr_myinfo);

    return EXIT_SUCCESS;
}

// -----------------------------
void print_myinfo(Myinfo* m)
{
    printf("-----------------------------------\n");
    printf("First Name: %s\n", m->firstName);
    printf("Address   : %s\n", m->address);
    printf("Age: %d\n", m->age);
}

输出(不需要scanf格式说明符中的's')


    Enter first name: one two
    Address: three four 124
    Age: 65536
    -----------------------------------
    First Name: one two
    Address   : three four 124
    Age: 65536

    Process returned 0 (0x0)   execution time : 13.803 s
    Press any key to continue.