C++ 程序返回两种不同类型的输出
初学者程序员在这里。下面的代码运行良好;但是,程序可能会输出两种不同类型的值。在某些情况下,程序将输出正确的值。其他时候(大多数情况下),它将输出一个不正确且荒谬的值。例如,hiAvg的正确值应为~89。但是,在随机时间,它将返回6638000 除hiTemp数组外,下面显示的包含整数的所有其他数组都可以正常工作。我花了好几个小时试图弄清楚为什么会发生这种情况,但我想不出任何解决办法C++ 程序返回两种不同类型的输出,c++,struct,dynamic-arrays,C++,Struct,Dynamic Arrays,初学者程序员在这里。下面的代码运行良好;但是,程序可能会输出两种不同类型的值。在某些情况下,程序将输出正确的值。其他时候(大多数情况下),它将输出一个不正确且荒谬的值。例如,hiAvg的正确值应为~89。但是,在随机时间,它将返回6638000 除hiTemp数组外,下面显示的包含整数的所有其他数组都可以正常工作。我花了好几个小时试图弄清楚为什么会发生这种情况,但我想不出任何解决办法 #include <iostream> #include <fstream&
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
//C++ struct to contain 6 values for each city
struct dynArr{
//Contain pointers for to set as dynamic arrays
string *cityName;
int *hiTemp;
int *loTemp;
string *fcastStr;
int *fhiTemp;
int *floTemp;
int size;
};
//Function to read in the file
void openFile ( dynArr & arr1 ){
arr1.size = 10;
//Allocating the dynamic arrays
arr1.cityName = new string[arr1.size];
arr1.hiTemp = new int[arr1.size];
arr1.loTemp = new int[arr1.size];
arr1.fcastStr = new string[arr1.size];
arr1.fhiTemp = new int[arr1.size];
arr1.floTemp = new int[arr1.size];
string city;
int hi, lo;
string fcast;
int fhi, flo;
//Prompt user for a file input
ifstream is;
cout << "Enter name of file: ";
char fname2[80];
cin >> fname2;
is.open (fname2, ios::in);
int i = 0;
is >> city;
while ( ! is.eof() ){
is >> hi >> lo >> fcast >> fhi >> flo;
//Increase size of dynamic arrays if necessary
if ( i >= arr1.size){
string *tempStr1;
tempStr1 = new string[arr1.size*2];
string *tempStr2;
tempStr2 = new string[arr1.size*2];
int *tempInt1;
tempInt1 = new int[arr1.size*2];
int *tempInt2;
tempInt2 = new int[arr1.size*2];
int *tempInt3;
tempInt3 = new int[arr1.size*2];
int *tempInt4;
tempInt4 = new int[arr1.size*2];
for (int a = 0; a < arr1.size; a++){
tempStr1[a] = arr1.cityName[a];
tempStr2[a] = arr1.fcastStr[a];
tempInt1[a] = arr1.hiTemp[a];
tempInt2[a] = arr1.loTemp[a];
tempInt3[a] = arr1.fhiTemp[a];
tempInt4[a] = arr1.floTemp[a];
}
delete[] arr1.cityName;
delete[] arr1.fcastStr;
delete[] arr1.hiTemp;
delete[] arr1.loTemp;
delete[] arr1.fhiTemp;
delete[] arr1.floTemp;
arr1.cityName = tempStr1;
arr1.fcastStr = tempStr2;
arr1.hiTemp = tempInt1;
arr1.loTemp = tempInt2;
arr1.fhiTemp = tempInt3;
arr1.floTemp = tempInt4;
arr1.size = arr1.size*2;
}
//Store the read in values into the dynamic arrays
arr1.cityName[i] = city;
arr1.hiTemp[i] = hi;
arr1.loTemp[i] = lo;
arr1.fcastStr[i] = fcast;
arr1.fhiTemp[i] = fhi;
arr1.floTemp[i] = flo;
i++;
is >> city;
}
}
//Function to calculate average of high and low temperatures
void func1 (dynArr & arr1){
int count = 0;
for (int a = 0; a < 400; a++){
if ( arr1.cityName[a] != "" ){
count = count + 1;
}
}
float hiSum = 0;
float loSum = 0;
for (int i = 0; i < count; i++){
hiSum = hiSum + arr1.hiTemp[i];
loSum = loSum + arr1.loTemp[i];
}
float hiAvg = hiSum/count;
float loAvg = loSum/count;
cout << "The average high temperature for all of the cities: " << hiAvg << endl;
cout << "The average low temperature for all of the cities: " << loAvg << endl;
}
int main() {
dynArr arr1;
openFile(arr1);
func1(arr1);
return 0;
}
#包括
#包括
#包括
使用名称空间std;
//C++结构包含每个城市的6个值
结构动态{
//包含要设置为动态数组的指针
字符串*cityName;
int*hiTemp;
int*loTemp;
字符串*fcastStr;
int*fhp;
int*floTemp;
整数大小;
};
//要在文件中读取的函数
void openFile(dynArr&arr1){
arr1.size=10;
//分配动态数组
arr1.cityName=新字符串[arr1.size];
arr1.hiTemp=新整数[arr1.size];
arr1.loTemp=newint[arr1.size];
arr1.fcastStr=新字符串[arr1.size];
arr1.fhiTemp=新整数[arr1.size];
arr1.floTemp=新整数[arr1.size];
字符串城市;
int hi,lo;
字符串fcast;
佛罗里达国际机场;
//提示用户输入文件
如果流是;
cout>fname2;
is.open(fname2,ios::in);
int i=0;
是>>城市;
而(!is.eof()){
is>>hi>>lo>>fcast>>fhi>>flo;
//如有必要,增加动态数组的大小
如果(i>=arr1.size){
字符串*tempStr1;
tempStr1=新字符串[arr1.size*2];
字符串*tempStr2;
tempStr2=新字符串[arr1.size*2];
int*tempInt1;
tempInt1=新int[arr1.size*2];
int*tempInt2;
tempInt2=新int[arr1.size*2];
int*tempInt3;
tempInt3=新int[arr1.size*2];
int*tempInt4;
tempInt4=新int[arr1.size*2];
对于(int a=0;a>城市;
}
}
//用于计算高温和低温平均值的函数
无效功能1(dynArr&arr1){
整数计数=0;
对于(int a=0;a<400;a++){
if(arr1.cityName[a]!=“”){
计数=计数+1;
}
}
浮丝长度=0;
浮点数loSum=0;
for(int i=0;i 无法停止使用new[]
和delete[]
并改为使用std::vector
。不幸的是,我还不知道如何使用向量。不过,我肯定会很快尝试。但我无法克服好奇,无法理解为什么上面的代码会这样。你的导师需要指导。嗯,这没有帮助,但我看到的最大问题是你的平均计算。for(int a=0;a<400;a++){
Um。您正在使用a
对一个数组进行索引,根据您的输入,该数组不可能有400个元素长,但没有任何东西可以阻止您取消对不存在的节点的引用。该循环到处都是灾难。动态数组在不知道其实际大小的情况下是毫无意义的。而且您的输入循环似乎是m也有相当多的错误。你弄错了,你应该用一个cityName、hiTemp、loTemp等的结构代替多个cityName、hiTemp等的结构,然后用该结构的数组(向量)。你走错了。停止使用new[]
和delete[]
并改为使用std::vector
。不幸的是,我还不知道如何使用向量。不过,我肯定会很快尝试。但我无法克服好奇,无法理解为什么上面的代码会这样。你的导师需要指导。嗯,这没有帮助,但我看到的最大问题是你的平均计算。for(int a=0;a<400;a++){
Um。您正在使用a
对一个数组进行索引,根据您的输入,该数组不可能有400个元素长,但没有任何东西可以阻止您取消对不存在的节点的引用。该循环到处都是灾难。动态数组在不知道其实际大小的情况下是毫无意义的。而且您的输入循环似乎是m如果你弄错了,你应该有一个