C ++ Моя программа дает мне другой результат, чем режим отладки, когда он скомпилирован в исполняемый файл? - PullRequest
0 голосов
/ 10 июля 2019

Это программа, которую я создал, которая позволяет мне вводить данные, такие как Имя, Цвет и возраст, затем она создает текстовый файл со всей введенной мной информацией. Я создал другую функцию, которая позволяет программе просматривать информацию изтекстовый файл и отображать его на консоли, он отлично работает в режиме отладки.Однако, когда я пытаюсь использовать EXE-файл, созданный Visual Studio после компиляции моего кода, я не могу просмотреть информацию и понятия не имею, почему (это не потому, что консоль закрывается быстро), но тот факт, что он не будетвообще отображать информацию на консоли, что странно, потому что он делал именно это в режиме отладки, любая помощь будет принята с благодарностью, спасибо.C ++ не мой родной язык, поэтому я заранее извиняюсь за плохой код.

#include "pch.h"
#include <iostream>
#include<string>
#include<fstream>
#include<sstream>
struct  UserDatabase {
    std::string Name;
    std::string Colour;
    int age;
    UserDatabase() {std::cout << "Database Build 0.040 Alpha" << std::endl;}//Constructor
    ~UserDatabase() { std::cout << "Program has finished running!!" << std::endl; }


    void UserInformation(std::string name, std::string colour, int age) {
        std::cout << "Name: " << name << " Favourite colour: " << colour << " Age: " << age; std::string copyname = name;
        std::cout << " Has been added to the database" << std::endl; std::stringstream sl; sl << name; sl << ".txt"; 
        name = sl.str();
        std::ofstream fl(name);
        std::cout << "\n\n" << std::endl;
        fl << "Name: " << copyname << "\nFavourite Colour: " << colour << "\nAge: " << age << std::endl;
        fl.close();

    }

    void SearchInformation(std::string name) {
        std::stringstream sl; sl << name << ".txt"; name = sl.str();
        std::ifstream file(name); std::string _name;
        if (file) {
            while (std::getline(file, _name)){
                std::cout << _name << std::endl;
            }
            std::cout << "Information has been provided" << std::endl;
            std::cin.get();
        }
        else {
            std::cout << "Sorry, but i could not find the person you were looking for??" << std::endl;

        char Option;
    valid1:
        Option = NULL;

            std::cout << "Would you like to add a new person to the Database?(y)yes (n)no:" << std::endl;
             std::cin >> Option;
            if (Option == 'y' || Option == 'Y') {
                std::cout << "Enter the person's name: " << std::endl; std::string newPerson; std::cin >> newPerson;
                std::cout << "Enter the person's favourite colour: " << std::endl; std::string newColour; std::cin >> newColour;
                std::cout << "Enter the person's age: " << std::endl; int newAge; std::cin >> newAge;
                UserInformation(newPerson,newColour,newAge);


            }
            else if (Option == 'n' || Option == 'N') {
                std::cout << "Okay closing program..." << std::endl;
            }
            else {
                std::cout << "Don't know what you entered try again" << std::endl;
                goto valid1;
            }
        }
    }

    void UseraddPerson() {
        std::cout << "Enter the person's name: " << std::endl; std::string newPerson; std::cin >> newPerson;
        std::cout << "Enter the person's favourite colour: " << std::endl; std::string newColour; std::cin >> newColour;
        std::cout << "Enter the person's age: " << std::endl; int newAge; std::cin >> newAge;
        UserInformation(newPerson, newColour, newAge);
        std::cin.get();

    }

    void UserViewInformation() {
        std::cout << "Enter the person's name: " << std::endl; std::string newPersons; std::cin >> newPersons;
        SearchInformation(newPersons);


    }
    void UserChoice() {
        std::cout << "What would you like to?\nView user-information(v)\nAdd new person(n)" << std::endl; char choice;
        std::cin >> choice;
        if (choice == 'v' || choice == 'V') {
            UserViewInformation();
        }
        else if(choice == 'n' || choice == 'N'){
            UseraddPerson();

        }
    }

}; UserDatabase UD;



int main() {
/*  Devloper tools
    UD.UserInformation("Samantha","Purple",39);
    UD.SearchInformation("Tom");
*/
    UD.UserChoice();//This is to allow the user to freely add new people or view there information through the application


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...