"Variable declared but never referenced"

Hello i wrote this program but i got an error message for line 6:


#include <iostream>
using namespace std;
int main()
{
int r;
float pi=3.14159;
cout << "Type the radius.";
cin >> r;
cout << "The circumference is "; << 2*pi*r;
return 0;
}


but i get this error message:
"ComeauTest.c", line 6: warning: variable "pi" was declared but never referenced
double pi=3.13159;

How do you correct that?

thanks
Last edited on
It is not an error it is a warning that you may ignore. As you are not going to change the value of pi I advice to declare it as

const float pi=3.13159;


I am sorry. I have not seen the semicolon after string literal "The circumference is " in statement

cout << "The circumference is "; << 2*pi*r;

You shall remove it.:)

It seems that it is a bug of the compiler. It shall issue an error instead of warning.:)
Last edited on
Ok, now i get it... Thanks for the help!
Last edited on
Please reread my previous message. I have updated it.
closed account (zb0S216C)
Doppler wrote:
cout << "The circumference is "; << 2*pi*r;

This line has two statements, not 1. The first statement outputs "The circumference is". The second statement doesn't affect std::cout. The second statement is an error. However, the expression 2*pi*r doesn't affect program flow, and therefore, the compiler will remove it; thus, pi is never used, because the only expression that references pi was optimised away.

Wazzak
Last edited on
Topic archived. No new replies allowed.