# Thread: Beginner, help with loop and variable

1. ## Beginner, help with loop and variable

Hello, I'm trying to make a loop that will loop for the value that has been input and then continues to update a variable so that at the end it can spit out the total. The catch is I can't figure out how to update that variable. I am using C not C++ which has the new and delete. I looked around but haven't had any luck finding anything that I can understand. This is what I have so for:
Code:
```for (int r=0; r<d; r++)
{
int q = (p*2);
int p = (q);
}
printf("At the end of the month your total is:\n");
printf("%d\n", p);
return 0;```
Before I can update p I need a way to clear it. Any help would be great, thanks!

2. I have no idea what you are asking actually. In the loop you are effectively doing: p = p * 2 until r == d.

What is it you want to do exactly?

Edit: btw, is there another 'p' in your program? Because now you are using p before you declare it.

3. yes there is. That may have helped, sry. P is also an inputed variable prior for the calculation information. That's why I am stuck. I cant find a way to replace it so that it continuously updates the new value of (p). (D) is another input

4. (D) is another input and the way the math played out it needed to be less than not equal to

5. Originally Posted by keeganstarr
yes there is. That may have helped, sry. P is also an inputed variable prior for the calculation information. That's why I am stuck. I cant find a way to replace it so that it continuously updates the new value of (p). (D) is another input
Ok so you have two variables named p. The initial value of the p that exists before you enter the loop is multiplied with 2 and assigned to q.

Your new p with a restricted scope to the for loop is assigned the result of old_p * 2 over and over.

dpq are horrible variable names, now you also have two variables named p. Your original p is never updated in the loop.

Originally Posted by keeganstarr
(D) is another input and the way the math played out it needed to be less than not equal to
Sure, but the loop will continue until r == d, when it is the loop body will not be executed anymore.

6. I'll show you the whole thing that way you will be able to understand why I'm doing what I am.
Code:
```#include <cs50.h>
#include <stdio.h>

int
main(void)
{
days:
printf("How many days in month?");
int d = GetInt();
if (d>=28 && d<=31)
{
printf("Days in months were: %d\n", d);
}
else
{
printf("Invalid number of days\n");
goto days;
}
pennies:
printf("How many pennies are you starting with (1 or 2)?");
int p = GetInt();
if (p>=1 && p<=2)
{
printf(" Starting amount is: %d penny(ies)\n", p);
}
else
{
printf("You did not choose a valid option!");
goto pennies;
}
for (int r=0; r<d; r++)
{
int q = (p*2);
int p = (q);
}
printf("At the end of the month your total is:\n");
printf("%d\n", p);
return 0;```
its a small project. I see what you are saying with the loop and will change that. I'm not sure of the best way to do this but the second (p) needs to be the value inputed at the beginning. It needs to start out as the same and then continue to updated. I understand the scope conflict but I'm not sure how to correct it.

7. Why don't you just update p?

Code:
```for (int r=0; r<d; r++)
{
p = p * 2;
}```

8. I didn't think that would, but it did. Now the only problem is its only displaying 1 as the value of p at the end instead of the total after the loop and math.

9. Did you get rid of int p in the loop? You don't want two variables with the same name like this. A variable that is declared with in the loop scope is not known outside the loop.

The p you are referring to outside the loop is the old p.

10. Okay so I would need to make the printf inside the loop to allow it to hold the correct value?

11. No, you would need to stop declaring a new variable with the same name.

12. Hmm, even though it is updating itself after each loop? How would I go about carrying that value from within the loop to outside so I can display it?

13. Originally Posted by keeganstarr
Hmm, even though it is updating itself after each loop? How would I go about carrying that value from within the loop to outside so I can display it?
? The variable is declared in main, it's accessible in the loop and outside. Example.

Code:
```int main()
{
int a = 1;

for(int i = 0; i < 10; i++) {
a = a * 2;
}

printf("%d\n", a);

return 0;
}```

14. Ahh gotcha, that makes sense. Thank you for all your help, I appreciate it and will give it a try.

15. Also, refrain from using goto's as much as possible. There are several situations in which a goto is not terribly harmful, but you seem to be a very happy goto user, and that is a bad thing for several reasons:

1) It leads to spaghetti code, meaning execution jumps from one place to another almost randomly, and this prevents the compiler from doing good optimizations to the code.

3) Tracking bugs in spaghetti code will most likely lead to exasperation and a shift+delete of the source file, followed by starting over from scratch. This may not be the case with your 50 line program, but it will certainly be the case with a 1000 lines program.