Like Tree1Likes

Problem in my first program :/

This is a discussion on Problem in my first program :/ within the C Programming forums, part of the General Programming Boards category; this is my first program, a simple arithmetic, but it wont add! Code: main() { int x,y,z; scanf("%d", x); scanf("%d", ...

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

    Thumbs down Problem in my first program :/

    this is my first program, a simple arithmetic, but it wont add!

    Code:
    main()
    {
       int x,y,z;
    
       scanf("%d", x);
       scanf("%d", y);
    
    z=x+y;
    
    printf("sum is:", z);
    
    
    
    }
    where did I go wrong? compiler shows no error,
    takes input and prints only--> sum is:



    pretty demotivating for my first program!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RattleSlash View Post
    this is my first program, a simple arithmetic, but it wont add!
    No worries... a very simple fix...

    Code:
    main()
    {
       int x,y,z;
    
       scanf("%d", x);
       scanf("%d", y);
    
    z=x+y;
    
    printf("sum is: %d \n", z);
    
    
    
    }
    And don't let it bug you... I've been at this for a long time and I still make some rather obvious mistakes... It kinda comes with the territory.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You really should read through whatever study material you are using, you seemed to have missed some crucial knowledge. Have a read through the site's C Tutorials. As for your immediate questions:
    Code:
    #include <stdio.h>
    
    int main(void){ //note how main is defined
    
    	int num1, num2, result; //note descriptive variable names
    
    	printf("Enter two numbers:");
    	/*scanf takes pointers to the variables
    	to store the input. Note the use of the & operator*/
    	scanf("%d %d", &num1, &num2); 
    	result = num1+num2;
    
    	printf("Result is: %d", result);
    
    	return(0); //Note returning a value to the OS
    }
    anduril462 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make one more simple fix: (in red)


    Code:
    int main(void)
    {
       int x,y,z;
    
       scanf("%d", &x);
       scanf("%d", &y);
    
       z=x+y;
    
       printf("sum is: %d \n", z);
    
       return 0;
    
    }
    Scanf() needs the address of the variable it is supposed to change. Printf() doesn't need it, but scanf() does.

    int main(void) with a return of 0, is something you want to add to your programs as well. May not be necessary right now, but it is ONE of the standard ways to structure a C program -- main() is not.
    Last edited by Adak; 09-12-2011 at 05:16 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Quote Originally Posted by AndrewHunter View Post
    You really should read through whatever study material you are using, you seemed to have missed some crucial knowledge. Have a read through the site's C Tutorials. As for your immediate questions:
    Code:
    #include <stdio.h>
    
    int main(void){ //note how main is defined
    
    	int num1, num2, result; //note descriptive variable names
    
    	printf("Enter two numbers:");
    	/*scanf takes pointers to the variables
    	to store the input. Note the use of the & operator*/
    	scanf("%d %d", &num1, &num2); 
    	result = num1+num2;
    
    	printf("Result is: %d", result);
    
    	return(0); //Note returning a value to the OS
    }
    thanx andrew!
    did it! and it worked!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    Scanf() needs the address of the variable it is supposed to change. Printf() doesn't need it, but scanf() does.
    To be even more precise, any function that you expect it to change whatever you are passing it, without assigning that value via a return value, needs the address of (a pointer to) whatever it is expected to change: Pointers in C - Tutorial - Cprogramming.com


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Make one more simple fix: (in red)

    Quote Originally Posted by CommonTater View Post
    No worries... a very simple fix...

    Code:
    main()
    {
       int x,y,z;
    
       scanf("%d", &x);
       scanf("%d", &y);
    
    z=x+y;
    
    printf("sum is: %d \n", z);
    
    
    
    }
    Scanf() needs the address of the variable it is supposed to change. Printf() doesn't need it, but scanf() does.
    Nice catch Andrew and Adak...

    (I'm thinking it's time to get my reading glasses prescription updated... "What's that?" ... "I can CLEAN them?".... Who knew! )
    Last edited by CommonTater; 09-12-2011 at 05:36 PM.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    [QUOTE=CommonTater;1052036]
    Quote Originally Posted by Adak View Post
    Make one more simple fix: (in red)



    Nice catch Andrew and Adak...

    (I'm thinking it's time to get my reading glasses prescription updated... "What's that?" ... "I can CLEAN them?".... Who knew! )
    You are spending too much time with your other projects, it is killing your eyes my friend.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Quote Originally Posted by quzah View Post
    To be even more precise, any function that you expect it to change whatever you are passing it, without assigning that value via a return value, needs the address of (a pointer to) whatever it is expected to change: Pointers in C - Tutorial - Cprogramming.com


    Quzah.

    I hope some day soon I understand watever you just said!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote tags: You are doin' it wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RattleSlash View Post
    I hope some day soon I understand watever you just said!
    You will... just keep working your textbooks and doing those examples and exercises... It'll come soon enough.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    You are spending too much time with your other projects, it is killing your eyes my friend.
    <sigh> I fear you may be right...
    Last edited by CommonTater; 09-12-2011 at 05:37 PM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Quote tags: You are doin' it wrong.
    Quzah.
    Who me? No way...

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by RattleSlash View Post
    I hope some day soon I understand watever you just said!
    Don't count on it; English is not quzah's first language.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    Quote tags: You are doin' it wrong.


    Quzah.
    Interesting enough, these errors have showed up after the switch to the new format. In all cases I have just hit the "Reply with Quote" link.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

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

Similar Threads

  1. CS1 Program (New Problem)
    By DaNxTh3xMaNx in forum C Programming
    Replies: 3
    Last Post: 09-30-2010, 02:04 AM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  4. Program problem
    By Mick in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2002, 06:41 AM
  5. Problem with program...
    By Screwz Luse in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:54 AM

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