c++ - 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();
}
解决方案
我相信问题可能是您的一组注释掉的行的最后一行末尾没有分号!
同样,请注意您在文件顶部使用命名空间std“使用命名空间std”,在编程实践中应该避免这种情况,因为在使用另一个命名空间时会产生冲突(两个命名空间最终可能具有冲突的功能)
相反,只需使用范围运算符来保持您的代码特定,就像您在类中所做的那样:(即 std::cout << "...."; )
除此之外,您的代码看起来不错,应该可以运行!
推荐阅读
- websocket - 在标头中传递 jwtToken 时,Spring WebSocket 无法向经过身份验证的用户发送消息
- c++ - 如何使用 ArduinoJson 从同一个文件中检索多个 NDJSON 对象?
- java - java.lang.NoClassDefFoundError:无法初始化类 com.google.pubsub.v1.ProjectTopicName
- javascript - 如何将对象映射到对象
- java - 静态到非静态重构,不能两者兼得?
- sql-server - SQL Server 忽略凝胶符号
- mysql - 在mysql表中查找下一个/上一个数字
- firebase - Flutter MissingPluginException(在通道 plugins.flutter.io/firebase_admob 上找不到方法 loadInterstitialAd 的实现)
- angular - 服务器以 404 状态响应 - 存储 blob
- html - YouTube 视频意外打开并阻止移动设备