Note that there are some explanatory texts on larger screens.

plurals
  1. POMy search function in C++ is pulling up all the answers and not a single one
    primarykey
    data
    text
    <p>I'm really confused. I have to make this lab for a class and I can't seem to have the search only display one result but all of the months of the year. I also can't seem to figure out why its not displaying the TotalRainfall when I input 0 into the month of the year. Thank you.</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; 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&amp; 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&amp; MyinFile, WeatherInformation WeatherArray[]) { float TotalRainfall = 0; char exit_char; int count = 0; int Month = 0; cout &lt;&lt; "Your Weather Machine" &lt;&lt; endl &lt;&lt; endl; MyinFile.open("weather.dat"); if (!MyinFile) { //no cout &lt;&lt; "Can't open input file." &lt;&lt; endl; //Tests the right file. char exit_char; //End Program cout &lt;&lt; "Press any key to exit" &lt;&lt; endl; cin &gt;&gt; exit_char; } for(count = 1; count &lt; MaxSize; count++) //Puts the file variables in the array. { WeatherArray[count].Month = WeatherArray[count].Month + 1; MyinFile &gt;&gt; WeatherArray[count].TotalMonthsRainfall; MyinFile &gt;&gt; WeatherArray[count].HighTemp; MyinFile &gt;&gt; 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 &lt;&lt; "Please input the number of the Month. Ex. 1=Jan. 2=Feb. etc \n\n"; cin &gt;&gt; MonthOfWeather; for(int i = 1; i &lt;= MaxSize; i++) { months = WeatherArray[i].Month; if(months == MonthOfWeather ) //Finds the artist and outputs the results { cout &lt;&lt; "\nTotal Months Rainfall: " &lt;&lt; WeatherArray[i].TotalMonthsRainfall &lt;&lt; " \n"; cout &lt;&lt; "Highest Temperature: " &lt;&lt; WeatherArray[i].HighTemp &lt;&lt; " \n"; cout &lt;&lt; "Lowest Temperature: " &lt;&lt; WeatherArray[i].LowTemp &lt;&lt; " \n"; cout &lt;&lt; "Average Temperature: " &lt;&lt; WeatherArray[i].AverageTemp &lt;&lt; " \n"; MonthOfWeather = true; } } if(MonthOfWeather == 0) { cout &lt;&lt; "The total rainfall for the year is: " &lt;&lt; TotalRainfall &lt;&lt; "."; } if(MonthFound == false) { cout &lt;&lt; "\nMonth Number error. Month not found. Try again.\n\n"; MonthOfWeather = false; } cout &lt;&lt; "Would you like to look up another month of weather?\n"; cout &lt;&lt; "Enter a 'Y' if yes and 'N' if no.\n"; cin &gt;&gt; ProgramRedo; }while(ProgramRedo == 'Y'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload