code reads outputs results twice for some reason
Apr 28, 2015 at 7:25pm UTC
Hello. I am writing a code for a school project which asks me to read in certain values from 2 text files and then output them to another file called report.txt. Here is a little snippet of my code:
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
int main()
{
ifstream materialProperties;
ifstream testCases;
ofstream report;
materialProperties.open("material_properties.txt" );
testCases.open("test_cases.txt" );
report.open("report.txt" );
// Counter for Test Case
int n = 1;
// Counter for "Acceptable"
int x = 0;
// Use while loop to go through n lines of code
while (not testCases.fail())
{
// Read in test_cases.txt
testCases >> timeTaken;
testCases >> tempDiff;
testCases >> height;
testCases >> thickness;
testCases >> htThreshold;
testCases >> ysThreshold;
testCases >> cThreshold;
testCases >> numMaterials;
report << "Test Case " << n << endl;
report << "Q = " << htThreshold << endl;
report << "S = " << ysThreshold << endl;
report << "C = " << cThreshold << endl;
report << "Processing " << numMaterials << " materials" << endl;
// Series of nested loops going through all possible varitions
.....
else if (numMaterials == 2)
{
testCases >> one;
testCases >> two;
if (one == 1 && (slumpyQ > htThreshold) && (slumpyYS > ysThreshold) && (slumpyCost < cThreshold))
{
report << "Slumpy Wetcrete is acceptable" << endl;
x = x+1;
}
else if (one == 1 && ((slumpyQ < htThreshold) || (slumpyYS < ysThreshold) || (slumpyCost > cThreshold)))
{
report << "Slumpy Wetcrete is unacceptable" << endl;
}
else if (one == 2 && (skyQ > htThreshold) && (skyYS > ysThreshold) && (skyCost < cThreshold))
{
report << "Sky Iron is acceptable" << endl;
x = x+1;
}
else if (one == 2 && ((skyQ < htThreshold) || (skyYS < ysThreshold) || (skyCost > cThreshold)))
{
report << "Sky Iron is unacceptable" << endl;
}
else if (one == 3 && (primalQ > htThreshold) && (primalYS > ysThreshold) && (primalCost < cThreshold))
{
report << "Primal Iron Strongcrete is acceptable" << endl;
x = x+1;
}
else if (one == 3 && ((primalQ < htThreshold) || (primalYS < ysThreshold) || (primalCost > cThreshold)))
{
report << "Primal Iron Strongcrete is unacceptable" << endl;
}
else if (one == 4 && (fluffyQ > htThreshold) && (fluffyYS > ysThreshold) && (fluffyCost < cThreshold))
{
report << "Fluffy Marshmallent is acceptable" << endl;
x = x+1;
}
else if (one == 4 && ((fluffyQ < htThreshold) || (fluffyYS < ysThreshold) || (fluffyCost > cThreshold)))
{
report << "Fluffy Marshmallent is unacceptable" << endl;
}
else if (one == 5 && (wimpyQ > htThreshold) && (wimpyYS > ysThreshold) && (wimpyCost < cThreshold))
{
report << "Wimpy Pig Ironcrete is acceptable" << endl;
x = x+1;
}
else if (one == 5 && ((wimpyQ < htThreshold) || (wimpyYS < ysThreshold) || (wimpyCost > cThreshold)))
{
report << "Wimpy Pig Ironcrete is unacceptable" << endl;
}
else if (one == 6 && (airQ > htThreshold) && (airYS > ysThreshold) && (airCost < cThreshold))
{
report << "Air Entrained Polycarbonate is acceptable" << endl;
x = x+1;
}
else if ( one == 6 && ((airQ < htThreshold) || (airYS < ysThreshold) || (airCost > cThreshold)))
{
report << "Air Entrained Polycarbonate is unacceptable" << endl;
}
else
{
report << "Unknown material is unacceptable" << endl;
}
if (two == 1 && (slumpyQ > htThreshold) && (slumpyYS > ysThreshold) && (slumpyCost < cThreshold))
{
report << "Slumpy Wetcrete is acceptable" << endl;
x = x+1;
}
else if (two == 1 && ((slumpyQ < htThreshold) || (slumpyYS < ysThreshold) || (slumpyCost > cThreshold)))
{
report << "Slumpy Wetcrete is unacceptable" << endl;
}
else if (two == 2 && (skyQ > htThreshold) && (skyYS > ysThreshold) && (skyCost < cThreshold))
{
report << "Sky Iron is acceptable" << endl;
x = x+1;
}
else if (two == 2 && ((skyQ < htThreshold) || (skyYS < ysThreshold) || (skyCost > cThreshold)))
{
report << "Sky Iron is unacceptable" << endl;
}
else if (two == 3 && (primalQ > htThreshold) && (primalYS > ysThreshold) && (primalCost < cThreshold))
{
report << "Primal Iron Strongcrete is acceptable" << endl;
x = x+1;
}
else if (two == 3 && ((primalQ < htThreshold) || (primalYS < ysThreshold) || (primalCost > cThreshold)))
{
report << "Primal Iron Strongcrete is unacceptable" << endl;
}
else if (two == 4 && (fluffyQ > htThreshold) && (fluffyYS > ysThreshold) && (fluffyCost < cThreshold))
{
report << "Fluffy Marshmallent is acceptable" << endl;
x = x+1;
}
else if (two == 4 && ((fluffyQ < htThreshold) || (fluffyYS < ysThreshold) || (fluffyCost > cThreshold)))
{
report << "Fluffy Marshmallent is unacceptable" << endl;
}
else if (two == 5 && (wimpyQ > htThreshold) && (wimpyYS > ysThreshold) && (wimpyCost < cThreshold))
{
report << "Wimpy Pig Ironcrete is acceptable" << endl;
x = x+1;
}
else if (two == 5 && ((wimpyQ < htThreshold) || (wimpyYS < ysThreshold) || (wimpyCost > cThreshold)))
{
report << "Wimpy Pig Ironcrete is unacceptable" << endl;
}
else if (two == 6 && (airQ > htThreshold) && (airYS > ysThreshold) && (airCost < cThreshold))
{
report << "Air Entrained Polycarbonate is acceptable" << endl;
x = x+1;
}
else if (two == 6 && ((airQ < htThreshold) || (airYS < ysThreshold) || (airCost > cThreshold)))
{
report << "Air Entrained Polycarbonate is unacceptable" << endl;
}
else
{
report << "Unknown material is unacceptable" << endl;
}
report << x << " of 2 were acceptable" ;
report << endl;
}
n = n+1;
}
}
For some reason, when I go to report, the results are inputted twice for some reason. The report.txt looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Test Case 1
Q = 90000
S = 800
C = 600000
Processing 2 materials
Wimpy Pig Ironcrete is unacceptable
Air Entrained Polycarbonate is unacceptable
0 of 2 were acceptable
Test Case 2
Q = 90000
S = 800
C = 600000
Processing 2 materials
Wimpy Pig Ironcrete is unacceptable
Air Entrained Polycarbonate is unacceptable
0 of 2 were acceptable
Why are the results inputted twice and how do I fix it? Any help is appreciated
Apr 29, 2015 at 4:20pm UTC
The problem is probably being caused by this line:
while (not testCases.fail())
The stream won't fail until you actually read the file, which would happen right after you do this test. You really should either use the read operation it's self or pre-read the file prior to the loop, and move the reading that is inside the loop to the end of the loop.
1 2 3
while (testCases >> timeTaken >> tempDiff >> height >> thickness >> htThreshold
>> ysThreshold >> cThreshold >> numMaterials)
{
Topic archived. No new replies allowed.