Beginner, help with loop and variable

This is a discussion on Beginner, help with loop and variable within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to make a loop that will loop for the value that has been input and then continues ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    11

    Question 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. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.
    Last edited by Subsonics; 05-28-2012 at 05:09 AM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    11
    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. #4
    Registered User
    Join Date
    May 2012
    Posts
    11
    (D) is another input and the way the math played out it needed to be less than not equal to

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by keeganstarr View Post
    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 View Post
    (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. #6
    Registered User
    Join Date
    May 2012
    Posts
    11
    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. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Why don't you just update p?

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

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    11
    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. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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. #10
    Registered User
    Join Date
    May 2012
    Posts
    11
    Okay so I would need to make the printf inside the loop to allow it to hold the correct value?

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    No, you would need to stop declaring a new variable with the same name.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    11
    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. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by keeganstarr View Post
    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. #14
    Registered User
    Join Date
    May 2012
    Posts
    11
    Ahh gotcha, that makes sense. Thank you for all your help, I appreciate it and will give it a try.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

    2) The code becomes unreadable.

    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Problem with Loop
    By ToweyLeake in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2011, 10:12 AM
  2. beginner loop help
    By helloalyssa in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2010, 09:08 PM
  3. Replies: 10
    Last Post: 03-28-2010, 01:35 AM
  4. Replies: 4
    Last Post: 11-15-2005, 01:13 PM
  5. trouble with a loop (beginner)
    By Procta in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2003, 09:27 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21