Need help with a while satement
Apr 17, 2016 at 10:02pm UTC
When I try to get the "low" to be outputted, it just outputs "0". I am not sure what else to do. Any advice and help is appreciated. Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void CalcAverage(string, float &);
void PromptForFile(string&);
void CalcLow(string, float &);
int main()
{
string dataFileName;
float average;
float low;
PromptForFile(dataFileName);
CalcAverage(dataFileName, average);
CalcLow(dataFileName, low);
}
void PromptForFile(string& fileName)
{
cout << endl << "What's the data filename? " ;
cin >> fileName;
return ;
}
void CalcAverage(string fileName, float & ave)
{
ifstream inData;
float total, value;
int num;
inData.open(fileName.c_str());
total = 0;
num = 0;
inData >> value;
while (inData)
{
num = num + 1;
total = total + value;
inData >> value;
}
ave = total / num;
cout << endl;
cout << fixed << "The average is " << setprecision(2) << ave <<endl;
return ;
}
void CalcLow(string fileName, float & low)
{
ifstream inData;
float total;
float value;
float count;
float ave;
float disLow;
int num;
inData.open(fileName.c_str());
total = 0;
num = 0;
inData >> value;
while (inData)
{
num = num + 1;
total = total + value;
inData >> value;
}
ave = total / num;
disLow = ave - 5;
while (inData)
{
if (value < disLow)
{
low = low + 1;
}
inData >> value;
}
cout << "The distribution is" << endl;
cout << fixed << setw(5) << "Low" ;
cout << fixed << setw(10) << setprecision(0) << low << endl;
return ;
}
Apr 17, 2016 at 10:26pm UTC
At what point in the code do you think you are setting low to something other than zero?
Apr 17, 2016 at 10:28pm UTC
Where I add 1 to low.. I might be completely wrong. What I need low to equal is how many times a number from the data file equals a number that is 5 less than the average.
Apr 18, 2016 at 2:02pm UTC
And how do you know that line 85 is ever being reached? Learn to debug.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
cout << "About to read data from file" << endl;
while (inData)
{
cout << "Top of data loop: inData state is: " ;
if (inData) {cout << "Valid" ;} else {cout << "Invalid" ;}
cout << " Value is now: " << value << " disLow is " << disLow;
if (value < disLow)
{
cout << "Incrementing low, from " << low << " to " ;
low = low + 1;
cout << low << endl;
}
inData >> value;
cout << "Data fetch attempted. inData state: " ;
if (inData) {cout << "Valid" ;} else {cout << "Invalid" ;}
cout << " Value read: " << value;
}
Start thinking about how to test your own code, and then do it.
Last edited on Apr 18, 2016 at 2:05pm UTC
Topic archived. No new replies allowed.