CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: C++ while loop

  1. #1
    Join Date
    Oct 2010
    Posts
    13

    C++ while loop

    Ok so Im trying to make the user enter an angle and if the angle is greater than 360 im trying to scale it back down by - 360, if its less than 0 I want it to + 360. However currently I cant even get it to subtract, It keeps giving me garbage output and when I try to pass it by refrence by adding a & in front of angle the compiler gives me an error

    Code:
    #include <iostream>
    #include <cmath>
    #include <string>
    using namespace std;
    
    int main(void)
    {
        double angle = 0;
    	cout << "Please enter angle : " << endl;
    	cin >> angle;
    	
    	while (360 < angle) {
    	angle = angle - 360;
    	cout << angle;
    	}
    	
    	
    	
    
    	
    		 
    
    return 0;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ while loop

    The indentation needs a little work, but other than that it looks okay (though you probably want to print the angle after the while loop. The "garbage output" might be because you are misinterpreting the correct output printed from within the loop.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2007
    Posts
    35

    Re: C++ while loop

    Instead of trying to fix invalid input, it's generally better to give an error message and quit. It's not sensible to do a calculation on 470 degrees that's really 110 degrees. It just causes confusion.

    Code:
    int angle = 0;
    	cout << "Please enter an angle in the range 1 to 359 : " << endl;
    	cin >> angle;
          if(angle < 1 || angle > 359)
             throw "input out of bounds" // or  cout << "input out of bounds : exiting" << endl

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ while loop

    Perhaps the fact that you're outputting multiple doubles in the loop without any spaces or newlines between them could be confusing you?

  5. #5
    Join Date
    Oct 2010
    Posts
    13

    Re: C++ while loop

    Well here is the real issue I'm having I was just testing that out. I need to make a program that takes 1/10 of the max time all the way up till the max time. It seems to be working but the numbers just really don't add up

    Code:
    #include <iostream>
    #include <cmath>
    #include <string>
    using namespace std;
    const double g = 9.8;
    double readdouble (string prompt);
    double distance (double v, double angle, double g);
    double ttime (double distance, double v, double angle);
    double height (double v, double angle, double time, double g); 
    int main(void)
    {
        double v, angle, dis, total, time,h, i;
        cout << "Please enter the velocity " << endl;
    	cin >> v;
    	
    	cout << "Please enter an angle between 0 - 360 degrees " << endl;
    	cin >> angle;
        
    	    
        dis = distance(v, angle, g);
        total = ttime(dis, v, angle);
        time = total/2;
        h = height (v,angle,time,g);
        cout << "The distance is: " << dis << endl
             << "The total time is: " << total << endl
             << "The height is: " << h << endl;
    
    while (i >= time) {
    	cout << angle << endl;
    	angle = angle * .1;
    	}
    
    return 0;
    }
    double distance (double v, double angle, double g)
    {
        return (pow(v, 2.0) / g) * sin(angle);
    }   
    double ttime (double distance, double v, double angle)
    {
        return distance/(v*cos(angle));
    }
    double height (double v, double angle, double time, double g)
    {
        return v * (sin(angle)) * time - (1/2) * g * pow(time,2.0);
    }
    double readdouble (string prompt)
    {
        double rv=0.0;
        cout << prompt;
        cin >> rv;
        while (cin.fail() != 0)
        {
            cin.clear();
            cin.ignore(INT_MAX, '\n');
            cout << "Error" << endl;
            cout << prompt;
            cin >> rv;
            return rv;
            }
        }
    Last edited by akaicewolf; October 29th, 2010 at 12:40 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured