Thread: C beginners question

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Question C beginners question

    Hi guys, I've just started learning C and had a question. I am making a program that :

    * Loops until user selects NO.
    * In every loop it asks for an "int" value between (1-10), adds it to the previous value and displays it as "*".

    I can get the variable to increment the new input value with the old one, like:
    Code:
    finalValue += oldValue
    Problem--> However, when I try to display the finalValue as "*" its still working according to the value that was given to it the last time, not the total sum that has accumulated so far.

    This is my actual code, I would really appreciate if someone could point out what is it that I am doing wrong
    Code:
                    feedFinalScore = feedf(feedInput);     /* getting its value from feedf function */
                    feedTotal += feedFinalScore;
                    printf("Feed level: ");
                          do      {
                    
                          printf("*");
                          j = j++;                                        /* j set to 1 at the start */
                                    } while ( j <= feedTotal);     
    
                    printf("\n %d\n", feedTotal);        /* echoes info to check if the total value is */
                                                                         /* passed to the end of program or not */
    When I try to run this, the last printf statement shows the accurate sum (the way it is supposed to, the total accumulated value of all loops) however the first printf statement (with the * ) shows only the last recent input(not the total value).

    What I am wondering is, if its the same variable, how come the first printf statement isnt taking the value from that because one line after that, the variable still holds the right information.

    I would appreciate any type of help.
    Thanks.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i assume there is another loop controlling the function call feedf()? otherwise you are only collecting one value anyway, i.e thein effecet that is the final one, post a bit more code?

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    in the printf() in the do while loop you are only asking it to print an asterix "*" you are not asking for e.g. an integer > printf("&#37;d", myvalue) you have done this ok for the final print statement so i assume you must have just forgotten in the loop

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
                          do      {
                    
                          printf("*");
                          j = j++;                                        /* j set to 1 at the start */
                                    } while ( j <= feedTotal);
    Looking closely here, where do you set j to one? Nowhere!
    j++ simply increments j by one and assigns it to j.
    So j = j++ could be written as j++, but it still just increments j and does not set it to one.
    And for this task, a for loop might be more suitable. Allow me to demonstrate:
    Code:
    for (j = 1; j <= feedTotal; j++)
    {
    	printf("*");
    }
    From the little code you posted (and from what you described the problem as), I gather this is the problem (or one of them anyway).
    I'm not sure about your comment, if you meant it sets j to 1 or if it's set to 1 before the actual loop, because you haven't shown that code.
    Anyway, there's no harm in trying it. And there's no harm in switching it to a for loop either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    success!!!

    hey thanks for the feedback guys. First of all, it worked. Elysia's suggestion to use "for" instead of "do-while" worked. All I had to change the loop structure, nothing else and IT WORKS!!!

    Elysia: I was setting j's value to 1 right at the start of the program, where I declared all the other variables as well, heh. But yeah, your method works. Now I just need to learn where to use "for" and "do-while".

    rogester001: yeah it was getting its value from another function feedf(), all the feedf() did was double the input that was given to it and return it, so I didnt think it was necessary to paste its code. I kind of had an idea that something was wrong in my loop or printf lines.
    And in the loop printf statement, i wasnt trying to print the int value itself, but those asterisks "*" as many times as the int value.

    Thanks a lot for helping me see outside the box guys, i will definitely keep that in mind.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by incipientmind View Post
    hey thanks for the feedback guys. First of all, it worked. Elysia's suggestion to use "for" instead of "do-while" worked. All I had to change the loop structure, nothing else and IT WORKS!!!

    Elysia: I was setting j's value to 1 right at the start of the program, where I declared all the other variables as well, heh. But yeah, your method works. Now I just need to learn where to use "for" and "do-while".
    Then your problem was that you didn't reset j to 1 everytime before the loop, which is what the new for loop does. Simply because I told it to do so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 05:53 PM
  2. beginner's question :D
    By kingliaho in forum C Programming
    Replies: 5
    Last Post: 10-17-2008, 05:20 PM
  3. beginner's question about C
    By Roberto Llovera in forum C Programming
    Replies: 5
    Last Post: 02-26-2004, 05:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM