gpt4 book ai didi

C++ 崩溃(编译器?)

转载 作者:行者123 更新时间:2023-12-02 02:43:15 24 4
gpt4 key购买 nike

我正在启动一个非常简单的程序,但是当我尝试运行它时,它会“具有初始数量”,然后每次都会崩溃。作为上下文,我在 Windows 10 上使用 Visual Studio 2015。

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

/*Ideas for features to add
*----------------------------
*Trade, agriculture, traffic, tourism, housing, crime rate, language, religion
*/

string version = "1.0";
void main() {
cout << "Welcome to Population Simulator v" + version + ".\n";
cout << "In this game we will simulate a city and how the population numbers change over time.\n";
//City initialization
cout << "Please input a name for your city: ";
string name;
cin >> name;
cout << "Please input an initial population of the city (Recommended: 10,000): ";
int population = 0;
cin >> population;
int initialPopulation = population;
int initialWealth = population * 10000;
int wealth = initialWealth;
int year = 2000;
cout << name + " has an initial population of ";
cout << population + " and an initial net-worth of $" + wealth;
cout << ".\n";


}
int weather() {
return 0;
}
int NPG() { //natural population growth
return 0;
}

最佳答案

表达式:

population + " and an initial net-worth of $" + wealth

并没有像你想象的那样做。它对指向 "的 char 指针和 $"的初始净值 执行算术,导致无效指针,当程序尝试使用它进行打印时,该指针会导致崩溃。

尝试:

cout << population << " and an initial net-worth of $" << wealth;

关于C++ 崩溃(编译器?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32000631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com