创建结构数组,填充结构变量(由用户输入的4个字符串组成),打印这些变量

问题描述 投票:0回答:2

我正在尝试创建一个函数,允许创建一些由 4 个字符串组成的记录(我的数据库项目的一部分)。这个函数必须打印出这些记录 但它不会打印任何内容(没有错误) 以下是文件:main.c仅调用不同的函数,organs.c存储所有函数,organs.h具有结构类型

main.c

#include <stdio.h>
#include "organs.h"

int main(){

    initMenu();
    int chosenOption = chooseOption(); //chosen option

    //working with chosen option
    switch (chosenOption){
        case 1:
            userKeyboardInput();
            break;
            //other cases not done yet
    }

}

器官.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "organs.h"

struct nn records[9];

void initMenu(){
    printf(" 1 - Input\n 2 - Output\n 3 - Add record\n 4 - Search\n 5 - Deleting\n 6 - Sort\n 7 - Exit\n");
}
int chooseOption(){
    printf("Choose option form 1 to 7:\n");
    int chosenOption;
    scanf("%d", &chosenOption);
    return chosenOption;
}

void userKeyboardInput(){
    printf("How many records do yo wish to make?\n");
    int noOfRecords;
    scanf("%d", &noOfRecords);
    int counter = 1;
    for(int x = 0; x < noOfRecords; x++){
        printf("%d record\n", counter);
        struct nn newRecord;
        for(int paramNO = 0; paramNO < 4; paramNO++){
            char symbol;
            char param[20];
            int i = 0;


            while(scanf("%c",&symbol) > 0 && (symbol!='\n' || i < 20)){
                param[i] = symbol;
                i++;
            }
            if(paramNO == 0){
                strcpy(newRecord.param1, param);
            }
            if(paramNO == 1){
                strcpy(newRecord.param2, param);
            }
            if(paramNO == 2){
                strcpy(newRecord.param3, param);
            }
            if(paramNO == 3){
                strcpy(newRecord.param4, param);
            }
        }
        records[x] = newRecord;
    }
    for(int x = 0; x < noOfRecords; x++){
        for(int i = 0; i < 10; i++){
            printf("%c", records[x].param1[i]);
        }
        printf ("  ");

        for(int i = 0; i < 10; i++){
            printf("%c", records[x].param2[i]);
        }
        printf ("  ");

        for(int i = 0; i < 10; i++){
            printf("%c", records[x].param3[i]);
        }
        printf ("  ");

        for(int i = 0; i < 10; i++){
            printf("%c", records[x].param4[i]);
        }
        printf ("  ");
    }
}

器官.h

#ifndef UNTITLED2_ORGANS_H
#define UNTITLED2_ORGANS_H

struct nn{
    char param1[20];
    char param2[20];
    char param3[20];
    char param4[20];
};
void initMenu();
int chooseOption();
void userKeyboardInput();

#endif //UNTITLED2_ORGANS_H
c database struct
2个回答
0
投票
while(scanf("%c",&symbol) > 0 && (symbol!='\n' || i < 20))
{
    param[i] = symbol;
    i++
}

||
逻辑的情况下,如果第一个为真,他不会检查另一个的。

所以停止循环的唯一等待是:

  • 具有 EOF 字符。
  • 有一个 len > 20 的字符串,并且在第 20 个之后有一个
    '\n'
    角色的。

如果我将一个字符串放入 > 20 的字符串中,我就会破坏堆栈,这听起来不太好。


0
投票

如果可以使用数组的数组(param[4][20])代替四个每个 20 个字符的字符串,它会压缩代码。
由于

scanf
用于其他输入,因此将
scanf
与扫描集一起使用,将输入直接输入到结构中。扫描集
" %"FS(LENGTH_STRS)"[^\n]"
将跳过前导空格并扫描最多
LENGTH_STRS
不是换行符的字符。
检查
scanf
的返回,它可以返回 EOF、零或最多为说明符数量的数字(
%n
是一个例外)。

#include <stdio.h>

#define OPTIONS 7
#define RECORDS 9
#define STRS 4
#define LENGTH_STRS 19
// strinify to use in format string
#define FS(x) SFS(x)
#define SFS(x) #x

struct nn {
    char param[STRS][LENGTH_STRS + 1]; // +1 for terminating zero
};

void initMenu ( );
int chooseOption ( );
int userKeyboardInput ( struct nn *record);

int main ( void) {
    struct nn records[RECORDS];
    int chosenOption = 0;

    initMenu();
    if ( -1 == ( chosenOption = chooseOption ( ))) { //chosen option
        fprintf ( stderr, "input problem\n");
        return 1;
    }

    //working with chosen option
    switch ( chosenOption) {
        case 1:
            if ( -1 == userKeyboardInput ( records)) {
                fprintf ( stderr, "input problem\n");
                return 1;
            }
            break;
            //other cases not done yet
    }

}
void initMenu ( ) {
    printf ( " 1 - Input\n 2 - Output\n 3 - Add record\n 4 - Search\n 5 - Deleting\n 6 - Sort\n 7 - Exit\n");
}
int chooseOption ( ) {
    int chosenOption = 0;

    do {
        printf ( "Choose option form 1 to 7:\n");
        if ( 1 != scanf ( "%d", &chosenOption)) {
            return -1;
        }
    } while ( chosenOption < 1 || chosenOption > OPTIONS);
    return chosenOption;
}

int userKeyboardInput ( struct nn *record){
    int noOfRecords = 0;
    do {
        printf ( "How many records do yo wish to make(up to %d)?\n", RECORDS);
        if ( 1 != scanf ( "%d", &noOfRecords)) {
            return -1;
        }
    } while ( noOfRecords < 1 || noOfRecords > RECORDS);
    for ( int x = 0; x < noOfRecords; x++){
        printf ( "%d record\n", x + 1);
        for ( int paramNO = 0; paramNO < STRS; paramNO++){
            if ( 1 != scanf ( " %"FS(LENGTH_STRS)"[^\n]", record[x].param[paramNO])) {
                return -1;
            }
        }
    }
    for ( int x = 0; x < noOfRecords; x++){
        printf ( "\t%d record\n", x + 1);
        for ( int i = 0; i < STRS; i++){
            printf ( "\t%s\n", record[x].param[i]);
        }
    }
    return 1;
}
© www.soinside.com 2019 - 2024. All rights reserved.