user controlled nested loop

I'm having a bit of trouble with this assignment. Basically I'm supposed to create a program that lets the user decide how many days they studied for programming and biology they tell me how many hours and then I'll tell them the average of the hours they studied for each subject. I created an additional nested loop to be able to get both the subjects but it caused the output to be wrong. This is what i have.

for(day = 1; day <= daysWorked; day++)

{

cout << "Please enter the number of hours student " << student << " spent on biology" <<" on day " << day << "." << endl;

cin >> numHours; total = total + numHours;



}
total2= 0;

for(day; day<=daysWorked; day++)
{
cout << "please enter the number of hours student" << student << " spent on programming" << " on day " << day << "." << endl;
cin >> numHours1; total = total + numHours;
}

average = total / daysWorked;

cout << endl;

cout << "The average number of hours per day spent on biology by " << "student " << student << " is " << average << endl << endl << endl;

average1 = total2 / daysWorked;

cout << endl;

cout << "The average number of hours per day spent programming by " << "student " << student << " is " << average1 << endl << endl << endl;
You have two sequential loops, they aren't what I would call nested.

The first loop. Input goes into numHours1, but you add numHours. Has total been initialized to 0 before being used to accumulate a total here?

cin >> numHours1; total = total + numHours;

average = total / daysWorked;
Are these variables defined as int or as float or double? If int, you'll get integer division which might give you an unexpected result.


The second loop - day isn't initialized to a value.
for(day; day<=daysWorked; day++)

Again, input goes into numHours1, but you add numHours. Do you mean to use total or the total2 variable?
cin >> numHours1; total = total + numHours;

Same thing about possible integer division here.
average1 = total2 / daysWorked;
Here is my full code

// This program finds the average time spent programming by a student

// each day over a three day period.



#include <iostream>

using namespace std;

int main()

{

int numStudents, daysWorked;

float numHours, numHours1, total, total2, average, average1;



int student,day = 0; // these are the counters for the loops

cout << "This program will find the average number of hours a day" << " that a student spent programming over a period of time\n\n";

cout << "How many students are there ?" << endl << endl;

cin >> numStudents;

cout<< "How many days have they worked? "<< endl <<endl;

cin >> daysWorked;

for( student = 1; student <= numStudents; student++)

{

total = 0;

for(day = 1; day <= daysWorked; day++)

{

cout << "Please enter the number of hours student " << student << " spent on biology" <<" on day " << day << "." << endl;

cin >> numHours; total = total + numHours;



}
total2= 0;

for(day; day<=daysWorked; day++)
{
cout << "please enter the number of hours student" << student << " spent on programming" << " on day " << day << "." << endl;
cin >> numHours1; total = total + numHours;
}

average = total / daysWorked;

cout << endl;

cout << "The average number of hours per day spent on biology by " << "student " << student << " is " << average << endl << endl << endl;

average1 = total2 / daysWorked;

cout << endl;

cout << "The average number of hours per day spent programming by " << "student " << student << " is " << average1 << endl << endl << endl;


}

return 0;

}
Last edited on
1
2
3
4
5
6
total2= 0;
        for(day; day<=daysWorked; day++)
        {
        cout << "please enter the number of hours student" << student << " spent on programming" << " on day " << day << "." << endl;
        cin >> numHours1; total = total + numHours;
        }


The second loop needs day to start at a valid value. Since you've declared it outside of the for loops, day keeps its value from the previous loop. It should start back at 1 again.

hour and total variables need to match. total vs total2, numhors vs numhours1
Topic archived. No new replies allowed.