我在动态分配结构时遇到问题
I'm having trouble dynamically allocating my struct
我创建了一个简单的程序来帮助我了解如何动态分配结构。我希望程序从用户那里获得5个名称和5个帐户,并显示这些名称和帐户。我知道指针就像一个引用变量,唯一的区别不是传递值,而是传递变量的地址。我为第23行("getline(std::cin,clientPtr[count].name);")、第25行("std::cin.ignore(std:;numeric_limits:max(),'\n');",第27行("std::cin>>clientPtr[count].accounts;")、第40行("td::cout<<名称:"<<clientPtr[count].Name;")、第41行("std::cout<<名称:"<<clientPtr[count]].Name;")和第31行(showInfo(&client);)。当我调试时,它显示第41行没有执行。它应该显示每个客户端的名称和帐户。在这种情况下,情况并非如此。我不知道为什么,只是我的一点背景知识,我是C++的新手,也是使用调试器的新手。我使用的是xcode 8.2,我使用的调试器是lldb。我是来学习的,所以任何事情都会有所帮助。谢谢
#include <iostream>
#include <limits>
struct BankInfo
{
std::string name;
std::string accounts;
};
void showInfo(BankInfo*);
int main()
{
BankInfo client;
BankInfo* clientPtr=nullptr;
clientPtr = new BankInfo[5];
for(int count =0; count < 5; count++)
{
std::cout << "Enter your name:";
getline(std::cin,clientPtr[count].name);
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'n');
std::cout << "Enter you account number:";
std::cin >>clientPtr[count].accounts;
}
showInfo(&client);
return 0;
}
void showInfo(BankInfo* clientPtr)
{
for(int count =5; count < 5; count++)
{
std::cout <<"Name:" << clientPtr[count].name;
std::cout <<"Account:" << clientPtr[count].accounts;
}
}
你把错误的东西交给了showInfo()
。你有两个变量。。单个BankInfo变量和大小为5的动态分配数组。
您希望对后者进行迭代,而不是对前者进行迭代。
将showInfo(&client);
更改为showInfo(clientPtr);
也许可以做到这一点?
所以我修复了我犯了几个错误的解决方案,但感谢您的建议。以下是我所做的。
#include <iostream>
#include <limits>
struct BankInfo
{
std::string name;
std::string accounts;
};
void showInfo(BankInfo*);
int main()
{
BankInfo client;
BankInfo* clientPtr=nullptr;
clientPtr = new BankInfo[5]; //Allocate an array of BankInfo struct on the heap
for(int count =0; count < 5; count++)
{
std::cout << "Enter your name:";
getline(std::cin,clientPtr[count].name); // stores the value in the name member
std::cout << "Enter you account number:";
std::cin >>clientPtr[count].accounts; // stores the value in accounts member
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'n');
}
showInfo(clientPtr);
delete [] clientPtr;
clientPtr = nullptr;
return 0;
}
void showInfo(BankInfo* clientPtr)
{
for(int count =0; count < 5; count++)
{
std::cout <<"nName:" << clientPtr[count].name; // dereference the pointer to the structure
std::cout <<"nAccount:" << clientPtr[count].accounts; // dereference the pointer to the structure
}
}
for(int count=1 ; count<=5 ; count++)
{
//do your stuff here
}
相关文章:
- 编译包含字符串的代码时遇到问题
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 从其他类访问类时遇到问题
- 使用静态成员声明类时遇到问题
- C++ 填充数组时遇到问题
- 在将字符串放入字符串向量时遇到问题?
- 余数除法和不允许除以零 (c++) 时遇到问题
- 我在C++中使用 getline() 函数时遇到问题
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 在我的四叉树实现中遇到问题
- 在 c++ 中循环遍历数组时遇到问题
- 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- 编译 Cilk Plus 程序时遇到问题
- 存储另一个函数返回的布尔数组时遇到问题
- 我在 OpenGL 中显示图像时遇到问题
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 在 for 循环中更新两个变量时遇到问题C++
- 为什么我的程序在打开网络设备时遇到问题
- 在 Tensorflow C++ 中使用 FIFOQueue 时遇到问题
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?