Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Trouble with if statements

SyphentosSyphentos TowsonMember Posts: 0
edited October 2014 in Beginner C/C++

I need help figuring out this problem. I need to ask the user if they want the program to calculate Diameter, Circumference or Area and I also need the the user to input the radius after that. But after that I am having trouble making the program only display the relevant answer. If the user designates Diameter I only want to display the answer for diameter.

Here is the problem as it is on my lab sheet: Write code that calculates and prints the diameter, the circumference, or the area of a circle,
given the radius. The program inputs two data items. The first is a character—‘D’ (for diameter), ‘C’ (for
circumference), or ‘A’ (for area)—to indicate the calculation needed. The next data value is a floatingpoint
number indicating the radius of the particular circle. The program should print the input data. The
output should be labeled appropriately. For example, if the input is: A 6.75 your program should print
something like this:
The area of a circle with radius 6.75 is 143.14.

Here is what I have so far:

include

using namespace std;
int main()
{
char letter;
char D, C, A;
float Dia;
float Cir;
float Area;
float R;
float Pi;

cout << "What do you want to solve for? (D, C, A)" << endl;
cin >> letter;

cout << "What is the radius?" << endl;
cin >> R;

Pi = 3.14159265;
Dia = 2 * R;
Cir = 2 * Pi * R;
Area = Pi * pow(R,2);

if (letter == D) //error C4700 uninitialized local vairbale 'D' used
    cout << "The Diameter of the Circle is: " << Dia << endl;
if (letter == C) //error C4700 uninitialized local vairbale 'C' used
    cout << "The Circumference of the Circle is: " << Cir << endl;
if (letter == A) //error C4700 uninitialized local vairbale 'A' used
    cout << "The Area of the Circle is: " << Area << endl;

return 0;

}

Tagged:

Comments

  • pseudocoderpseudocoder Member Posts: 701 ✭✭✭

    since letter is of type char, you should be comparing it to a single character

    if(letter == 'D' || letter == 'd')
       ...
    else if(letter == 'C' || letter == 'c')
       ...
    else if(letter == 'A' || letter == 'a')
       ...
    else
       // invalid input
    

    I'm not sure why you've declared chars A, C, and D as you don't use them in your program; if you don't need them, you can remove their declarations.

    I'm not much into c++, but as I recall, it can do goofy stuff with floating point numbers. If you don't like the output, mill over here.

    to meet the program requirement, you need to show the user's radius in the output.

    HTH

Sign In or Register to comment.