Results 1 to 10 of 10

Thread: Beginner needs help with C

  1. #1
    Member
    Join Date
    Jan 2004
    Location
    Ontario, Canada
    Posts
    77
    Thanks
    0
    Thanked
    0 times in 0 posts

    Beginner needs help with C

    I just started learning C and I have a question about float...

    i made a simple multiplying program:

    #include<stdio.h>
    #include<stdlib.h>

    main()
    {
    float x;
    float y;
    float answer;
    char xchar;
    char ychar;

    printf("Enter a number:\n");
    gets(xchar);
    x=atoi(xchar);
    printf("Enter a second number\n");
    gets(ychar);
    y=atoi(ychar);
    answer=x*y;
    printf("%f x %f = %f\n",x,y,answer);

    }

    When I try to muliply decimal numbers, it will give me the answer but if the answer if supose to have a decimal point, it will only give the numbers that come before the decimal. like 5.33 x 2 will equal 10 and not 10.66
    AMD Athlon XP 2000+ @ 2.4GHz
    Shuttle AN35N Ultra
    1.5GB DDR SD-RAM
    ATI Radeon 9200 128MB
    20GB Seagate Barracuda
    52X CD-ROM

    Total SETI@Home CPU time: 856 hr 50 min

  2. #2
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Its because you use atoi (ascii to integer), try using atof instead.

  3. #3
    Member
    Join Date
    Jan 2004
    Location
    Ontario, Canada
    Posts
    77
    Thanks
    0
    Thanked
    0 times in 0 posts
    Thanks!
    AMD Athlon XP 2000+ @ 2.4GHz
    Shuttle AN35N Ultra
    1.5GB DDR SD-RAM
    ATI Radeon 9200 128MB
    20GB Seagate Barracuda
    52X CD-ROM

    Total SETI@Home CPU time: 856 hr 50 min

  4. #4
    Member
    Join Date
    Sep 2003
    Location
    Nottingham, UK
    Posts
    152
    Thanks
    0
    Thanked
    0 times in 0 posts
    I suggest using "scanf" instead of "gets".

    Code:
    scanf("%s", &xchar)
    scanf basically waits for the user to input the data, and once "enter" is pressed it stores the data into the variable "xchar".

    Code:
    printf("Enter a number: ");
    scanf("%s", &xchar);
    It's just personal opinion I guess, but I just prefer to use printf and scanf
    AMD Athlon XP 2400+ | Connect3D Radeon 9600PRO | Seagate Barrcuda 80GB HDD | 512mb PC2700 Crucial RAM | MSI KT6 Delta FIS2R | Zorro Silver Case | Windows XP/Gentoo Linux

  5. #5
    Member
    Join Date
    Jan 2004
    Location
    Ontario, Canada
    Posts
    77
    Thanks
    0
    Thanked
    0 times in 0 posts
    What's the difference between the two methods? I just got 'gets' from a tutorial that I was reading.
    AMD Athlon XP 2000+ @ 2.4GHz
    Shuttle AN35N Ultra
    1.5GB DDR SD-RAM
    ATI Radeon 9200 128MB
    20GB Seagate Barracuda
    52X CD-ROM

    Total SETI@Home CPU time: 856 hr 50 min

  6. #6
    Member
    Join Date
    Sep 2003
    Location
    Nottingham, UK
    Posts
    152
    Thanks
    0
    Thanked
    0 times in 0 posts
    I'm not sure on the actual difference, it's just I prefer the printf and scanf method, they are easier to remember, and the format is generally the same
    AMD Athlon XP 2400+ | Connect3D Radeon 9600PRO | Seagate Barrcuda 80GB HDD | 512mb PC2700 Crucial RAM | MSI KT6 Delta FIS2R | Zorro Silver Case | Windows XP/Gentoo Linux

  7. #7
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    I think that printf and scanf are better methods too... Again, personal preference. I think that they are buffered input/output streams. hence the "enter" is the newline character, and the "SUBMIT". So the I/O methods are closer to the way "proper" command line apps work i.e. telnet...

    A word of advice... It is probably best to develop a 'library' of input checking functions that you can use forever. I suggest two functions.... 1) getCleanString(question) & 2) getCleanInt(question) or getCleanInt(question, start, end)

    where "question" is a text question that is needed to be asked i.e. "Please enter a number (1-10)" and start = 1 and end = 10, so you can accept input only between a certain range. This way you can verify you user input always and prevent programs from crashing. Sorry. Went a bit off topic I think.

  8. #8
    Member
    Join Date
    Jan 2004
    Location
    Ontario, Canada
    Posts
    77
    Thanks
    0
    Thanked
    0 times in 0 posts
    I didn't fully understand what you just said How do I use the scanf? I think I'll start using it.
    AMD Athlon XP 2000+ @ 2.4GHz
    Shuttle AN35N Ultra
    1.5GB DDR SD-RAM
    ATI Radeon 9200 128MB
    20GB Seagate Barracuda
    52X CD-ROM

    Total SETI@Home CPU time: 856 hr 50 min

  9. #9
    Member
    Join Date
    Jan 2004
    Location
    Ontario, Canada
    Posts
    77
    Thanks
    0
    Thanked
    0 times in 0 posts
    Ohh wait I think I get it. %s meaning 'string' right?
    AMD Athlon XP 2000+ @ 2.4GHz
    Shuttle AN35N Ultra
    1.5GB DDR SD-RAM
    ATI Radeon 9200 128MB
    20GB Seagate Barracuda
    52X CD-ROM

    Total SETI@Home CPU time: 856 hr 50 min

  10. #10
    Member
    Join Date
    Sep 2003
    Location
    Nottingham, UK
    Posts
    152
    Thanks
    0
    Thanked
    0 times in 0 posts
    Yup scanf uses your usual printf formatting identifiers.

    Printf and Scanf are useful in file I/O aswell.
    AMD Athlon XP 2400+ | Connect3D Radeon 9600PRO | Seagate Barrcuda 80GB HDD | 512mb PC2700 Crucial RAM | MSI KT6 Delta FIS2R | Zorro Silver Case | Windows XP/Gentoo Linux

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. yep, another beginner in need of your expertise !
    By redeyebriggs in forum PC Hardware and Components
    Replies: 7
    Last Post: 10-10-2003, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •