Logical Errors in Loop Structures

Hi I'm 5 weeks into my first computer science course and I'm having difficulty understanding how the syntax of c++ works. I probably just need a better understanding of fundamental concepts however the campus being shut down because of COVID-19 and a very apathetic Portland Community College instructor have backed me into a corner in terms of sifting through information.

my assignment:
Bianca is preparing special dishes for her daughter’s birthday.
It takes her a minutes to prepare the first dish, and each following dish takes b minutes
longer than the previous dish. She has t minutes to prepare the dishes.
For example, if the first dish takes a = 10 minutes and b = 5, then the second dish will
take 15 minutes, the third dish will take 20 minutes, and so on.
If she has 80 minutes to prepare the dishes, then she can prepare four dishes because 10 +
15 + 20 + 25 = 70.
A data file has been provided with the following data, where a, b, and t are described
above.
a b t



I have thus far been able to write a value of zero into the column I want to place the calculated variable numberOfDishes into

I believe that my control structures are both formatted incorrectly and I also have a logical error that I don't know enough about proper syntax to correct.

A hint about the correct loop-style or
a corrected version of my code would do a lot for me in understanding how this task is achieved per the instructions

Input file (.txt)

10 5 70
5 15 85
12 9 75
10 6 60
20 10 100
15 8 95
4 3 35
20 10 200
9 5 65


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
  #include<iostream>
#include<fstream>
#include <string>
#include <iomanip>
#include<conio.h>
using namespace std;

int main()
{
	string inputFileName; // Declarations
	string outputFileName;
	ifstream inputFile;
	ofstream outputFile;
	int a = 0;
	int b = 0;
	int t = 0;
	int c = a;
	int count = 0;
	int sum = a;
	int numberOfDishes = 0;

	inputFile.open("times.txt"); // open the input file


	//check if the file sucessfully opened
	if (!inputFile.is_open()) {
		cout << "Error. \n file would not open " << endl;
		exit(1);
	}
	// This section echos the input
	while (inputFile.peek() != EOF) {
		inputFile >> a >> b >> t;
		cout << left << setw(3) << a
			<< setw(6) << b << setw(9) << t << "\n" << endl;
	}
	cout << "end of file\n" << endl;
	// clear
	inputFile.clear();
	// restart
	inputFile.seekg(0);

	outputFile.open("Preptime.txt");
	// non-looped output section
	outputFile << setw(10) << "Variable A" << setw(22) << "Variable B "
		<< setw(33) << "Variable t" << setw(40) << "Number of Dishes" << endl;
	//priming statment
	inputFile >> a >> b >> t;
	//begin loop
	while (inputFile.good()) {

		outputFile << setw(3) << a;
		outputFile << setw(26) << b;
		outputFile << setw(36) << t;
		outputFile << setw(40) << numberOfDishes << endl;

		inputFile >> a >> b >> t;

		while (!sum > t) {
			sum = sum + (sum + b);
			count++;
			numberOfDishes = count;
			outputFile << setw(3) << a;
			outputFile << setw(26) << b;
			outputFile << setw(36) << t;
			outputFile << setw(40) << numberOfDishes << endl;
			if (sum > t) {
				sum > t == true;
			}


		}
	} 
cout << count << endl;
> while (!sum > t)
Yeah, be careful of operator precedence here.
!sum by itself will have a value of 0 or 1.
Which you then compare with t.

Maybe
while ( sum <= t )
or
while ( !(sum > t) )


> sum > t == true;
This won't work either.


You also don't need all those .peek or .good calls either.
1
2
3
while ( inputFile >> a >> b >> t ) {
  // do stuff
}

This will read the file, and the loop will exit the first time it fails to successfully extract those three variables.
Topic archived. No new replies allowed.