Hello!
So i'm working on a program assignment that requires me to figure out how many plants a florist sells in one day based on the temperature outside (I haven't completely finished). My problem is when I run the code it jumps past the while loop and ends the run. The compiler gave me no errors...so i'm guessing there's a logical error somewhere in my code. Can someone help and point out my error? Thanks in advance!
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace std;
void fillArray(ifstream &infile, int myArray1[], int AVAILABLE_PLANTS)
{
// fill array with input file and for loop
infile.open("in.txt");
if (infile.fail()) //if file does not open
{
cout << "input file could not be opened" << endl;
}
//begin filling array with in.txt
for (int i = 0; i < AVAILABLE_PLANTS; i++)
{
infile >> myArray1[i];
}
//spits out array
/* for (int i = 0; i < AVAILABLE_PLANTS; i++)
{
cout << myArray1[i]
<< endl;
}
*/
infile.close(); // close file
};
//create function that finds the approx. sold plants
int approxPlantsSold(int tempOutside, double &plantsSold, double &plantsLeft )
{
int result;
if (tempOutside < 40)
{
// calculate how many plants sold
plantsSold = plantsLeft * .10;
result = plantsLeft - round(plantsSold);
}
return result;
};
int main() {
// declare arrays
int AVAILABLE_PLANTS = 8, OUTDOOR_TEMP = 5;
int myArray1[AVAILABLE_PLANTS];
//int myArray2[OUTDOOR_TEMP];
double plantsLeft = 100;
double plantsSold;
ifstream infile;
fillArray(infile, myArray1, AVAILABLE_PLANTS);
int tempOutside; //variable user inputs
while(tempOutside != 0)
{
cout << "Our plant store is now open for business.\n"
<< "It is the first day and we have " << plantsLeft << " plants available\n"
<< "On the first day of opening, what is the outdoor temperature?\n"
<< "(Press 0 to end game)";
// user enters first outdoor temperature
cin >> tempOutside;
cout << "With the temperature being " << tempOutside << endl
<< plantsSold << " plants were sold.\n"
<< "The number of plants remaining are: ";
// call fxn that calculates amount of plants sold
cout << approxPlantsSold(tempOutside, plantsSold, plantsLeft);
}
return 0;
}
You need to initialize tempOutside to a non-zero value. If you don't initialize it it could have any value, including zero, which would cause your while loop to never execute.