Unable to update variables in class object (school project) [on hold]

This is for a beginning programming course.

The program is supposed to create a class to store information about different archery shots, then compare two shots to find the one with the closest distance to the bulls eye.

I've implemented the class and everything compiles, however my distance variable will always return 0. I printed the xVal and yVal after user input and it appears that they are not updating properly.

Any pointers toward my mistake will be much appreciated!

#include<iostream> #include<string> #include<cmath> using namespace std;  class Archer {public:  Archer();  void read();  bool better(Archer a) const;  void print() const;  private: string name; double xVal double yVal; double distance; };  Archer::Archer() {name = "none"; xVal=0; yVal=0; cout <<"x"<<xVal; cout <<"y"<<yVal; distance = sqrt(xVal * xVal + yVal * yVal); }  void Archer::read() {       cout << "Please enter the contestant's name:  ";         cin >> name;         cout << "Please enter their X Coordinate:  ";         cin >> xVal;         cout << "Please enter their Y Coordinate:  ";         cin >>yVal; }  bool Archer::better (Archer a) const {return (distance < a.distance); }  void Archer::print() const {cout  << "The winner is " << name << " with a distance of " << distance << " from the center!\n"; }  int main() { string again; Archer first; first.read();  bool more = true; while (more){ Archer next;         next.read();         if(first.better(next)){                 first=next;}         cout << "Is there another contestant (y/n)? ";         cin >> again;         if (again != "y"){                 more = false;} } first.print(); } 


Category: c# Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.852 (s). 13 q(s)