cout表现出了一种不同的行为

cout is showing a differnet kind of behavior

本文关键字:一种 cout      更新时间:2023-10-16

我正在实现区块链的基础知识。但是面对一个奇怪的问题。当我在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"using namespace std",在编程实践中应该避免这样做,因为使用另一个命名空间时可能会产生冲突(两个命名空间最终可能具有冲突的函数)

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

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

相关文章: