【问题标题】:having trouble using structs and filling their variables with functions (usingpointers)在使用结构和用函数填充变量时遇到问题(使用指针)
【发布时间】:2017-04-09 23:36:02
【问题描述】:

好的,所以我正在尝试做一个艺术画廊管理程序(有点),当将值插入到结构中时,程序崩溃了。任何人都可以发现错误/错误的编码?

这是我的代码(对不起,变量名是葡萄牙语): main.cpp

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include "liga.h"

using namespace std;

bool menu1=false,
     menu2=false,
     menu3=false;

int opc,opc1;
string key="1qaz2wsx",pass;

int main()
{
    OBRAS database;
    OBRAS *ptr;

    ptr=&database;

    do {
        printf("\n\t********************     GESTOR ADAO     ********************\n");
        printf("\t\t   ->            modo admin          - prima 1\n");
        printf("\t\t   ->            modo guest          - prima 2\n");
        printf("\t\t   ->               exit             - prima 3\n");
        cin>>opc;

        if (opc<1 || opc>3)  printf("\nInseriu uma opcao invalida, tente de novo.\n\n");

        if (opc==1) {
            do {
                cin.sync();
                cin.clear();
                system("cls");
                printf("Insira a password:\n");
                cin>>pass;

                if (pass!=key) printf("\nPassword incorreta, tente de novo;\n");

            } while (key!=pass);

            do {
                cin.sync();
                cin.clear();
                system("CLS");
                printf("\n\t********************     GESTOR ADAO     ********************\n");
                printf("\t\t   ->            inserir obra          - prima 1\n");
                printf("\t\t   ->            listar obras          - prima 2\n");
                printf("\t\t   ->                voltar            - prima 3\n");
                cin>>opc1;

                if (opc1<1 || opc1>3)  printf("\nInseriu uma opcao invalida, tente de novo.\n\n");

                if (opc1==3)  menu2=true;

                if (opc==1) {
                    cin.sync();
                    cin.clear();
                    inserir(ptr);
                }
            } while(menu2==false);

        }

        /*else if (opc==2) ->MODO GUEST, sem pass, read only*/

        if (opc==3)  menu1=true;
    } while (menu1==false);

return EXIT_SUCCESS;
}

liga.h

#ifndef LIGA
#define LIGA

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

typedef struct obras {
    char autor[50];
    char nome[50];
    int ano;
    int price;
}OBRAS;

#include "inserir.h"

//#include "main.cpp"
//AQUI SÃO CHAMADOS OS OUTROS HEADERS
/*#include "inserir.h"
#include "lista.h"*/

#endif

inserir.h

void inserir(OBRAS *ptr) {
    std::cin.sync();
    std::cin.clear();
    system("CLS");
    printf("Insira o nome do autor da obra:\n");
    scanf("%[50]", ptr->autor);

    std::cin.sync();
    std::cin.clear();
    printf("Insira o nome da obra:\n");
    scanf("%[50]",ptr->nome);

    std::cin.sync();
    std::cin.clear();
    printf("Insira o ano em que foi feita a obra:\n");
    scanf("%d",ptr->ano);

    std::cin.sync();
    std::cin.clear();
    printf("Insira o preco da obra:\n");
    scanf("%d",ptr->price);
}

【问题讨论】:

  • 你为什么使用char数组而不是std::string,以及printfscanf而不是std::coutstd::cin?您对scanf 的错误使用是第一个问题。
  • 我怎样才能使用 std::cin 并且仍然使用指针发送输入?

标签: c++ function pointers struct


【解决方案1】:

就像 aschepler 在他的评论中提到的那样,将所有 chars 更改为 std::strings 并将所有 scanfs 更改为 std::cins,如下所示:

typedef struct obras {
    std::string autor;
    std::string nome;
    int ano;
    int price;
}OBRAS;

在你的 inserir.h 中:

void inserir(OBRAS *ptr) {
    std::cin.sync();
    std::cin.clear();
    system("CLS");
    std::cout << "Insira o nome do autor da obra:\n";
    std::cin >> ptr->autor;

    std::cin.sync();
    std::cin.clear();
    std::cout << "Insira o nome da obra:\n";
    std::cin >> ptr->nome;

    std::cin.sync();
    std::cin.clear();
    std::cout << "Insira o ano em que foi feita a obra:\n";
    std::cin >> ptr->ano;

    std::cin.sync();
    std::cin.clear();
    std::cout << "Insira o preco da obra:\n";
    std::cin >> ptr->price;
}

在编辑您的帖子时,我(幸运地)看到了一条评论行 #include "main.cpp"。不要那样做,这是非常糟糕的做法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多