• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

question-closed [C++] Co mówi o tym doświadczenie? + cout nie działa :/

Object Storage Arubacloud
0 głosów
998 wizyt
pytanie zadane 1 marca 2018 w C i C++ przez Hiskiel Pasjonat (22,830 p.)
zamknięte 6 marca 2018 przez Hiskiel
Witam! Przychodzę z pytaniem i problemem.

Co doświadczenie mówi o dołączaniu headerów?

Mianowicie mam sobie header Helper.h. On dołącza <windows.h>. W pliku settings.h dołączam "Helper.h" i jednocześnie używam paru obiektów z <windows.h>, jednakże includuje przecież Helper.h który dołącza to windows.h czyli w skrócie, tutaj dołączać to windows.h czy nie? Co mówi o tym praktyka i doświadczenie? Program się odpala, bo przecież helper.h ma w sobie windows.h, ale nie wiem czy takie coś jest "zdrowe".

 

 

A problem mam taki, że cout mi nie działa :/. Po prostu nic nie wypisuje. Nie wiem czy to jest ważne, ale w main.cpp dołączam "Settings.h" i "Helper.h", a "settings.h" również includuje <iostream>. Gdy usunąłem include <iostream> z main.cpp nic się nie stało i dalej cout nic nie wyświetla, ale i tak warto wspomnieć.
komentarz zamknięcia: mam odpowiedź.
komentarz 2 marca 2018 przez Hiskiel Pasjonat (22,830 p.)
komentarz 4 marca 2018 przez Hiskiel Pasjonat (22,830 p.)
Wiadomo już coś?
komentarz 4 marca 2018 przez DragonCoder Nałogowiec (36,500 p.)
Nie mam dostepu
komentarz 6 marca 2018 przez Hiskiel Pasjonat (22,830 p.)

main.cpp:

#include <iostream>
#include <vector>
#include <sstream>
#include "Settings.h"
#include "Helper.h"

#define PVERSION 1.0f  /** PROGRAM VERSION **/

using namespace std;

Settings settings;

bool LoadFromFile(const char* FilePath, OptType type);
bool LoadToFile(const char* FilePath, const string &TextTW); //textToWrite
void CommandInterpreter(const string &Command);
bool FolderExists(const string &Path);

string LoadDifLang(int ID, Lang lang){
    ostringstream TmpSS;
    TmpSS<<ID;A tak na serio. Zbieram materiały o pedofilach na omegle, żeby wreszcie zamknęli te debilną stronę. Jeśli zrobiłem Ci nadzieję sorry. Muszę spadać, cześć. Ciesz się, żę jesteś normalny/a ;)
    string id = TmpSS.str();
    ifstream stream("Text.lang");
    if(!stream){
        color(13);
        cout<<"\tThe file Text.lang is missing!\n\tBrak pliku Text.lang!";
        Sleep(3000);
        color(8);
        system("cmd");
        exit(0);
    }
    string IdFromFile;

    bool Found=true;

    do{
        getline(stream, IdFromFile, ' ');
        Found = !IdFromFile.compare(id);
    }while(!Found&&!stream.eof());

    if(Found==false) { //Found != false it means Found==false
        color(13);
        cout<<"\tThe file Text.lang is missing!\n\tBrak pliku Text.lang!";
        Sleep(3000);
        color(8);
        system("cmd");
        exit(0);
    }
    else{
        string PLText;
        getline(stream, PLText, '/');
        string ENGText;
        getline(stream, ENGText, '"');
        stream.close();
        if(lang==PL) return PLText;
        else return ENGText;
    }
}

bool FileExist(const string &PathToFile){
    ifstream stream(PathToFile.c_str());
    return stream;
}

void Prompt(){ ///NOTE TYMCZASOWY VOID PROMPT
    color(8);
    string prompt = "DisIsMajPrompt> ";
    cout<<prompt;
}

bool RegExists(string key, string subkey){
    HKEY hk = HKEY_CURRENT_USER;

    RegOpenKeyA(hk, key.c_str(), &hk);

    long res = RegQueryValueEx(hk, TEXT(subkey.c_str()), NULL, NULL, NULL, NULL);
    if(res == ERROR_FILE_NOT_FOUND)return false;
    else return true;
}


bool WriteToReg(HKEY HexKey, LPCSTR subkey,LPCSTR KeyName, LPCSTR DataToWrite){
    long OpenKey = RegOpenKeyEx(HexKey, subkey, 0, KEY_ALL_ACCESS, &HexKey);

    if(ERROR_SUCCESS!=OpenKey) return false;
    else return true;

    long CreateKey = RegCreateKeyEx(HexKey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &HexKey, NULL);

    if(ERROR_SUCCESS!=CreateKey) return false;
    else return true;

    long CloseKey = RegCloseKey(HexKey);

    if(ERROR_SUCCESS!=CloseKey) return false;
    else return true;
}

int main(){
    signal(SIGINT, SIG_IGN);
    settings.Setup("C:\\","MyPrompt> ", "Console Title", 12, PL);

    if(!FileExist("Text.lang")){
        color(13);
        cout<<"\tThe file Text.lang is missing!\n\tBrak pliku Text.lang!";
        Sleep(3000);
        color(8);
        system("cmd");
        exit(0);
    }

    if(!FolderExists("C:\\ProgramData\\Hast"))
        CreateDirectory("C:\\ProgramData\\Hast", NULL);

    if(!FileExist("C:\\ProgramData\\Hast\\Hast.exe"))
        CopyFile(GetProgramPath().c_str(), "C:\\ProgramData\\Hast\\Hast.exe", FALSE);

    if(!RegExists("Software\\Microsoft\\Command Processor\\", "AutoRun")){

        system("attrib +H +R C:\\ProgramData\\Hast\\Hast.exe");

        LPCSTR subkey = "Software\\Microsoft\\Command Processor\"";
        LPCSTR data = "C:\\ProgramData\\Hast\\Hast.exe";

        if(!WriteToReg(HKEY_CURRENT_USER, subkey, "AutoRun", data)){
            color(13);
            cout<<"\tBlad! Nie mozna zapisac wartosci do rejestru! \n"
            "\tProgram nie bedzie uruchamial sie z startem konsoli.\n\n";
        }


    }

    string command;
    while(1){
        Prompt();
        getline(cin, command, '\n');
        CommandInterpreter(command);
    }

    return 0;
}

void ShowInfo(){
    color(settings.GetOtherColor());
    cout<<"Wersja programu: "<<PVERSION<<endl;
    cout<<"Autor: Hiskiel"<<endl;
    cout<<"Jezyk: C++"<<endl;
    cout<<"Data ostatecznej wersji: \n"; //TODO DATA OSTATECZNEJ WERSJI
    cout<<"Opis: ";

    cout<<endl;

}


void MultiCommand(){
    cout<<"\nWpisz komendy ktore wykonaja sie po kolei (max 50). \nKiedy skonczysz wpisz \"done\".\n\n";

    vector<string> vect;
    string tmp;
    for(int i=0; i<50; i++){
        cout<<"\t"<<i+1<<": ";
        getline(cin, tmp, '\n');
        if(tmp.compare("done")==0){
            break;
        }
        else if(tmp.compare("multicommand")==0){
            continue;
        }
        vect.push_back(tmp);
    }

    cout<<"\n\n\n";
    vector<string>::iterator EOV = vect.end();
    for(vector<string>::iterator itr = vect.begin(); itr!=EOV ; ++itr){
        color(settings.GetOtherColor());
        CommandInterpreter(*itr);
    }
}


void CommandInterpreter(const string& Command){
    if(Command.substr(0, 4).compare("info")==0)ShowInfo();

    else if(Command.substr(0,12).compare("multicommand")==0){
        MultiCommand();
    }

    else if(Command.substr(0, 4).compare("hide")==0){
        HWND console;
        AllocConsole();
        console = FindWindowA("ConsoleWindowClass",NULL);
        ShowWindow(console, SW_HIDE);

        bool active = true;
        do{
            for(int i=8;i<=255;i++){
                if(GetAsyncKeyState(i)){
                    if(i==38){
                        ShowWindow(console, SW_RESTORE);
                        active=false;
                    }
                }
            }
            Sleep(100);
        }while(active);

    }

    else if(Command.compare("")==0);

    else if(Command.compare("exit")==0)exit(0);

    else if(Command.compare("close")==0){
        color(8);
        system("cmd");
        exit(0);
    }

    else if(Command.substr(0,3).compare("dir")==0){string command = "dir "+settings.GetCDir(); system(command.c_str());}

    else if(Command.substr(0,2).compare("cd")==0){
        string substring = Command.substr(3, Command.length()-3);
        if(!FolderExists(substring)){
            color(13);
            cout<<"\tNie ma takiego folderu!"<<endl;
        }
        else if(!FileExist(substring)){
            color(13);
            cout<<"\tNie ma takiego pliku!"<<endl;
        }
        string Dir = Command.substr(3, Command.length()-3);
        settings.Setup(Dir, settings.GetLTitle(), settings.GetUPrompt(), settings.GetCColor(), settings.GetCLang());
    }

    else{
        system(Command.c_str());
    }
}


bool FolderExists(const string &Path){
    DWORD type = GetFileAttributesA(Path.c_str());

    if(type==INVALID_FILE_ATTRIBUTES) return false;
    else if(type & FILE_ATTRIBUTE_DIRECTORY) return true;
    else return NULL;
}

bool LoadFromFile(const char* FilePath, OptType opt){
    ofstream stream(FilePath);

    for(int i=1;i<=opt;i++){

    }

    stream.close();
}

bool LoadToFile(const char* FilePath, OptType opt){
    ifstream stream(FilePath);
    if(!stream.good()||!stream.is_open()||stream.fail()){
        cout<<"Cannot open the file. The default options will be loaded.";
    }


    stream.close();
}





 

 

Helper.h:

#include <string>
#include <windows.h>

bool IsNumber(const std::string &str);
std::string GetUsername();
std::string GetProgramPath();
void color(int color);
void GXY(int x, int y);
COORD GetCursorPosition();
std::string tolow(const std::string &StringToLowCase);

 

 

 

 

Helper.cpp:

#include "Helper.h"

using namespace std;

bool IsNumber(const string &str){

}

string GetUsername(){
    char UN[257];
    DWORD UNLength = 257;
    GetUserName(UN, &UNLength);

    return UN;
}

string GetProgramPath(){
    char buf[261];
    GetModuleFileName(NULL, buf, 261);
    return string(buf);
}

void color(int color){
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle, color-1);
    CloseHandle(handle);
}


void GXY(int x, int y){
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD cd;
    cd.X=x;
    cd.Y=y;
    SetConsoleCursorPosition(handle, cd);
    CloseHandle(handle);
}

COORD GetCursorPosition(){
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(handle, &csbi);
    CloseHandle(handle);
    return csbi.dwCursorPosition;
}

string tolow(const string &StringToLowCase){
    string tmp = StringToLowCase;

    for(int i=0;i<tmp.length()-1;i++){
        tolower(tmp[i]);
    }

    return tmp;
}

 

 

 

Settings.h:

#include <chrono>
#include <fstream>
#include <iostream>
#include "Helper.h"

enum OptType{cPath=1, titleString=2, prompt=3, otherTColor=4};
enum Lang{PL, ENG};

class Settings {
    public:
        Settings();
        ~Settings();
        void Setup(const std::string &cPath, const std::string &tString, const std::string &prompt, int col, Lang lang);
        void Setup();
        int GetOtherColor();
        std::string GetCDir();
        std::string GetLTitle();
        std::string GetUPrompt();
        int GetCColor();
        Lang GetCLang();
    private:
        std::string MyTime;
        std::string CurrentDir;
        std::string LabelTitle;
        std::string UserPrompt;
        int OtherColor;
        Lang CurrentLang;

        void ShowPrompt(const std::string &str, int type);
        void UpdateTime();
};

 

 

 

Settings.cpp:
 

#include "Settings.h"

using namespace std;

Settings::Settings(){}
Settings::~Settings(){}


void Settings::ShowPrompt(const string &str, int type){
        /** REWRITE THE PROMPT **/
    /*
        Two letters are numbers - color.
        Two letters are !U - Username.
        Two letters are !C - Computer Name.
        Two letters are !P - Current Path.
        Two letters are !D - Current Disk.
        Two letters are !S - Space.
        Two letters are !_ - Pass to a new line.
        Two letters are !\ - Backslash.
        Two letters are !/ - Forward slash.
    */
    int index=0;

    for(int i=0; i<=index; i++){
        string substring = str.substr(0 + index, 2 + index);

    }
}

void Settings::UpdateTime(){
    char buf[10];
    auto t = chrono::system_clock::to_time_t(chrono::system_clock::now());
    strftime( buf, 10, "%H:%M", localtime(&t));
    MyTime = buf;
}

void Settings::Setup(){
    cout<<"Something";
    COORD pos = GetCursorPosition();
}

void Settings::Setup(const string &cPath, const string &tString, const string &prompt, int col, Lang lang){
    if(cPath.empty())CurrentDir="C:\\";
    else CurrentDir=cPath;

    if(tString.empty())LabelTitle="Command Prompt";
    else LabelTitle=tString;

    if(prompt.empty())UserPrompt="Input> ";
    else UserPrompt=prompt;

    OtherColor=col-1;

    switch(lang){
        case PL:
            CurrentLang = PL;
            break;
        case ENG:
            CurrentLang = ENG;
            break;
    }
}

int Settings::GetOtherColor(){return OtherColor;}
string Settings::GetCDir(){return CurrentDir;}
string Settings::GetLTitle(){return LabelTitle;}
string Settings::GetUPrompt(){return UserPrompt;}
Lang Settings::GetCLang(){return CurrentLang;}
int Settings::GetCColor(){return OtherColor;}

 

 

 

komentarz 6 marca 2018 przez Hiskiel Pasjonat (22,830 p.)
Dobra, mam odpowiedź. Podczas zamykania uchwytu HANDLE się dzieją cuda: "Closehandle(handle)" w pliku helper.cpp

3 odpowiedzi

0 głosów
odpowiedź 1 marca 2018 przez Programista 22 Bywalec (2,270 p.)
Gdzie biblioteka iostream albo using namespace std? To jest chyba problem. Bez tego komenda cout nie działa. Bez #include <iostream> nic nie zrobisz z konsolą.
komentarz 1 marca 2018 przez Programista 22 Bywalec (2,270 p.)

Dobra, jeżeli nie dałeś całego kodu to daj cały kod czy potrzebne czy też nie potrzebne

0 głosów
odpowiedź 1 marca 2018 przez Benek Szeryf (90,870 p.)

(...) jednakże includuje przecież Helper.h który dołącza to windows.h czyli w skrócie, tutaj dołączać to windows.h czy nie? Co mówi o tym praktyka i doświadczenie? Program się odpala, bo przecież helper.h ma w sobie windows.h, ale nie wiem czy takie coś jest "zdrowe".

 Poczytaj sobie o dołączaniu bibliotek i dyrektywach #ifndef, #define.

0 głosów
odpowiedź 1 marca 2018 przez j23 Mędrzec (194,920 p.)

jednakże includuje przecież Helper.h który dołącza to windows.h czyli w skrócie, tutaj dołączać to windows.h czy nie?

Jak załączysz nic się nie stanie -> include guards

 

A problem mam taki, że cout mi nie działa :/. Po prostu nic nie wypisuje.

Aplikacja jest typu konsolowego?

Podobne pytania

+1 głos
3 odpowiedzi 448 wizyt
pytanie zadane 10 stycznia 2020 w C i C++ przez amtrax Dyskutant (9,630 p.)
0 głosów
2 odpowiedzi 347 wizyt
pytanie zadane 10 lutego 2016 w C i C++ przez Caishen Nowicjusz (210 p.)
0 głosów
1 odpowiedź 2,589 wizyt
pytanie zadane 25 października 2018 w C i C++ przez XezolPL Obywatel (1,530 p.)

92,551 zapytań

141,393 odpowiedzi

319,523 komentarzy

61,936 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...