Thread: Newbie trying to create a program in C language has run into an error can you help?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Newbie trying to create a program in C language has run into an error can you help?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void){
        
        int stw;
        int weight;
        int total;
        stw=190;
        total=0;
        loopcount=0;
            while (loopcount<3){
               printf("Hello Mickey, what is your weight today?");
               scanf("%d", &weight);
               loopcount=loopcount+1;
    }
        
       
        total = stw - weight;
        printf("Total weight lost is %d", weight);
        scanf("%d", &total);
        getch();
        getch();
        
    }
    I get the error:
    `loopcount' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

    Please help with this simple program if you can. Thank you.

  2. #2
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    Have you tried declaring loopcount?

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, make sure you return 0 at the end of main.

    On a side note get rid of conio and all the obsolete crap DOS libraries, enter the 21st century and get an IDE more modern than Turbo C such as Codeblocks (my favourite), Pelles, etc.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    This looks wrong:

    Code:
        total = stw - weight;
        printf("Total weight lost is %d", weight);
        scanf("%d", &total);
    1. the weight lost is total, not weight, right? (I'm assuming stw is "starting weight")
    2. What's the scanf call for? (i.e., why are you attempting to input something?)
    Last edited by mike65535; 06-07-2011 at 02:37 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Quote Originally Posted by Inanna View Post
    Have you tried declaring loopcount?
    I am really really new...uh but I did finally get it and also thanx to Mike for helping me clear up the bottom.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You're recording weights in the loop, OP, but you're not doing anything with that information. For example, if someone weighs too much and wants to lose thirty pounds in three months, you record a weight each month and subtract each time to figure out how much they lost.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    You're recording weights in the loop, OP, but you're not doing anything with that information. For example, if someone weighs too much and wants to lose thirty pounds in three months, you record a weight each month and subtract each time to figure out how much they lost.
    Thats the kind of programming that I would eventually like to do but for now I am starting off very slow. I was told C was the easiest language to learn to program with so here I am. I just got snafu'ed with the earlier problem and some other problems but now it seems to work...Maybe I can move on to more complex tasks now...Thanx for the help guys.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    C is far from the easiest language to learn. It's not as bad as C++, but there are better languages.

    Furthermore, if you want to learn to program, you should use a modern object oriented language.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ newbie program error
    By brack in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2010, 05:34 AM
  2. Newbie in C++ and error with run program
    By Daydream in forum C++ Programming
    Replies: 15
    Last Post: 12-02-2008, 12:47 PM
  3. Create some animation using C language??
    By neo_lam in forum C Programming
    Replies: 3
    Last Post: 11-21-2006, 07:20 AM
  4. newbie: steps to create and compile c program in SUSE
    By gemini_shooter in forum Linux Programming
    Replies: 12
    Last Post: 06-22-2005, 06:35 PM
  5. a program to create months in a year newbie question
    By robasc in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 05:41 PM

Tags for this Thread