Thread: tough question

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    11

    Question tough question

    Write a program that uses a user defined function called 'truncate()' which separates the integer and the decimal part from a float value.

    The prototype for this function is as follows :

    void truncate (float);

    The function accepts a float value from main () and truncates the decimal part of the variable


    plz help

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read this, in particular, the homework section.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Please don't just post your homework questions, at least try to solve them and be able to prove it or you won't get help here. I will offer suggestions to get you started though, try printing the float value cast to an int and see what you get.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    i tried. it was wrong, which is why i am here. so what, the rules are i cant be helped?

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    my output:

    Enter a decimal point number : 12.60

    After truncating the variable, the values are : 12.00 and 0.75

    Do you want to run the operation again [Y/N]? n


    Press any key to continue

    ------------

    wrong.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i tried. it was wrong, which is why i am here.
    Then you'll need to describe what the output should look like in all cases because this works just peachy for me and I can't see you needing to handle values that would overflow an int for homework:
    Code:
    void truncate ( float val )
    {
      std::cout<< (int)val <<std::endl;
    }
    >so what, the rules are i cant be helped?
    No, the rules are that you will not be helped if you don't bother trying to solve the problem on your own. We assume this to be the case if you do nothing but post the problem you were given and not any solutions you have tried that didn't work.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    check it out.


    //
    //
    //

    #include <iostream.h>

    void truncate (float);

    void main ()

    {

    int a;
    int b;
    float c;

    cout<<"enter your number"<<endl;
    cin>>c;

    cout<<"values are "<<a<<"and"<<b<<"<<endl;

    }

    void truncate (float)

    {

    int a= float c;
    int b=(c-a);

    return a,b;

    }

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Well, for one thing, where in main do you call truncate()?
    For another, how are you returning two values?
    For that matter, why does truncate() have a data type but not a data variable? etc...
    I'm not trying to pick on you, but you need to spend some time learning about functions and passing arguments; forget about actually splitting the integer and decimal parts for now.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Replies: 6
    Last Post: 05-01-2003, 02:25 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM