Help with a do loop in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thesis99
    New Member
    • Mar 2008
    • 16

    Help with a do loop in c++

    I have an assignment for my class to create a table of volumes ina sphere. Starting with 5cm end w/50cm increments of 5. I feel I am very close but I can't get it to compile. here is what I have. Any help would be great.
    [code=cpp]
    #include <iostream>
    #include <iomanip>

    int main()
    {
    int i= 5; //radius of sphere
    float x= (4.);
    float b= (3.14);
    float c= (4.0/3.0);
    float y=0;// area
    float z=0;// volume

    {
    do

    cout<< setw (5) << i << setw (5) << y << set(5) << z <<endl;
    <snipped>
    }
    while (i<=50);
    cout<<"out of loop"<<endl;
    return(0);
    }
    [/code]
    Last edited by debasisdas; Mar 4 '08, 12:39 PM. Reason: added code=cpp tags
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Question moved to C / C++ forum.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Well, your opening brace of the loop is before your do statement. That makes no sense. Do what? The whole point of the braces is to enclose the body of the loop, so you are 'do'-ing that set of statements.

      Also, you are printing y and z before you get their values within the loop. Do your calculation first, then print them.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        You have forgotten to declare your namespace


        Also note we do not allow posting of complete code solutions to coursework questions so I have snipped a chunk from your code.

        Add 3.14 is a double constant but you are assigning to a variable of type float. If you are going to do floating point arithmetic the is no real reason on todays computers not to use a double types but if you are going to use floats then declare your constants with float type 3.14F

        Comment

        • thesis99
          New Member
          • Mar 2008
          • 16

          #5
          Im still lost,


          I have:
          [code=cpp]
          do
          {

          my equation

          cout<< setw (5) << i << setw (5) << y << set(5) << z <<endl;
          i+=5;
          }


          while(i<=50);
          [/code]
          MY COMPILIER DOES NOT LIKE THE COUT<<
          Last edited by sicarie; Mar 4 '08, 02:24 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Try typing "using namespace std;" right after your #include statements, or referring to cout as std::cout.

            Comment

            • thesis99
              New Member
              • Mar 2008
              • 16

              #7
              Originally posted by Ganon11
              Try typing "using namespace std;" right after your #include statements, or referring to cout as std::cout.
              THAT DOES NOT SEEM WORK. IM STILL GETTING THE COUT<< HIGHLIGHTED ON MY COMPILIER.

              THIS KIND OF THING DRIVES A BEGINNER MAD!!!!!

              Comment

              • thesis99
                New Member
                • Mar 2008
                • 16

                #8
                this is what i have:
                [code=cpp]
                #include <iostream>
                #include <iomanip>

                using namespace std;

                int main()

                {
                int i= 5; //radius of sphere
                float x= (4.);
                float b= (3.14F);
                float c= (4.0/3.0);
                float y=0;
                float z=0;


                do
                {
                <SNIPPED>
                y= i*(x*b)*2*2;
                z= i*(c*b)*3*3*3;
                cout<< setw (5) << i << setw (5) << y << set(5) << z <<endl;
                i+=5;

                }

                while(i<=50);



                cout<<"out of loop"<<endl;


                return(0);
                }[/code]
                Last edited by sicarie; Mar 4 '08, 02:54 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  What's snipped above the cout statement in the loop? What did you leave out?

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    thesis99-

                    Please check your Private Messages, accessible via the PMs link in the top right corner of your page.

                    Thanks

                    sicarie

                    Comment

                    • thesis99
                      New Member
                      • Mar 2008
                      • 16

                      #11
                      Originally posted by sicarie
                      What's snipped above the cout statement in the loop? What did you leave out?
                      jUST some calculations. Didn;t want to post the whole program. I was informed we are not to do that anymore.

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Can I see the two lines above that?

                        Comment

                        • thesis99
                          New Member
                          • Mar 2008
                          • 16

                          #13
                          Originally posted by sicarie
                          Can I see the two lines above that?
                          sure;
                          y= i*(x*b)*2*2;
                          z= i*(c*b)*3*3*3;

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by thesis99
                            sure;
                            y= i*(x*b)*2*2;
                            z= i*(c*b)*3*3*3;
                            Hmmm, try putting a space between 'cout' and '<<'.

                            Comment

                            • thesis99
                              New Member
                              • Mar 2008
                              • 16

                              #15
                              Originally posted by sicarie
                              Hmmm, try putting a space between 'cout' and '<<'.
                              That didn't work! Am I completley off track with the program.

                              Comment

                              Working...