# Loop issues and syntax calculation challenges (not with the math)

Page 1 of 1

## 14 Replies - 1018 Views - Last Post: 11 March 2012 - 08:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=270341&amp;s=6cc6d8a9d5d7ac41485d1f8338c3566c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

# Loop issues and syntax calculation challenges (not with the math)

Posted 10 March 2012 - 02:27 PM

```#include<iostream>
using namespace std;

int main()
{
double weight=1.0;

cout << "How many categories in the gradebook? ";

//This loop prints categories in ascending order

//read in the weight of the category
cout << "Enter the weight of Category " <<gradeBook <<" : ";
cin >> weight;

cout << "How many items available in category " << gradeBook <<" ? ";
int items=1, item=1;
cin >> items;

//Nested loop
for (item=1; item<=items; item++){

//The points earned and points possible for each item
cout << "Enter points earned, then points possible for item " << item <<" in category " <<gradeBook <<" : " <<endl;
double points=1.0, pointsPoss=1.0, pointAvg=1.0, points_1=1.0, pointsPoss_1=1.0;
cout << "Earned ";
cin >> points;
cout << "Possible ";
cin >>pointsPoss;
continue;
//compute and display the percentage grade of totalled items

pointAvg=((points_1/pointsPoss_1)*100);
cout << "Your grade for category " <<gradeBook <<" is " <<points <<" out of " <<pointsPoss <<" , or " <<pointAvg <<" % "<<endl;

//This is the weighted grade for each category

//The course grade letter anbd percentage in a large multi-way if-else statement
double pointsTotal=1.0, pointsPossTotal=1.0, pointAvgTotal=1.0;
pointsTotal=points++, pointsPossTotal=pointsPoss++, pointAvgTotal=((pointsTotal/pointsPossTotal)*100);
}
}
return 0;
}

```

I used a continue to get the items to loop for seperate input. I am having difficulties not knowing how to get all the information where it needs to go. All assistance is greatly appreciated. I still need to be able to add the points for each category and points possible for each category to the formula, which is there, to get a grade percentage for each category. Then I need to get the weight of each category and category percentage down to that equation for that weighted grade number. Then I need to be able to bring down each category percentage for a total divided by the number of categories to get that percentage. Then I will be fine to add the large multi-way if-else statement to assign the grade to the total percentage and put it where it needs to go. Thanks in advance. Look forward to some kind advice to nudge me over these rough spots. You can see that I have made considerable effort already. If my college tutor were open, I would go there. You guys/gals need to be my tutors.

Is This A Good Question/Topic? 0

## Replies To: Loop issues and syntax calculation challenges (not with the math)

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 10 March 2012 - 03:20 PM

continue means "immediately go back to the loop control statement and (if not finished) start the next iteration". Therefore, none of the code in the part of the loop below the continue statement will ever be executed. Fix that, and then you should be able to make some more progress.

This post has been edited by r.stiltskin: 10 March 2012 - 03:25 PM
Reason for edit:: to clarify meaning

### #3 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 10 March 2012 - 04:39 PM

I should have explained more fully:

continue is used when, due to the occurrence of some condition or event, you don't want the rest of the code in the loop to be executed. Therefore, it's usually placed in an if statement. It would look something like this:

```while( [condition A] ) {

// do stuff...

if( [condition B] ) {
continue;
}

// do other stuff whenever condition B is false

}

```

Conditions A and B may be related or they may be completely independent of each other.

continue is similar to break. The difference is that break will immediately and completely quit the loop, whereas continue will keep looping but, for the current iteration, it will skip the portion of the loop that comes after the continue statement.

### #4 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 10 March 2012 - 07:51 PM

I still cannot get values to read across loops. I must be really lousy at nesting loops. I still tend to either close them off or run everything together.

I am supposed to get, as a dialogue:

How many categories in the gradebook? 3

Enter the weight of Category 1: 0.05
How Many items available in category 1? 2
Enter points earned, then points possible for Item 1 in Category 1: 8 8
Enter points earned, then points possible for Item 2 in Category 1: 7.5 8
Your grade for Category 1 is 15.5 out of 16, or 96.875%
Your weighted grade for Category 1 = 0.05 x 96.875 = 4.84375

Enter the weight of Category 2 : 0.15
How many items available in Category 2? 3
Enter points earned, then points possible for Item 1 in category 2: 4.5 5
Enter points earned, then points possible for Item 2 in Category 2: 10 10
Enter points earned, then points possible for Item 3 in category 2: 9 10
Your grade for category 2 is 23.5 out of 25, or 94%
Your weigfhted grade for Category 2 = 0.15 x 94 = 14.1

Enter the weight of Category 3: 0.35
How many items available in Category 3? 1
Enter points earned, then points possible for Item 1 in category 3: 18 20
Your grade for Category 3 is 18 out of 20, or 90%
Your weighted grade for Category 3 = 0.35 x 90 = 31.5

Press any key to continue...

Ther is a lot of crossing values that my looping scheme is not allowing me to properly place where they need to go and in the values that belong there. I do not know how to run a total of item points so I can carry them into my equations. My loops will not allow me to get anything beyond individual item value calcuolations and looping back to the next Category. I am not getting to the last 2 lines' values correctly added. I get the last item's value calculated where the total values should be. I never get to the coourse grade percentage. If I get that far, I will put int the long If Else to capture all possible grades. (A+, a, a-, b+, etc) I am on my 14th hour of futility today and had 7 yesterday. I am getting fairly convinced I cannot get this without help beyond that of this forum. I do not know what I am going to do.

### #5 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 10 March 2012 - 08:02 PM

When you declare a variable inside a loop, it exists only for the current iteration of the loop. On the next iteration, a brand-new variable of the same name is created, and any value that it had during the previous iteration is lost.

Any variables whose values must persist for longer than a single iteration must be declared and initialized before the loop structure.

### #6 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 11:44 AM

Am I using the wrong loops? Why won't the loop calculations carry to the variables outside the loop?

### #7 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 12:06 PM

points, pointsPoss, pointAvg, points_1, pointsPoss_1, weightGrade, pointsTotal, pointsPossTotal, pointAvgTotal are all declared inside the inner loop so each one exists only for a single iteration of that inner loop, in other words, for a single "item".

By the way, your code would be easier to understand if you would indent it better. For example, I would format your code from Post #1 as follows.
```#include<iostream>
using namespace std;

int main()
{
double weight=1.0;

cout << "How many categories in the gradebook? ";

//This loop prints categories in ascending order

//read in the weight of the category
cout << "Enter the weight of Category " <<gradeBook <<" : ";
cin >> weight;

cout << "How many items available in category " << gradeBook <<" ? ";
int items=1, item=1;
cin >> items;

//Nested loop
for (item=1; item<=items; item++) {

//The points earned and points possible for each item
cout << "Enter points earned, then points possible for item " << item <<" in category " <<gradeBook <<" : " <<endl;
double points=1.0, pointsPoss=1.0, pointAvg=1.0, points_1=1.0, pointsPoss_1=1.0;
cout << "Earned ";
cin >> points;
cout << "Possible ";
cin >>pointsPoss;
continue;
//compute and display the percentage grade of totalled items

pointAvg=((points_1/pointsPoss_1)*100);
cout << "Your grade for category " <<gradeBook <<" is " <<points <<" out of " <<pointsPoss <<" , or " <<pointAvg <<" % "<<endl;

//This is the weighted grade for each category

//The course grade letter anbd percentage in a large multi-way if-else statement
double pointsTotal=1.0, pointsPossTotal=1.0, pointAvgTotal=1.0;
pointsTotal=points++, pointsPossTotal=pointsPoss++, pointAvgTotal=((pointsTotal/pointsPossTotal)*100);
}
}
return 0;
}

```

This post has been edited by r.stiltskin: 11 March 2012 - 12:09 PM

### #8 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 05:49 PM

```#include<iostream>
using namespace std;

int main()
{

cout << "How many categories in the gradebook? ";

//This loop prints categories in ascending order
//nested loop to get categories to count up
do {
//read in the weight of the category
cout << "Enter the weight of Category " <<gradeBook <<" : ";
double weight=1.0;
cin >> weight;

cout << "How many items available in category " << gradeBook-1 <<" ? ";
int items=1, item=1;
cin >> items;

//Nested loop to get items to count up
do {
//The points earned and points possible for each item
cout << "Enter points earned, then points possible for item " << item <<" in category " <<gradeBook-1 <<" : " <<endl;
double points=1.0, pointsPoss=1.0, points_1=1.0, pointsPoss_1=1.0;

cout << "points ";
cin >> points;
cout << "Possible ";
cin >>pointsPoss;
item++;

//compute and display the percentage grade of totalled items
cout <<pointsPoss <<" , or ";
double pointsAvg=1.0;
pointsAvg=((points/pointsPoss)*100);
cout <<pointsAvg <<" % "<<endl;

//This is the weighted grade for each category

//The course grade letter and percentage in a large multi-way if-else statement
double pointsTotal=1.0, pointsPossTotal=1.0, pointAvgTotal=1.0;
pointsTotal=points, pointsPossTotal=pointsPoss, pointAvgTotal=((pointsTotal/pointsPossTotal)*100);
}while(item<=items);

return 0;
}
```

I made the loops do-while instead of for. Doesn't do anything different.

I need to be able to get the gradebook category number like I do now. The items per gradebook number like I do now. (I get the items to display inside each individual category. The items also seperate for the points actual and possible for each item. But, the average and weighted grade calculates for EACH item) The input for points and possible are supposed to sum to the end of the individual category, calculate the weighted average for the whole category, Then the percentage for each category is supposed to add up at the end, divide by the number, and go through the if els

I currently get the categories to loop one by one. I get the items to loop one by one within the category. I get the points and possible points to read and calculate per item, along with weighted grade. I need the weight and average to only get calculated and read per Individual category. Then I need the percentage per category to add up and divide by the number of the categories to get the final percentage to get the current grade using the if else loop whic i know how to put into the code just abbove the display. at least I hope so. I need to get there first. Anyone can get a number once and use it. How do you get the loop to read the same variable with a different user entry and add those up? Then I can try to work on the other parts. I cannot get the variables to display outside the category or item loops so I can work with them in calculations. Am I in an exercise of futility? Guide me please.

This post has been edited by r.stiltskin: 11 March 2012 - 06:36 PM
Reason for edit:: deleted duplicate code

### #9 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 07:06 PM

Changing from for loops to while or do-while loops is a wasted effort. Anything that you can do with one you can do with the others with only minor changes.

Your description of the task is confusing, but it sounds like you probably need a third level of nesting for your loop structure. The general rule is that any variable whose value must persist longer than a single iteration of a loop must be declared before that loop.

And frankly, you would probably find it easier to declare all of your variables at the beginning.

So, for example if I want to input 10 numbers, multiply each of them by 5, print the product, and then add the product to a sum, I could do this (in pseudocode):
```integer: sum
integer: temp
sum <-- 0
repeat 10 times:
temp <-- user_input
temp <-- temp * 5
print temp
sum <-- sum + temp

print sum

```

If I wanted to do the same thing 4 times, totaling up 4 sums into a group total, I could add a grouptotal variable at the beginning and put everything from line 3 onward inside another loop:

```integer: grouptotal
integer: sum
integer: temp
grouptotal <-- 0
repeat 4 times:
sum <-- 0
repeat 10 times:
temp <-- user_input
temp <-- temp * 5
print temp
sum <-- sum + temp

print sum
grouptotal <-- sum

print grouptotal

```

If I want to combine 3 grouptotals into a grandtotal, I could add a grandtotal variable at the beginning, and put everything from line 4 onward into yet another loop.

And so on...

One more minor point: it looks like gradeBook should be an int. I see no reason for it to be a double. You're just using it for counting natural numbers -- that's what ints are for.

### #10 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 07:44 PM

but I do not know how many times the iterations will be. The variable is based upon the variable "items". Until that value, I do not know how many point values or pointPoss values to add in a sum. How do I begin from an unknown like that???

I will declare all variables at the beginning as I figure what names to give them.

I will change the non-calculating variables to int.

### #11 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 07:48 PM

That was just an example. I didn't mean for you to use those constants literally.

Your program uses variables for the number of repetitions, so declare those variables at the beginning, input the values as you have already been doing, and use them to control the loops.

I was just trying to illustrate how to use variables to accumulate the results from multiple loops (which, if I understood you correctly, is the aspect that you're finding difficult).

This post has been edited by r.stiltskin: 11 March 2012 - 07:49 PM

### #12 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 07:53 PM

I changed the doubles to int that need to be int and put all declared variables at the beginning.

### #13 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 07:58 PM

You might also need to declare one or more additional variables if you have to combine information from multiple "gradeBooks".

(Similar to the way my example used "grouptotal" to combine several "sums", and "grandtotal" to combine several "grouptotals". Yours is more complicated, but the underlying principle is the same, if I understood you correctly.)

That's the part that's not quite clear in your description.

This post has been edited by r.stiltskin: 11 March 2012 - 08:01 PM

### #14 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 08:48 PM

The out put I am expecting is the one in #4 post. I am getting the items to add in category 1 of 3. I am not getting the ability to run the two points earned and possible to be together. Each does it's own percentage prior to the two adding up for a category percentage and weight. I also get a course grade there also. I know it is from the position of the condition and right bracket but I have been unable to cast values beyond the brackets. The numbers of items carry to the next category but only iterate once regardless of the number I put in because the number 4 is obviously higher than the 3 I input for category 2. My loops are still wrong but the math is summing up. So, you got me over that hump.

If I do not get this one this week during Spring Break, I am dropping this class. I already e-mailed my Vocational Rehabilitation Counselor. My Psychologist said I do not need the grief, and I will have to change Majors so I will not need this grade. HOWEVER, It will drive me crazy if I do not complete this course. It will be the last programming I ever do. I promise you that.

Please help me finish this class. I only have this homework and a few more. I beg you.

```#include<iostream>
using namespace std;

int main()
{
double weight=1.0, points=1.0, pointsPoss=1.0, points_1=0.0, pointsPoss_1=0.0;
double pointsTotal=0.0, pointsPossTotal=0.0, pointAvgTotal=0.0;

cout << "How many categories in the gradebook? ";
cout <<endl;

//This loop prints categories in ascending order
//nested loop to get categories to count up
do {
//read in the weight of the category
cout << "Enter the weight of Category " <<gradeBook <<" : ";
cin >> weight;

cout << "How many items available in category " << gradeBook-1 <<" ? ";
cin >> items;

//Nested loop to get items to count up
do {
//The points earned and points possible for each item

cout << "Enter points earned, then points possible for item " << item <<" in category " <<gradeBook-1 <<" : " <<endl;
item++;
cout << "points ";
cin >> points;
cout << "Possible ";
cin >>pointsPoss;

points_1+=points;
pointsPoss_1+=pointsPoss;

//compute and display the percentage grade of totalled items
cout <<pointsPoss_1 <<" , or ";
pointsAvg=((points_1/pointsPoss_1)*100);
cout <<pointsAvg <<" % "<<endl;

//This is the weighted grade for each category
cout<<endl;
//The course grade letter and percentage in a large multi-way if-else statement
pointsTotal=points, pointsPossTotal=pointsPoss, pointAvgTotal=((pointsTotal/pointsPossTotal)*100);

}while(item<=items);

return 0;
}
```

here is an update on what I have done.

### #15 Famlyman45

Reputation: 0
• Posts: 23
• Joined: 24-February 12

## Re: Loop issues and syntax calculation challenges (not with the math)

Posted 11 March 2012 - 08:56 PM

I need a Category 2 and 3 reset. It is somewhere in the loop. Maybe I used the wrong loop by tricking the condition. It worked for categories and the first items but not in category 2 and 3.