0

[I want to calculate the space of the circle but after i enter the value of r the value of x become 0]

(https://i.stack.imgur.com/BjgpH.png) (https://i.stack.imgur.com/LaRWI.png)

I did not try anything since I am a beginner

nour
  • 17
  • 3
  • After you enter the value of `r`, `x` was already `0` and hasn't been changed after entering the value of `r`. – Eljay Dec 01 '22 at 16:49
  • 2
    Your code is using the wrong approach, you need to write **functions** not namespaces. Since you are a beginner you need to read a [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282) that will explain the basics of the language. You cannot learn C++ by asking questions here. – john Dec 01 '22 at 16:50

2 Answers2

2

This is not the proper usage to namespace. We use such language feature in case we want to generate a space with our own functions and variables, similar to classes. I recommend you to create a function with desired behaviour.

Bruno Peixoto
  • 211
  • 1
  • 4
  • 17
1

you are not returning anything in your namespace.. check this code:

// Creating namespaces
#include <iostream>
using namespace std;

namespace mc {
const float b = 3.14;
float value(int r) { 
    return b*r*r; 
    }
}
 
int main()
{
    int r;
    cin>>r;
    cout << mc::value(2) << '\n';
    return 0;
}

Please mark this answer correct if you are satisfied with this.

Masoom Raza
  • 125
  • 1
  • 17