Keep getting Errors!

Pages: 12
Here is what I have to do:

Write a C++ program to "draw" ellipses by printing characters, to the following specifications:

Prompt the user for values for a and b, then draw an ellipse using the formula
(x2/a2)+(y2/b2)=1. Actually, since double values in C++ are rarely exact, use (x2/a2)+(y2/b2)-1.0<0.0001, but use a named constant in place of 0.0001.
Since there are 25 rows in a standard PuTTy screen, output 24 rows for the ellipse. For each row number row 0 to 23, let y be 12-row.
Since there are 80 columns in a standard PuTTy screen, output 80 characters in each line. Since each column is equivalent to about half a row in terms of space, for each column number col 0 to 79, let x be (40-col)/2.
Use pow from <cmath> to calculate the squares, ensuring that the two parameters are of type double.
After the ellipse, output information about it and prompt to repeat or not.
If it is a circle, output the word Circle, the radius of the circle (either a or b), the area of the circle and the number of characters printed in the "drawing", as in the first example.
If it is not a circle, output the word Ellipse, both a and b, the area of the ellips and the number of characters printed, as in the second example.
The area of an ellipse is Π * a * b. Use M_PI from <cmath> for the value of Π.
All floating point numbers should be output in fixed format, always showing the point and with a precision of 2 places after the point.
An example executable named project1 is available in the /share directory on the server.
As with project 1, upload a text file containing a cat of your program file, a compiler instruction and a sample run of your program to this dropbox.
You must include documentation at the top of the program with your name, the date, the course section, and a short description of the program. Every variable must have a comment explaining briefly what it is for.

Here is my Program:
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
#include<iostream>
#include<cmath>
using namespace std;
const double PI = M_PI;
const double ZERO = 0.0001;
int a, b; //variables for a and b
int Characters; //variable for the number of characters used to create the shape
int runProgram();
int main(){ //main method that uses a do-while loop to repeat the program if the user enters anything besides Y or y at the end.

char Continue;
   do{ runProgram();
    cout << "Would you like to run again? Enter Y to continue or N to quit." <<endl;
    cin >> Continue;
    }//End of do
    while((Continue!='n')||(Continue!='N'));
    } //end of main

int runProgram(){
    cout << "Enter a number for 'a' : "; //asks for input a
    cin >> a; //inputs number from keyboard to variable a
    cout << "Enter a number for 'b' : "; //asks for input b
    cin >> b; //inputs number from keyboard to variable b

  // drawEllipse(); //method to draw ellipse
  // circletest(); //method to output information about the ellipse
  // characters(); //method to output the number of characters that the ellipse used
  }//End of runprogram
 int drawEllipse(){ //method to draw an ellipse
 int y=0,x=0,row=0;
   for(y=0; y<12-row ; row++ ){
           for(x=0; x<80; x++){
              if((x*x)/(a*a)+(y*y)/(b*b)-1.0<ZERO);
                 cout << " " <<endl;else { cout << "@" <<endl;
  } //End of else
  }//End  of for
 }//End of for
}//End of draw ellipse

 int circletest(){ //tests to see if variable a=b
   if (a==b){
   cout << "Circle :: " << "radius = " << a << " :: area = " << PI*a*a << " :: " <<endl;
    } else {
   cout << "Ellipse :: " << "a = " << a << "b = " << b << " :: area = " << PI*a*a << " :: " <<endl;
    }//End of else
  }
   int characters(){
   cout << "Characters = " << Characters << endl;
   }
                       


everything checks out ok, but I keep getting one Error message, and I don't know what to do.

.cpp: In function 'int drawEllipse()':
.cpp:35: error: expected primary-expression before 'else'
.cpp:35: error: expected `;' before 'else'


Any Ideas?
remove the semicolon after the if() in drawEllipse(). Really a silly mistake, but found often.

Your if block does nothing. See that semi-colon underlined? That's the end of the if block. So the else is just hanging around on its own without an accompanying if.

Use braces and format your code so you can see what's happening.

Bad, because you can't see your own erroneous semi-colon:
1
2
if((x*x)/(a*a)+(y*y)/(b*b)-1.0<ZERO);
                 cout << " " <<endl;else { cout << "@" <<endl;


This layout makes the erroneous semi-colon easier to see:
1
2
3
4
5
6
7
8
if((x*x)/(a*a)+(y*y)/(b*b)-1.0<ZERO);
{
  cout << " " <<endl;
}
else
{ 
  cout << "@" <<endl;
}
Last edited on
OK so I fixed it..it compiles and no errors, but it will not draw the ellipse with @ . what could be the reason for that??..Did I mess up in the algorithm?
Check to see what your formula evaluates to? Is it always less than zero? Do you need additional parentheses?

Check to see what your formula evaluates to? Is it always less than zero? Do you need additional parentheses?

I can not spot where I am going wrong.
if((x*x)/(a*a)+(y*y)/(b*b)-1.0<ZERO) Add a breakpoint, then step through each iteration of the loop verifying that this equation is functioning as you would expect it to. If @ is not being printed, that must mean, with your currently posted code, that the if() is evaluating to true each iteration... see why that is?

if((x*x)/(a*a)+(y*y)/(b*b)-1.0<ZERO) Add a breakpoint, then step through each iteration of the loop verifying that this equation is functioning as you would expect it to. If @ is not being printed, that must mean, with your currently posted code, that the if() is evaluating to true each iteration... see why that is?


clanmjc, that is what I am saying, I can not spot it. Even if I did something wrong I am not sure if I will be able to fix it. I had too many problems and I fixed them, but for some reason, I can not fix this. Can you Be little more specific, and see where I am wrong, or doing something wrong?
hi trex123,

I am sure that clanmjc would like you to do some debugging using the debugger. That is what was meant by the :

Add a breakpoint, then step through each iteration of the loop verifying that this equation is functioning as you would expect it to.


If you don't wish to use the debugger, then put cout statements to print the values of variables at each stage. Personally it's easier to use the debugger.

Here is another clue: Do you have the right types for your variables? What happens with integer division ? If you change the type of the variables, this has implications for other tests ie equality, less than, greater than.

Some other observations:

drawEllipse & CircleTest aren't called from anywhere, so the program does nothing except for getting a & b. Maybe you forgot to uncomment them. Also you need to declare these functions before main, otherwise it won't compile.

const double PI = M_PI;

I wouldn't have bothered doing that, just use M_PI in your code.

1
2
}//End of do
    while((Continue!='n')||(Continue!='N'));


If you are going to use a do loop, put the while condition on the same line as the brace. Even though you have commented it, this has caused huge confusion in the past because it looks like a while loop with a null statement.

Try using a bool variable to achieve continuation.

cout << "Ellipse :: " << "a = " << a << "b = " << b << " :: area = " << PI*a*a << " :: " <<endl;

Is this the correct way to calc the area of an ellipse?

HTH

f((x*x)/(a*a)+(y*y)/(b*b)-1.0<ZERO);

I see there are a lot of int values in here.

An int divided by an int (for example, (x*x)/(a*a) and (y*y)/(b*b) will give an int. If a is larger than x, (x*x)/(a*a) is zero. If b is larger than y, (y*y)/(b*b) is zero. Have you allowed for this?

Thank You guys for your help and all..but now I am even more confused.
The stupid program still would not work. The more I try to Fix it the more I mess it up. Any one can actually write the part of the code that think I should fix. It will be much better for me to Understand. I am getting different kind of compiling errors each time now.
Please write the specific part of the code I should work on.
Thank You all so much,
Your help means a lot to me.
Nope, not going to write code for you, that's your job.

We have given some good clues. Post your new code, and the compile errors.
ok...so I started it all over again...turns out I was way out of line when I started writing this program...Made some changes..Now it does print out the @...only thing I have to do is make it come out in either circle or Ellipse..Hopefully someone can help me with this :

Here is my new 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
#include<iostream>
#include<cmath>

using namespace std;

double PI = M_PI;
const double ZERO = 0.0001;
int a,b;


int main() {

cout << "Enter the Value for 'a' : " << endl;
cin >> a;
cout << "Enter the Value for 'b' : " << endl;
cin >> b;


int drawEllipse();
int y=0, x=0, row=0, column=0;
for (y=0; y<12-row; row++) {
for (x=0; x<(40-column)/2; x++){
if(((x*x)/(a*a))+((y*y)/(b*b))-1<ZERO)
{
cout << "  " << endl;
}else
{
cout << "@" <<endl;
}
}
}

int circletest();
if (a==b){
cout << "Circle :: " << " radius = " << a << " :: area = " << PI*a*a <<  endl;
} else {
cout << "Ellipse :: " << " a = " << a << " b = " << b << " :: area = " << PI*a*b <<endl;
 }

char again;
do {
cout << "Would you like to do it again? (y/n)";
cin >> again;
} while (again=='y');
}


Ok after a head banging thought process, i wrote this and made it easier..
I am feeling dizzy [No lie- been staring at the screen for 2 and a half hrs..completely worth it].
The program compiles, I got my CircleTest going, the only problem I have right now is The damn Ellipse won't draw like an Ellipse. I get @'s printing, but not the way they should be printing. They should actually print like this:


                                        @
                                @@@@@@@@@@@@@@@@@
                            @@@@@@@@@@@@@@@@@@@@@@@@@
                          @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                       @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                       @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                          @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                            @@@@@@@@@@@@@@@@@@@@@@@@@
                                @@@@@@@@@@@@@@@@@
                                        @




Ellipse:

    @@@
                       @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
            @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
            @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                       @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                                       @@@




Please help!..
Thank you guys..
you are still using ints
dude...you will have to give me little more detail on this..I feel like an idiot right now...even after getting all these hints..i am not able to figure it out..It is pretty idiotic, but still you will have to give me a little bit of details..
I know i messed up somewhere in
int drawEllipse();
but where at?
Last edited on
Ok, so you already know that division by 0 is bad (I don't need to cover that) but what you might not have picked up on in the last several posts is that integer division in C++ isn't like what you'd expect. You don't get a remainder, and this is bad for a program like yours. When you want to do math, you want decimals because things aren't perfect. Once you get a decimal, you can use ceil from the math header file. Anyways, let's look at your options:

You can have the user input floats (or doubles) which would prevent integer division so you can have decimals return (this is what we want). So let's let the user enter floats.
float a,b; // Found at the top of the program

So far, no major changes, this is good. Let's see what else we can get in to. Actually, it looks like that's all you'll need to change...hmmm that was easy...try it and see if it works for you.
Last edited on
Actually, it looks like that's all you'll need to change

Uhhh..., OK..., I'll try not o be rude (not as easy as it sounds!!!), soo, NOOOOO!
Here's the output for a=2, b=2;



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@



@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@

Here's the output for a=3, b=5:




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@




@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@

And here's the output for a=0, b=0:
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@

What do you say?
I say that your equation is wrong and that you over thought this process. The correct equation is:
1
2
3
4
5
6
7
8
9
10
for (int row = 0; row < 24; row ++) {
      int y = 12 - row;
      for (int col = 0; col < 80; col ++) {
         int x = (40 - col) / 2;
         if (((x * x) / (a * a)) + ((y * y) / (b * b)) - 1 < ZERO)
            std::cout << "@";
         else
            std::cout << " ";
      }
   }


You're welcome.

P.S. be sure to keep a and b as floats, otherwise you get some malformed ellipse.

Edit: Don't forget that your teacher wants you to use the math header file to calculate the squares (Hint: This is the pow function).
Last edited on
ok..The equation is correct and thank you for that.
But the output is still messed up..I did change a and b as float
Everything is working correctly now. I hope this is what you wanted:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

int main() {
   int a = 19, b = 10;
   for (int row = 0; row < 24; row ++) {
      int y = 12 - row;
      for (int col = 0; col < 80; col ++) {
         // needs to be of type double to accurately get
         //    the position on the screen.
         double x = (40 - col) / 2.0;
         if (((pow(x,2) / pow(a,2)) + (pow(y,2) / pow(b,2)) - 1.0) < .0001)
            std::cout << "@";
         else
            std::cout << " ";
      }
   }
   return 0;
}



                                        @
                        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
             @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
             @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                                        @


Take note of the variable types. The only one that can't to be an int is x. This is because even dividing by the double 2.0 will sometimes return .5. This is used to round the ellipse evenly.
Pages: 12