Options and Conditions??

Hi, all. I'm new to the forum, and even newer to C++. I'm working on a homework assignment and I've literally been baffled by it for the past three days. I can't quite figure out what exactly I'm doing wrong. My assignment is as follows:

1) Prompt the user to select a shape (1 - square, 0 - circle), then based on the user selection calculate the
Perimeter and Area.
2) Write the logic first to work on one instance - the user is prompted once. Once the code is working
correctly, adjust it such that the user is prompted to repeat the process again (if they’re interested).

and this is what I've come up with so far. It runs successfully, where it computes the area and circumference for the circle, however it won't do the same for the square. Could someone please shed some light on this situation for me? THanks !!

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
  #include <iostream>
#include <math.h>
using namespace std;

int main()

{
    
    double radius, length, circumference, perimeter, area;
    int number;
    
    // Prompt user to select a shape.
    cout << "First, select a shape. Enter 0 for circle, or 1 for square.";
    cin >> number;
    
    if (number==0)
        cout << "You've selected circle. Great choice! Enter the radius, and the area and circumference will be computed for you.";
    do
    {
        cin >> radius;
        area = radius * radius * 3.14159;
        circumference = 2 * 3.14159 * radius;
    
    
    cout << "The area is " << area << " and the circumference is " << circumference << ".";
    
    }
        while (number==0);
    
    if (number==1)
        cout << "You've selected square. Great choice! Enter the length, and the area and perimeter will be computed for you.";

    do
    {
        cin >> length;
        area = length * length;
        perimeter = length * 4;
    }
        while (number==1);
    
        
    
    return 0;
}
however it won't do the same for the square


that's simply because you did not have any statements to output the area and perimeter


You don't really need do - while and some if statements inside your if statements ?

you could have just written:
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
 #include <iostream>
#include <math.h>
using namespace std;

int main()

{
    
    double radius, length, circumference, perimeter, area;
    int number;

    // Prompt user to select a shape.
    cout << "First, select a shape. Enter 0 for circle, or 1 for square.";
    cin >> number;
    
    if (number==0) {
        cout << "You've selected circle. Great choice! Enter the radius, and the area and circumference will be computed for you.";
        /* do while loop is useless here */
        cin >> radius;
        area = radius * radius * 3.14159;
        circumference = 2 * 3.14159 * radius;
    
        cout << "The area is " << area << " and the circumference is " << circumference << ".";
    }
    else if (number==1) { // use else if to clarify the condition
        cout << "You've selected square. Great choice! Enter the length, and the area and perimeter will be computed for you.";

        /* do = while loop is useless here */
        cin >> length;
        area = length * length;
        perimeter = length * 4;
     
        cout << "The area is " << area << " and the perimeter is " << perimeter << ".";
    
    }
        
      
    return 0;
}


as the instruction says once the code is working you must modify it to ask if the user wants to repeat the process again -> then in this situation, do - while loop must be useful:

implement this to your program:
1
2
3
4
5
6
7
8
9
do {

/* your code */


/* at the end of the program you might want to prompt the user if 
    he wants to try again, then you might want an additional variable to hold the input of the user and
   use that condition to either exit or repeat the loop */
} while ( condition ); // sample:  } while ( userInput != 'n' ); 
Your if statements and the code that does work are not connected. Remember, just because something comes after an if statement doesn't mean that it is conditional; rather, it must be the command attached to the if statement:

1
2
3
4
5
6
7
8
if (number==0)
{
  // do a whole bunch of stuff here
}
else if (number==1)
{
  // do a whole bunch of other stuff here
}

Also, those do loops should be causing you to see infinite output.

The reason you don't think anything is happening if you select 'square' is because you don't print the area and circumference anywhere in lines 35-37.

Notice how you printed those things when you calculated them for the circle, lines 20-26?

Hope this helps.
Ha ! Thank you both so much !! I can't believe I missed something so simple LOL ! I've gotten it fixed, and I managed to have the program repeat itself upon the user's request. I couldn't be any more grateful for your help !!
Topic archived. No new replies allowed.