cout表现出了一种不同的行为
cout is showing a differnet kind of behavior
我正在实现区块链的基础知识。但是面对一个奇怪的问题。当我在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<<"….";)
除此之外,您的代码看起来不错,应该可以运行!
相关文章:
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 有没有一种方法可以在编译时获得作用域类名
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- 一种在C++中读取TXT配置文件的简单方法
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 在调用接收数组的方法时,模板化数组大小是不是一种糟糕的做法
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 有没有一种优雅而快速的方法来测试整数中的 1 位是否位于连续区域
- 在运行时检查继承是否只有一种类型和 void*
- 在C++中是否有一种既定的 cout 替代方案,其行为类似于 qDebug?
- cout表现出了一种不同的行为
- std::cout,是二进制的,是一种更快的写输出的方式
- 是否有一种方法来隐藏变量在cout (c++)