首页 > 解决方案 > cout 表现出不同的行为

问题描述

我正在实施区块链的基础知识。但面临一个奇怪的问题。当我在 main() 函数中注释掉 std::cout 语句后运行我的代码时,它运行良好。但是对于这些 cout 语句,它给出了错误的输出,我真的很困惑它为什么会发生。看看整个文件。https://github.com/mrcreamio/ourcoin

#include <iostream>
#include <string>
#include <time.h>
#include <fstream>
#include "sha256.h"
using namespace std;
string timestamp();

struct Block{
    string data;
    string prev_hashKey;
    string hashKey;
    int BlockNum;
    Block* next;
    Block* prev;
    long long nonce;

};

class chain{
public:
    Block* GenesisBlock = NULL;

    void insert(string data)
    {
        data += timestamp();
        //data+= " ";
        int length = data.length();
        Block* NewBlock = new Block;
        Block* pntr = GenesisBlock;
        Block* previous_ptr = GenesisBlock;

        if (GenesisBlock == NULL) {
            NewBlock->hashKey = sha256(data);
            string temp;
            while (!(NewBlock->hashKey[0] == '0' && NewBlock->hashKey[1] == '0' && NewBlock->hashKey[2] == '0' && NewBlock->hashKey[3] == '0'  && NewBlock->hashKey[4] == '0'))
            {
                temp = data;
                NewBlock->nonce += 1;
                std::string s = std::to_string(NewBlock->nonce);
                temp += s;
                std::cout << "data = "<<temp << '\n';
                std::cout << "nonce = "<< NewBlock->nonce << '\n';
                NewBlock->hashKey = sha256(temp);
            }
            std::cout << "nonce = "<< NewBlock->nonce << '\n';
            NewBlock->prev_hashKey="000000000000000000";
            NewBlock->data = data;
            NewBlock->BlockNum = 0;
            NewBlock->next = NULL;
            GenesisBlock = NewBlock;
        }
        else {
            int counter = 0;
            while(pntr != NULL) {
                previous_ptr = pntr;
                pntr = pntr->next;
                counter++;
            }
            previous_ptr->next = NewBlock;
            NewBlock->hashKey = sha256(data);
            string temp;
            while (!(NewBlock->hashKey[0] == '0' && NewBlock->hashKey[1] == '0' && NewBlock->hashKey[2] == '0' && NewBlock->hashKey[3] == '0'&& NewBlock->hashKey[4] == '0'))
            {
                temp = data;
                NewBlock->nonce += 1;
                std::cout << "nonce = "<< NewBlock->nonce << '\n';
                std::string s = std::to_string(NewBlock->nonce);
                temp += s;
                std::cout << "data = "<<temp << '\n';
                NewBlock->hashKey = sha256(temp);
            }
            std::cout << "nonce = "<< NewBlock->nonce << '\n';
            NewBlock->prev_hashKey = previous_ptr->hashKey;

            NewBlock->prev = previous_ptr;
            NewBlock->data = data;
            NewBlock->BlockNum = counter;
            NewBlock->next = NULL;
        }
    }
void print(){
    Block* curr = GenesisBlock;
    if (curr == NULL)
    {
        cout<<"-------------------------------------"<<endl;
            std::cout << "Nothing to print" << '\n';
        cout<<"-------------------------------------"<<endl;
    }
    else
    {

        cout<<"BlockNum:  "<<curr->BlockNum<<endl;
        cout<<"Data:  "<<curr->data<<endl;
        cout<<"Hash Key:  "<<curr->hashKey<<endl;
        cout<<"Previous Hash Key:  "<<curr->prev_hashKey<<endl;
        cout<<"Nonce :"<<curr->nonce<<endl;
        if (curr->next != NULL) {

        do{
            curr = curr->next;
            cout<<"-------------------------------------"<<endl;
            cout<<"BlockNum:  "<<curr->BlockNum<<endl<<endl;
            cout<<"Data:  "<<curr->data<<endl;
            cout<<"Hash Key:  "<<curr->hashKey<<endl;
            cout<<"Previous Hash Key:  "<<curr->prev_hashKey<<endl;
            cout<<"Nonce :"<<curr->nonce<<endl;
            cout<<"-------------------------------------"<<endl;
        }while(curr->next != NULL);
    }
    }
}

};

string timestamp()
{
time_t now = time(0);
char* dt = ctime(&now);
return dt;
}

int main(){
    chain ch;
    int var,x;
    string data;
    //**** these lines are creating problem****;
    // cout<<"-------------------------------------"<<endl;
    // cout<<"---------WELCOME TO OURCOIN----------" <<endl;
    // cout<<"-------------------------------------"<<endl;

    ch.insert("give me 50 ourcoins ");
    ch.insert("give ahmed 50 ourcoins ");
    ch.print();
}

标签: c++blockchain

解决方案


我相信问题可能是您的一组注释掉的行的最后一行末尾没有分号!

同样,请注意您在文件顶部使用命名空间std“使用命名空间std”,在编程实践中应该避免这种情况,因为在使用另一个命名空间时会产生冲突(两个命名空间最终可能具有冲突的功能)

相反,只需使用范围运算符来保持您的代码特定,就像您在类中所做的那样:(即 std::cout << "...."; )

除此之外,您的代码看起来不错,应该可以运行!


推荐阅读