Author Topic: Problem using if command....Please help  (Read 3461 times)

Offline DevilDemon

  • Single posting newcomer
  • *
  • Posts: 2
Problem using if command....Please help
« on: May 22, 2015, 08:53:31 am »
I am 11 years old and I have just started to learn C++ and I really don't know much about it. I was making a small program to calculate the area and circumference of the circle. I wanted to give user the option to choose which one they want to calculate. So I typed the following command.....

#include<iostream>
#include<conio.h>
int main()
{
 float r;
 float area;
 float cir;
 char n;
 std::cout<<"If you want to calculate the circumference of the circle, type 1 OR if you want to calculate the area, type 2: ";
 std::cin>>n;
 if(n=1)
 {
     std::cout<<"Enter the radius of the circle: ";
     std::cin>>r;
     cir=2*3.14*r;
     std::cout<<"The circumference of the circle is: "<<cir;
     getch();
     return 0;
 }
 if(n=2)
 {
     std::cout<<"Enter the radius of the circle: ";
     std::cin>>r;
     area=3.14*r*r;
     std::cout<<"The area of the circle is: "<<area;
     getch();
     return 0;

 }
}

I compiled it and it showed no errors. But when I ran it, it only calculated the circumference no matter what I type. Any help will be appreciated.


Offline raynebc

  • Almost regular
  • **
  • Posts: 217
Re: Problem using if command....Please help
« Reply #1 on: May 22, 2015, 10:26:21 am »
This forum isn't meant for programming help, but the issue is that when you use "if(n=1)" you're not testing the value of n, you're assigning it the value of 1 and the condition counts as true.  For this reason it runs that if statement's code, including the return statement that prevents the area calculation code from being reached.  You'll want to use "if(n==1)" and "if(n==2)" instead.

Offline DevilDemon

  • Single posting newcomer
  • *
  • Posts: 2
Re: Problem using if command....Please help
« Reply #2 on: May 22, 2015, 02:08:37 pm »
This forum isn't meant for programming help, but the issue is that when you use "if(n=1)" you're not testing the value of n, you're assigning it the value of 1 and the condition counts as true.  For this reason it runs that if statement's code, including the return statement that prevents the area calculation code from being reached.  You'll want to use "if(n==1)" and "if(n==2)" instead.

I changed the "if(n=1)" with "if(n==1)" and "if(n=2)" with "if(n==2)", now when I type 1 or 2 it does not run the code at all it shows that the process returned zero and ends the program. PLEASE HELP.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Problem using if command....Please help
« Reply #3 on: May 22, 2015, 06:33:55 pm »
This forum isn't meant for programming help, but the issue is that when you use "if(n=1)" you're not testing the value of n, you're assigning it the value of 1 and the condition counts as true.  For this reason it runs that if statement's code, including the return statement that prevents the area calculation code from being reached.  You'll want to use "if(n==1)" and "if(n==2)" instead.

I changed the "if(n=1)" with "if(n==1)" and "if(n=2)" with "if(n==2)", now when I type 1 or 2 it does not run the code at all it shows that the process returned zero and ends the program. PLEASE HELP.
This forum isn't meant for programming help ...

Please respect the forum rules !!