Can't get what I want..

Hello everybody!
This is my first post here, and maybe it is the most stupid you've ever seen.
This is the problem:

#include <stdlib.h>
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS

class cerchio {

int ra;

public:
double area();
void getr(int);

};

double cerchio::area(){
const double pi = 3.141592654;
return (ra*ra*pi);
}

void cerchio::getr(int r){
ra = r;
}

int main() {

int r=0;
cerchio c;

printf("Inserisci il raggio del cerchio: \n");
scanf("%ld", &r);
c.getr(r);
c.area();
printf("Area: %ld", c.area());
getchar();
return 0;
}

I can't get the correct result!!
I've tried everything, but I can't understand where I'm wrong!
Thank you very much!
What is the problem? "I can't get the correct result!!" doesn't really help be debug your code...
Sorry guys..
What I meant was.. I get the first value using c.getr(r), then i pass that value to c.area and the result is returned and printed.
Debugging the code, it looks like everything is ok, but the result shown at the end is not correct.
i.e.: input: 5, then c.area returns 5*5*3.14..=78.54, but printf returns a wrong value (154145820).
If I set a breakpoint right before printf"Area: %ld", c.area());, the value shown for void cerchio::area is correct (78.54).
I hope this helps more.
code tags code tags code tags code tags code tags code tags code tags!!!!!!!!!!

return (ra*ra*pi); this will not give you double value but int,

1
2
c.area(); // this is plain weird, perhaps double a = c.area(); printf("Area: %f", a); ??? 
printf("Area: %ld", c.area());


and why use C?
Last edited on
You're the man..
Anyway, I thought I had already tried that solution (I spent more then an hour, trying different solutions, but always getting the same, wrong result..);

and why use C?

What did you mean with that??

I know that's a stupid, useless function, but i'm just getting started with c++.

(At work I use VBA).

Thx again!

#include <stdlib.h>
#include <stdio.h>
printf();
scanf();


Those are C headers and functions. In C++, we usually use #include <iostream> for I/O. But the C library is also included in C++ but with prefix C and no file extension.


#include <cstdlib>
#include <cstdio>


Read the tutorial in this website if want to learn more.. http://www.cplusplus.com/doc/tutorial/
In fact I'm learning how to use c++ reading that tutorial! :-) But it will take a little bit before I can manage everything well!
Thx a lot! :-)
Your area function is working correctly.

The problem is that you're printing the number as a integer instead of a floating point.

The %ld format tag only works with integers. For floating point you should use either use %g, %e, or %f:

 
printf("Area: %f", c.area());  // %f will work 


Or... since this is C++, just use cout instead of printf, then you don't have to worry about that kind of thing:

 
cout << "Area:  " << c.area();
OK!
One more thing, and then I think I can go on with it.

#include <cstdlib>
#include <cstdio>
#include <string>

using namespace std;

void main() {
string str ("Hello world!");
printf ("%s" ,str);
getchar();
return;
}


What's wrong with this??
Instead of printing "Hello world", it prints "_#รค", or something different everytime I run the exe.
Using cout, it works obviously, but I'd like to understand what's wrong with it, and if it's correct to use this function (I'm getting mad learning all these function (fprint, fprintf, fscanf............))!!
Thx in advance!
Damiano.
You're mixing C and C++ in a way they're not supposed to be mixed. %s works with char arrays, not string objects.

Make this simple and just don't use printf. Use cout instead:

1
2
3
4
5
int main()  // note:  main returns an int, not void
{
  string str("Hello world!");
  cout << str;  // this works
}



Or if you really want to stick with printf for whatever reason, you can get it to work with printf by calling the c_str function:

1
2
3
4
5
int main()
{
  string str("Hello world!");
  printf("%s",str.c_str());
}
I don't really want to mix them.
So, cin >> for input and cout << for output. Got it.
Sometimes I see void main, sometimes int main..
I don't know what's the difference between, but I believe that what you're telling me is right.
So from now on it will always be int main! :-)
Thank you very much!! :-)
Topic archived. No new replies allowed.