gpt4 book ai didi

c++ - 我在 C++ 中的搜索功能是提取所有答案,而不是一个单一的

转载 作者:行者123 更新时间:2023-11-28 07:26:18 25 4
gpt4 key购买 nike

我真的很困惑。我必须为一个类做这个实验室,我似乎无法让搜索只显示一个结果,而是显示一年中的所有月份。我似乎也无法弄清楚为什么当我在一年中的月份输入 0 时它不显示 TotalRainfall。谢谢。

#include <iostream>
#include <fstream>

const int MaxSize = 12; //How many weather lines will be available.

using namespace std;

struct WeatherInformation
{
int Month; //Months of the year
float TotalMonthsRainfall; //Total amount of rainfall
float HighTemp; //The Highest temperature of the month.
float LowTemp; //The Lowest temperature of the month.
float AverageTemp; //The Average temperature of the month.
};

WeatherInformation WeatherArray[MaxSize]; //Declaring a month array of MaxSize

void ReadFile(ifstream& MyinFile, WeatherInformation WeatherArray[]);
void WeatherMonthSearch (WeatherInformation WeatherArray[]);


int main()
{
float TotalRainfall = 0;
int count = 1; //Counts how many times the for loop goes.
int MonthOfWeather; //User input of the month.
char ProgramRedo; //User input if they want to reuse the program.
char exit_char; //User input to exit the program.
ifstream MyinFile; //Variable that uses file.

ReadFile (MyinFile, WeatherArray); //Call ReadFile Function
WeatherMonthSearch (WeatherArray); //Call WeatherMonthSearch Function

MyinFile.close(); //Closes file.
}
//Brett Holmes
//4/30/2013
//PreCondition:You need a file labeled weather.dat
//PostCondition: It puts the file variables into an array.
void ReadFile(ifstream& MyinFile, WeatherInformation WeatherArray[])
{
float TotalRainfall = 0;
char exit_char;
int count = 0;
int Month = 0;

cout << "Your Weather Machine" << endl << endl;
MyinFile.open("weather.dat");
if (!MyinFile)
{ //no
cout << "Can't open input file." << endl; //Tests the right file.
char exit_char; //End Program
cout << "Press any key to exit" << endl;
cin >> exit_char;
}
for(count = 1; count < MaxSize; count++) //Puts the file variables in the array.
{
WeatherArray[count].Month = WeatherArray[count].Month + 1;
MyinFile >> WeatherArray[count].TotalMonthsRainfall;
MyinFile >> WeatherArray[count].HighTemp;
MyinFile >> WeatherArray[count].LowTemp;
(WeatherArray[count].AverageTemp = ((WeatherArray[count].HighTemp + WeatherArray[count].LowTemp)/2));
(TotalRainfall = TotalRainfall + WeatherArray[count].TotalMonthsRainfall);
}
}

//Brett Holmes
//4/30/13
//PreCondition:You need to have the months already put into an array in a struct.
//PostCondition:Outputs the rainfall stats the user puts in then asks to run again.
//Outputs a error message if they type in the month wrong.
void WeatherMonthSearch (WeatherInformation WeatherArray[])
{
float TotalRainfall;
int months;
int MonthOfWeather;
char ProgramRedo;
do
{
bool MonthFound = false;

cout << "Please input the number of the Month. Ex. 1=Jan. 2=Feb. etc \n\n";
cin >> MonthOfWeather;

for(int i = 1; i <= MaxSize; i++)
{
months = WeatherArray[i].Month;
if(months == MonthOfWeather ) //Finds the artist and outputs the results
{
cout << "\nTotal Months Rainfall: " << WeatherArray[i].TotalMonthsRainfall << " \n";
cout << "Highest Temperature: " << WeatherArray[i].HighTemp << " \n";
cout << "Lowest Temperature: " << WeatherArray[i].LowTemp << " \n";
cout << "Average Temperature: " << WeatherArray[i].AverageTemp << " \n";
MonthOfWeather = true;
}
}
if(MonthOfWeather == 0)
{
cout << "The total rainfall for the year is: " << TotalRainfall << ".";
}
if(MonthFound == false)
{
cout << "\nMonth Number error. Month not found. Try again.\n\n";
MonthOfWeather = false;
}
cout << "Would you like to look up another month of weather?\n";
cout << "Enter a 'Y' if yes and 'N' if no.\n";
cin >> ProgramRedo;
}while(ProgramRedo == 'Y');
}

最佳答案

几个明显的问题:

  1. C++ 中的数组是从 0 开始的,因此您的 for 循环是差一的。在您的搜索功能中,for(int i = 1; i <= MaxSize; i++)应该是 for(int i = 0; i < MaxSize; i++) .同样,在您的阅读功能中,for(count = 1; count < MaxSize; count++)应该是 for(count = 0; count < MaxSize; count++) (如果您想跳过索引 0,因为您将其用作信号值,那么您应该将 MaxSize 设置为 13,并让循环从 1 开始。)

  2. 为什么要为 MonthOfWeather 分配一个 bool 值?你是说 MonthFound ?

  3. 您阅读的函数未正确设置月份。 WeatherArray[count].Month = WeatherArray[count].Month + 1;应该是 WeatherArray[count].Month = count;如果您使用基于 1 的循环或 WeatherArray[count].Month = count + 1;如果循环是从 0 开始的。

  4. 您在读取函数中计算了总降雨量,但结果存储在局部变量中,因此在读取完成后会丢失。将 TotalRainfall 设为全局变量或在搜索函数中进行计算。

  5. 有很多多余的变量定义:例如,您的天气数据数组是全局的,因此没有理由实际传递它; exit_char在您的读取函数中声明了两次; main() 的前五行声明了您从未使用过的变量。

此外,您的读取函数实际上并没有在失败时退出程序——它甚至仍然尝试从流中读取,然后调用您的搜索函数!如果需要错误检查,您应该让读取函数返回一个 bool 值并在调用搜索函数之前检查读取函数是否成功,或者简单地调用 std::exit之后cin >> exit_char; .

关于c++ - 我在 C++ 中的搜索功能是提取所有答案,而不是一个单一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18708817/

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