Thread: i admit im a noobie

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    i admit im a noobie

    im starting to learn from the most basic 10 line progams(meaning 10 or less lines of code) to the most complex programs like i shuld but i cant even get a very basic code to work so in this topic (this same topic every time) im gonna post my codes and see what im doing wrong

    so heres #1 its supposed to multiply number "a" by number "b" then print the output... but instead it crashes after the first number

    Code:
    #include<stdio.h>
    int main(void){
    printf("enter a number hit return then repeat.\n");
    int a;
    a = scanf("%d" ,a);
    int b;
    b = scanf("%d" ,b);
    printf("%d",a * b);
    getchar();
    getchar();
    return 0;
    }
    please dont just hand me the edited code..... explane how i can avoid the mistake the next time



    EDIT: im sorry for my last topic i stated 2 months ago i was so into the game that i didnt care at all but now ive seen how bad i was and about a month ago i quit the game perminantly and im trying to get a real life
    Last edited by tommytomthms5; 06-03-2006 at 03:45 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You were kinda close there. Know that scanf returns an int equal to how many fields in the format string it was able to read.

    But when you pass in arguments to scanf, they should be a memory address.
    Code:
    int a;
    scanf("%d" , &a);
    int b;
    scanf("%d" , &b);

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your buffer-flushing code (two getchar()s) should be this:
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    See the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    You can read multiple values with scanf() at once:
    Code:
    scanf("%d%d", &a, &b);
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("enter a number hit return then repeat.\n");
    Actually, all the user has to do is put some sort of whitespace between the numbers. A newline will do, but so would a space or a tab or anything that isspace() returns true for.

    when you pass in arguments to scanf, they should be a memory address.
    What you were doing was passing the values of the variables a and b (which are uninitialized, [somewhat] random values). scanf() needs to know where the variables are located in memory so it can put the value the user entered into it.

    scanf() returns the number of fields successfully read in. So if you use scanf("%d%d", &a, &b), a good way to see if it worked would be to use
    Code:
    if(scanf("%d%d", &a, &b) == 2) {
        /* successfull */
    }
    else {
        /* error */
    }
    scanf() might not be successfull if, say, the user entered "large" instead of a number. A good way to handle an error like that would be to flush the input buffer using code in my previous post and try again:
    Code:
    while(scanf("%d%d", &a, &b) != 2) {
        int c;
        printf("Invalid number! Please try again: ");
        while((c = getchar()) != '\n' && c != EOF);
    }
    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.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    thank you so much i used

    Code:
    scanf("%d%d", &a, &b);
    EDIT: thats stupid of me to even try to make it a full calculator....

    but i have a question... how can i use the same code using "long double" instead of "int"
    Last edited by tommytomthms5; 06-03-2006 at 04:35 PM.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    just as easy as changing the %d to %ld on each scanf and in your last printf

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by louis_mine
    just as easy as changing the %d to %ld on each scanf and in your last printf
    Hmmm. And what would you use for a long double?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    hhmmm?? wasn´t that the original question??? I would use %ld

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    From the unix manpage ya?
    Code:
           L      Indicates	 that  the  conversion will be either efg and the next
    	      pointer is a pointer to long double or the  conversion  will  be
    	      dioux  and  the  next  pointer is a pointer to long long.	 (Note
    	      that long long is not an ANSI C type.  Any  program  using  this
    	      will not be portable to all architectures).
    Therefore the correct identifier is actually %Lf

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Time to update your man page. long long is in fact an ANSI C type as of C99.


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

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Thanks for the protip, I'll make sure to do that if I get Linux. That's not my document, it belongs to some school, because that's where Google found it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting text onto the next line...lol...noobie questions
    By JOlszewski in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 04:02 PM
  2. Noobie <-- alittle help /w compiler
    By Bean0578 in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2004, 09:22 PM
  3. complete noobie, confused on how to tackle c++
    By fletchpac in forum C++ Programming
    Replies: 7
    Last Post: 07-04-2004, 07:48 AM
  4. Noobie wanting to Expand and simplify this prog
    By bandito9111 in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 03:39 AM
  5. C++ program jumps for noobie
    By bandito9111 in forum C++ Programming
    Replies: 3
    Last Post: 03-01-2003, 01:48 AM