help plz

This is a discussion on help plz within the C Programming forums, part of the General Programming Boards category; ok i just decided to learn c about a week ago and have gotten through a little part of C ...

  1. #1
    ION
    ION is offline
    Registered User
    Join Date
    May 2007
    Posts
    2

    help plz

    ok i just decided to learn c about a week ago and have gotten through a little part of C for Dummies.....good book so far, i thought i was picking it up pretty well, until i tried to actually write something other than an example out of the book......obviously its a very simple program but i can't seem to get it right
    Code:
    #include <stdio.h>
    
    int main()
    {
       int a;
       int b;
       int x;
       printf("Enter a number:");
       gets(a);
       printf("Enter the number you wish to multiply it by:\n");
       gets(b);
       x=(a*b);
       printf("%d multiplied by %d equals %d\n" a,b,x);
       return(0);   
    }
    my compiler is saying: "[Warning] passing arg 1 of 'gets' makes pointer from integer without a cast" on lines 9 and 11 and a "syntax error before "a"" on line 13
    at one time i actually got the program to run, it asked me to enter a number and when i did i got one of those pop up errors that says math.c has encountered a problem and needs to close
    i'm sure its something very simple im forgetting or just don't know about yet, any help or advice would be appreciated

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    gets doesn't take an integer.

    Read the FAQ regarding gets(), you can also declare those vars on one line:

    Code:
    int a, b, x;
    There's also no need for parenthesis on x=(a*b)

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    gets() takes a pointer to type char; you're passing an integer to gets(). scanf() can be used for what you're doing, but there are safer ways to read an integer from stdin.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    Change smothing

    I am a learner of c proggram like you form one month. I think you can use scanf
    replace of get and use (&a) in brecket.

    Thanks

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by bangla View Post
    I am a learner of c proggram like you form one month. I think you can use scanf
    replace of get and use (&a) in brecket.

    Thanks
    eh? What you said makes NO sense.

    If you want to use sscanf,

    Code:
    char buff[256];
    int myint;
    
    fgets(buff, sizeof(buff), stdin);
    sscanf(buff, "&#37;d*", &myint);

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    If you want to use sscanf,
    If you go for the advanced version - use functions properly, check the return values...

    Code:
    char buff[256];
    int myint;
    
    if(fgets(buff, sizeof(buff), stdin) != NULL)
       if(sscanf(buff, "&#37;d", &myint) == 1)
       {
          /* use myint here */
       }
    if you go simple way and suppose that the user enters always what he should - go simple way
    Code:
    int a;
    scanf("%d",&a);
    This code is not error prone, but for the start - it can be easier to understand...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    rather,

    Code:
    char buff[256];
    int myint;
    
    fgets(buff, sizeof(buff), stdin);
    myint = atoi(buff);
    My sscanf example was purley a demonstration, hence no error checking

  8. #8
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    If you're using DevC++ or a similar IDE, you might want to think about putting getch() in there so you can actually see the result without having to run it from command line.

    Code:
    printf("&#37;d multiplied by %d equals %d\n" a,b,x); 
    getch();
    return 0;
    Simple yes, but worth mentioning.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I wouldn't suggest getch(), if you want to 'see' the result, then use something portable like, getchar()...

  10. #10
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16
    Hi ION!

    Like people already said, you can't use gets() when you are reading numbers. I recommend you to use scanf(). here's the working version of your app:

    Code:
    #include <stdio.h>
    
    int main()
    {
     int a;
     int b;
     int x;
     
       printf("Enter a number:");
       scanf("%d", &a);
       printf("Enter the number you wish to multiply it by:\n");
       scanf("%d", &b);
       x=a*b;
       printf("%d multiplied by %d equals %d\n", a, b, x);
    
    return(0);   
    }

  11. #11
    ION
    ION is offline
    Registered User
    Join Date
    May 2007
    Posts
    2
    thanx for your help guys.......some of your replies went right over my head but i think i got some of it anyways.......tehy seein how close i was is kinda encouraging......i tried scanf(a) and scanf(b), don't guess ive read enough about that function yet, but why add '&#37;d'??.......i thought '%d' was just a placeholder for the printf() function.......haven't gotten to pointers yet so i would've never gotten the '&a'

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    It is not a placeholder - rather format specifier, that describes how printf and scanf functions should treat the list of their arguments...

    These formats are very similar but not the same for two groups of functions...

    http://msdn2.microsoft.com/en-us/lib...ch(VS.71).aspx
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 08:54 AM
  3. plz help me with simple string comparison.
    By MegaManZZ in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2008, 12:11 PM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM

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