# Beginner, help with loop and variable

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-28-2012
keeganstarr
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!
• 05-28-2012
Subsonics
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.
• 05-28-2012
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
• 05-28-2012
keeganstarr
(D) is another input and the way the math played out it needed to be less than not equal to
• 05-28-2012
Subsonics
Quote:

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.

Quote:

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.
• 05-28-2012
keeganstarr
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.
• 05-28-2012
Subsonics
Why don't you just update p?

Code:

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

• 05-28-2012
keeganstarr
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.
• 05-28-2012
Subsonics
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.
• 05-28-2012
keeganstarr
Okay so I would need to make the printf inside the loop to allow it to hold the correct value?
• 05-28-2012
Subsonics
No, you would need to stop declaring a new variable with the same name.
• 05-28-2012
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?
• 05-28-2012
Subsonics
Quote:

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;
}

• 05-28-2012
keeganstarr
Ahh gotcha, that makes sense. Thank you for all your help, I appreciate it and will give it a try.
• 05-28-2012
claudiu
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.