Thread: New to C++ and need some help

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7

    New to C++ and need some help

    This is my first program ever and I am having some problems with it.

    http://pic9.picturetrail.com/VOL298/...4/82904752.jpg

    Here is what I have come up with so far.
    Code:
    #inlcude<iostream>
    #include<math.h>
    
    int main()
    {
    float i;
    double x, term, sum;
    
    cout <<"enter a value for x"<<endl;
    cin>>x;
    
    for(i= 0.1; i<=1.0; i=i+0.05)
    
    need more code
    
    return 0;
    }
    Thats basically as far as I get.

    In the examples I have they use
    Code:
    "double x,xi,xe,dx,term,sum,ex;"
    xi=0.1; xe=1.0; dx=0.05
    instead of how I did it above,
    then they specify
    Code:
    for(x=xi; x<=xe; x=x+dx)
    Is that the same as how I did it?

    Thanks for any help or suggestions in advance.
    Last edited by zombie; 01-27-2005 at 02:49 PM.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Add code tags.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Quote Originally Posted by Shakti
    Add code tags.
    how do you do that? I have never posted code before.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    http://cboard.cprogramming.com/showthread.php?t=13473
    And go read the announcment after you have read that.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    That better?

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Yah.

    Now, exactly what is it you have trouble with??

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    That better?
    Yes. But not perfect Don't forget to indent your code. One of the features od the code tags is preserving all your spacing.

    As for your question, they do appear to be the same.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Quote Originally Posted by sean_mackrory
    Yes. But not perfect Don't forget to indent your code. One of the features od the code tags is preserving all your spacing.

    As for your question, they do appear to be the same.
    oh I typed this up right on the forum and it wouldnt let me tab over.

    as for my question. I am having problems coming up with what code should come next.

    I was thinking it should be something along the lines of

    Code:
    term=term*(multiplication factor)
    sum=sum+0.05
    Also what code would be put to specify 6 decimal places? I used float i;

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    oh I typed this up right on the forum and it wouldnt let me tab over.
    You can't use tab in a most web browser's. And ideally you shouldn't copy and paste code that has tabs in it either. Use spaces (4 perhaps) instead.

    edit: There's a function you can use to set how many decimal places. It's something like setprecision(). It's a member function of I/O streams, so you'd say something like
    Code:
    cout.setprecision(6);
    But I'm not positive on the function name.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    the setpercision should be in the code before the function correct.
    Like put it in after
    Code:
     double x,term,sum;

  11. #11
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by zombie
    the setpercision should be in the code before the function correct.
    Like put it in after
    Code:
     double x,term,sum;
    Not neccesarily. You could print it out and such. Try printing it out with cout inside your for loop (if that's where you want it to print...of whatever you're trying to do here.) >_<

  12. #12
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    std::cout << setprecision(XX);

    Where XX is a number of how many decimals you want to go out to.

Popular pages Recent additions subscribe to a feed