程序不接受任何输入.第一次输入后,它会自动关闭
The program is not taking any input. After very first input it closes automatically
我正在尝试通过指针访问结构。我认为问题出在第 13 行。谁能告诉我问题到底是什么以及如何解决?
#include <iostream>
struct friends {
std::string name;
std::string lastName;
int age{};
};
int main() {
using namespace std;
int numberOfFriends{0};
cout << "Please enter the number of the friends: ";
cin >> numberOfFriends;
friends *dost[numberOfFriends];
for (int i = 0; i < numberOfFriends; ++i) {
cout << "Please enter the name of " << i + 1 << " friend: ";
cin>>(dost[i]->name);
cout << "Please enter the last name of " << i + 1 << " friend: ";
cin >> dost[i]->lastName;
cout << "Please enter the age of " << i + 1 << " friend: ";
cin >> dost[i]->age;
}
cout << "You entered following data. Please have a look: " << endl;
cout << "****************************************************" << endl;
for (int j = 0; j < numberOfFriends; ++j) {
cout << "Friend :" << j + 1 << endl;
cout << "Name :" << dost[j]->name << endl;
cout << "Last Name :" << dost[j]->name << endl;
cout << "Full Name :" << dost[j]->name << " " << dost[j]->lastName << endl;
cout << "Age :" << dost[j]->age << endl;
cout << "****************************************************" << endl;
}
}
问题确实归结为
friends *dost[numberOfFriends];
您没有为指针分配任何内存,可变长度数组也不是可移植的。
替换是
std::vector<friends> dost(numberOfFriends);
尽管您需要将dost[i]->
替换为dost[i].
尽管我倾向于使用dost.at(i).
,因为这会对索引进行运行时边界检查。
相关文章:
- 为什么线程不接受此输入?
- B不接受8作为输入的是什么?C++
- C++数组输入不接受一定数量的整数
- 斯堪夫不接受输入
- C++字符数组不接受超过 4 个字符的输入
- CIN不接受C++中带有空格的输入?
- std::cin不接受输入
- 为什么我的代码在要求打印长度和宽度的值后不接受多个输入?
- 在此代码中,它不接受 i=0 的输入.它直接用于 i=1
- 有没有一种方法可以接受数字数组而不接受垃圾值的输入
- 扫描线C++不接受输入的功能
- 菜单选择器不接受有效输入
- SCANF 在 Eclipse 中不接受调试模式下的输入?
- #defined 函数只能接受原始输入而不接受变量?
- 我的C++程序不接受菜单后的输入
- 找出一个正方形逻辑中可能的网格数量,但代码不接受超过六个输入,它停止并说.exe停止工作
- C++控制台不接受键盘输入
- 程序不接受任何输入.第一次输入后,它会自动关闭
- 为什么getline不接受输入
- uint8_t不接受两位数的输入