If problem

I am writing a cylinder calculator to determine volume and surface area, but I am getting a problem with an if statement.

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
/* Cylinder calculation program that finds the volume and surface area
when given the diameter, the height, and value of pi. */
//Edit made, end block added

#include <iostream>
#include <cstdlib>
using namespace std;
int main();

int end() {
    char cont;
    cout << "\n" << "Rerun cylcalc? y or n" << "\n";
    cin >> cont;
    if(cont='y'){
                  system("cls");
                  main(); }
        else if(cont='n'){
                  return 0;}                
        else {
             cout << "\n" << "Not understood, use 'y' or 'n'";
             return end(); }}
             
int main() {
    double di, hi, pi, sa, vo, ra, ci;
    // diameter
    cout << "Diameter: ";
    cin >> di;
    cout << "\n" << "Height: ";
    cin >> hi;
    // pi
    cout << "\n" << "Pi: ";
    cin >> pi;
    ra=di/2;
    ci=ra*ra*pi;
    // volume
    vo=ci*hi;
    cout << "\n" << "Volume= " << vo;
    // surface area
    sa=di*pi*hi+ci*2;
    cout << "\n" << "Surface Area= " << sa << "\n";
    system("pause");
    return end();}


The if in function end() returns main() if given y, but it does that even if given n or an invalid character. It should end the program if given n, and restart end() if given an invalid character. What do I need to change to fix this?
if(cont='y'){

You want ==.
== is comparison
= is assignment
firedraco, thanks for the help, it worked!
Last edited on
What about the call to main(). Very bad news.

The if in function end() returns main()


You need a return statement instead of calling main()

this bit just return control-flow to main, not what you want I think.

1
2
else if(cont='n'){
                  return 0;} 


Some confusion here too.

return end();}
Topic archived. No new replies allowed.