[Request] Help on a do/while loop

I'm trying to make a program that will allow a user to decide whether or not to repeat the program at the end. My program asks for user input, and based on what planet they pick, data values will be added to the associated data file. I want the user to be able to choose whether they get prompted to enter another planet, or to end the program. I'm having trouble with my do/while loop to achieve this.

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string.h>
#define PEarth 8.964
#define PMars 2.421
#define PMercury .269
#define PVenus 15.913
#define eEarth .0167
#define eMars .0934
#define eMercury .206
#define eVenus .00677
#define pi 3.1416
using namespace std;
int main()
{
    double A1, A2, Ai, e, r, x, y, theta,c;
    string P;
    do 
    {
           cout<<"Starting value of angle: ";
           cin>>A1;
           cout<<"Ending value of angle: ";
           cin>>A2;
           cout<<"Increment of angle: ";
           cin>>Ai;
           cout<<"Choose one planet (Earth, Mars, Mercury, or Venus): ";
           cin>>P;
           ofstream outfile;
           
             if(P=="Earth" || P=="earth")
             {
                     outfile.open("earth.dat");
                     for(theta=A1; theta<=A2; theta+=Ai)
                     {
                         r = (PEarth*eEarth)/(1-eEarth*cos((A2-A1)*(pi/180)));
                         x = r*cos(theta);
                         y = r*sin(theta); 
                         outfile<<x<<'\t'<<y<<endl;
                     }
                     outfile.close();
             } 
             
             if(P=="Mars" || P=="mars")
             {
                     outfile.open("mars.dat");
                     for (theta=A1; theta<=A2; theta+=Ai)
                     {
                         r=(PMars*eMars)/(1-eMars*cos((A2-A1)*(pi/180)));
                         x=r*cos(theta);
                         y=r*sin(theta); 
                         outfile<<x<<'\t'<<y<<endl;
                     }
                     outfile.close();
                     cout<<"Do you want to continue (Y/N)? ";
                     cin>>c;
             } 

             if(P=="Mercury" || P=="mercury")
             {
                     outfile.open("mercury.dat");
                     for (theta=A1; theta<=A2; theta+=Ai)
                     {
                         r=(PMercury*eMercury)/(1-eMercury*cos((A2-A1)*(pi/180)));
                         x=r*cos(theta);
                         y=r*sin(theta); 
                         outfile<<x<<'\t'<<y<<endl;
                     }
                     outfile.close();
                     cout<<"Do you want to continue (Y/N)? ";
                     cin>>c;
             } 
    
             if(P=="Venus" || P=="venus")
             {
                     outfile.open("venus.dat");
                     for (theta=A1; theta<=A2; theta+=Ai)
                     {
                         r=(PVenus*eVenus)/(1-eVenus*cos((A2-A1)*(pi/180)));
                         x=r*cos(theta);
                         y=r*sin(theta); 
                         outfile<<x<<'\t'<<y<<endl;
                     }
                     outfile.close();
                     cout<<"Do you want to continue (Y/N)? ";
                     cin>>c;
             }  
    }
    while(c=='y'||c=='Y');
    system ("pause");
    return 0;
}
Last edited on
You declare c as a double and then try to use it as a character. Declare it correctly and it should be fine.

Also, you shouldn't have the "Do you want to continue?" part at the end of each if statement; instead put it after all of them, right before the loop ends. No need to repeat the code.
Thank you very much! I knew it was something simple.
Topic archived. No new replies allowed.