Jump to content

n00b question

- - - - -

  • Please log in to reply
3 replies to this topic

#1
bajerocks

bajerocks

    Newbie

  • Members
  • Pip
  • 5 posts
I am getting back to c c++ after a long time and not only that but was a noob when I left so I need this help. My main question is that, I have this code:

#include<iostream>

using namespace std;


class baseball{

	double bat_avg;

	public:

	int calcbatavg(double hitsnum, double at_bats){bat_avg=(hitsnum/at_bats);}

	int printavg(){return bat_avg;}

};


int main(){

	baseball calc;

	double bathits, atbats;

	cout << "Enter number of hits\n";

	cin >> bathits;

	cout << "Enter number of atbats\n";

	cin >> atbats;

	calc.calcbatavg(bathits, atbats);

	cout << "The batting average is: " << calc.printavg();

	system("pause");

	return 0;

}


now when I run this code I get all the results fine. But however the double type is not working. What I mean by that is the results are not showing decimal points, I tried this with float too but the same error. Ususally the answers are rounded. Any help would be appreciated thank you.:)

#2
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
Your variable bat_avg is a double indeed, but printavg returns an int, thus, the value is truncated.

#3
bajerocks

bajerocks

    Newbie

  • Members
  • Pip
  • 5 posts
how to return a double and thank you for your response. As far as I know I dont think I can make the function return double, can I?

EDIT: Yes I was able to return double sorry my bad. But one thing is for sure I have a lot to review that I lost. And Mods, please close this thread, thank you.

#4
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts

bajerocks said:

how to return a double and thank you for your response. As far as I know I dont think I can make the function return double, can I?

Going to classes without knowing what a function is, or anything else, is at least non-sense. You should read a tutorial from the beginning to understand the basics first.

Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users