Newbie here trying to teach myself programming in c

This is a discussion on Newbie here trying to teach myself programming in c within the C Programming forums, part of the General Programming Boards category; Ok I am trying to teach myself programming in c but I am lost. I have gotten through the first ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    33

    Question Newbie here trying to teach myself programming in c

    Ok I am trying to teach myself programming in c but I am lost. I have gotten through the first few tutorials but I am stuck on this one.

    I need my output to be as the following:

    This program will do some computations.

    The two numbers I am using in this program are 63 and 25.

    The Sum of 63 plus 25 is 88.


    This is what i have written so far but I am so lost on this. Can someone tell me what I am doing wrong? Please I am getting an error on line 12 at the {
    Code:
    /*mavrakp1.c */
    /*Created by Colleen Mavrakos 2/7/2006 */
    
    #include <stdio.h>
    
    main () {
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
    	 return 0;
    }
    {
    	 int sum;
    	 sum = 63 + 25;
    	 printf ("The Sum of 63 and 25 is %i\n", sum);
    
    }
    or should it be something like this:

    Code:
    /*mavrakp1.c */
    /*Created by Colleen Mavrakos 2/7/2006 */
    
    #include <stdio.h>
    
    main () {
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
    	 return 0;
    
    {
    	 int  sum;
    	 int  num1 = 63;
    	 int  num2 = 25;
    
    	 sum = num1 + num2;    // addition
    }
    I get the second one to run with no errors but the "The sum of 63 plus 25 is 88" is not showing up.
    Last edited by paulntysmom; 02-08-2006 at 08:51 AM.

  2. #2
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by paulntysmom
    Ok I am trying to teach myself programming in c but I am lost. I have gotten through the first few tutorials but I am stuck on this one.

    I need my output to be as the following:

    This program will do some computations.

    The two numbers I am using in this program are 63 and 25.

    The Sum of 63 plus 25 is 88.


    This is what i have written so far but I am so lost on this. Can someone tell me what I am doing wrong? Please I am getting an error on line 12 at the {
    Code:
    /*mavrakp1.c */
    /*Created by Colleen Mavrakos 2/7/2006 */
    
    #include <stdio.h>
    
    main () {
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
    	 return 0;
    }
    {
    	 int sum;
    	 sum = 63 + 25;
    	 printf ("The Sum of 63 and 25 is %i\n", sum);
    
    }
    or should it be something like this:

    Code:
    /*mavrakp1.c */
    /*Created by Colleen Mavrakos 2/7/2006 */
    
    #include <stdio.h>
    
    main () {
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
    	 return 0;
    
    {
    	 int  sum;
    	 int  num1 = 63;
    	 int  num2 = 25;
    
    	 sum = num1 + num2;    // addition
    }
    I get the second one to run with no errors but the "The sum of 63 plus 25 is 88" is not showing up.

    Code:
    return 0;
    That's causing your program to end..
    Also watch your bracket use, here's a sample to compare.


    Code:
    /*mavrakp1.c */
    /*Created by Colleen Mavrakos 2/7/2006 */
    
    #include <stdio.h>
    
    int main () {
    	 int sum;
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
    
    	 sum = 63 + 25;
    	 printf ("The Sum of 63 and 25 is %d\n", sum);
    	 return 0;
    
    }
    Moved the declaration of sum since it needs to go in the beginning of a block of code.
    Changed the %i in printf to a %d.
    Moved the return 0 to the end of the main function.

    You might want to pick up a book to read.. like 'The C Programming Language'

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    As you've given it here, I'm surprised the second one executed: at all: there is no closing brace to terminate the main) function. Try one of the following;

    Code:
    #include <stdio.h>
    
    main () {
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
    
    {
    	 int  sum;
    	 int  num1 = 63;
    	 int  num2 = 25;
    
    	 sum = num1 + num2;    // addition
             printf("Sum is %d\n", sum);   // print the result
          }
    
    	 return 0;    // return statement AFTER you compute the sum
    }
    or (if you want to do it with functions);
    Code:
    #include <stdio.h>
    
    void myfunction();
    
    main () {
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
             myfunction()
    	 return 0;
    }
    
    void myfunction()
    {
    	 int  sum;
    	 int  num1 = 63;
    	 int  num2 = 25;
    
    	 sum = num1 + num2;    // addition
             printf("Sum is %d\n", sum);   // print the result
    }
    As a general rule, code placed in a function after a return statement does not get executed. And the compiler doesn't magically decide that, since you're computing a sum, that it needs to print the result.

    Unrelated to your problem: the // style of comment is fairly recent, and is only supported in C++ compilers (reasonably common) or C compilers compliant with the 1999 C standard (relatively rare). C compilers that comply with the 1989 C standard will choke on such comments.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    33

    Question Thank you :)

    Ok I am slowly understanding what I did wrong. I did purchase two books programming in ansi c and C Programming in Easy Steps. I have no programming experience (can you tell LOL)

    I think the most confusing part to me is just adding in a new part to the program.

    Example from above

    This program will do some computations
    The two numbers I am using in this program are 62 and 25.
    The sum of 63, plus 25 is 88

    Now if I wanted to add another line in there
    Code:
    (say something like - The product of 63 times 25 is 1575)  do I just start of by using { and will the return 0: go at the very end of the program? and then close if off using }

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    Ok I think I did this right to add in on my last reply! Can someone please let me know if I did it right or wrong?

    Code:
    /*mavrakp1.c */
    /*Created by Colleen Mavrakos 2/7/2006 */
    
    #include <stdio.h>
    
    int main () {
    	 int sum;
    
    	 printf("This program will do some computations\n");
    	 printf("The two numbers I am using in this program are 63 and 25.\n");
    
    	 sum = 63 + 25;
    	 printf ("The Sum of 63 plus 25 is %d\n", sum);
    
    	 sum = 63 * 25;
    	 printf ("The Product of 63 times 25 is %d\n", sum);
    	 return 0;
    
    }
    I compiled it and it came up with no errors and the output of it is :
    This program will do some computations.
    The two numbers I am using in this program are 63 and 25.
    The Sum of 63 plus 25 is 88
    The Product of 63 times 25 is 1575

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Excellent program.

    Good luck with programming.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    If you want a free compiler that does a decient job...and 1999 compliant - Dev-C is freeware.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    33

    Smile Thanks

    Thank you guys so much for your help. This board will help me alot

    I purchased Borlands Turbo C++ 4.5 (is that a good one) but it was a breeze to set up and it is user friendly (well at least for me) I really do want to learn how to program so I am reading reading and reading as much as I can.

  9. #9
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by paulntysmom
    Thank you guys so much for your help. This board will help me alot

    I purchased Borlands Turbo C++ 4.5 (is that a good one) but it was a breeze to set up and it is user friendly (well at least for me) I really do want to learn how to program so I am reading reading and reading as much as I can.
    I can't answer your compiler questions, as I work mostly on Linux and use gcc.

    But I do want to wish you luck, and tell you to keep asking questions in here! It's refreshing to see someone who really WANTS to learn, as opposed to some folks who just want to find the easiest way to get their homework assignment done.

    BTW, the previous suggestion about picking up "The C Programming Language" by Brian Kernighan and Dennis Ritchie is something you really should think about. It might seem a bit complex from a raw beginner standpoint (althought it was my first text), but it is a book that I think you will find highly valuable as you progress.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    Quote Originally Posted by fgw_three
    "The C Programming Language" by Brian Kernighan and Dennis Ritchie is something you really should think about.
    This is the book I use in Comp Solutions, part of EE program at UTA...
    good book

    Borlind is a decient compiler, one of the industry leaders.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    Thank you, I will buy that book also. Everything helps You guys are wonderful! Thanks for all the advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. Nothing but newbie.
    By Jimmey in forum C++ Programming
    Replies: 10
    Last Post: 07-03-2005, 11:23 PM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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