Program Won't Compile

We are given the header file with the RMEs and we have to write the functions for the class in the cpp file. I posted my specific questions at the bottom of the thread. Any help would be greatly appreciated.

GIVEN HEADER FILE WITH RMEs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef POINT_H
#define POINT_H

#include <iostream>
using namespace std;


class Point
{
public:
    /**
     * Requires: Nothing.
     * Modifies: x, y.
     * Effects:  Default contructor. Sets point to origin (0,0).
     */
	Point();
    
    /**
     * Requires: Nothing.
     * Modifies: x, y.
     * Effects:  Constructs a point and sets x and y coordinates.
     * Note: you will want to implement the private
     *    member function checkRange() before
     *    implementing this one
     */
	Point(int xVal, int yVal);
    
    /**
     * Requires: Nothing.
     * Modifies: x.
     * Effects:  Sets x coordinate.
     */
	void setX(int xVal);

    /**
     * Requires: Nothing.
     * Modifies: Nothing.
     * Effects:  Returns x coordinate.
     */
	int getX();
    
    /**
     * Requires: Nothing.
     * Modifies: y.
     * Effects:  Sets y coordinate.
     */
	void setY(int yVal);
    
    /**
     * Requires: Nothing.
     * Modifies: Nothing.
     * Effects:  Returns y coordinate.
     */
	int getY();
   
    /**
     * Requires: ins is in good state.
     * Modifies: ins, x, y.
     * Effects:  Reads point in form (x,y).
     */
    void read(istream& ins);
    
    /**
     * Requires: outs is in good state.
     * Modifies: outs.
     * Effects:  Writes point in form (x,y).
     */
    void write(ostream& outs);

private:
    int x;
    int y;
    
    /**
     * Requires: nothing
     * Modifies: nothing
     * Effects: 
     * Effects:  Returns val if val is in range [0,DIMENSION),
     *           otherwise returns the closest of 0 and DIMENSION - 1.
     */
    int checkRange(int val);
	

/**
 * Overloading >> and << for reading a Point from streams.
 * Example on how to use these:
 * Point pt;
 * cout << "Please enter a point using format (x,y) : ";
 * cin >> pt;
 * cout << "\nthe point you just entered is: ";
 * cout << pt << endl;
 */
istream& operator >> (istream& ins, Point& pt);   
ostream& operator << (ostream& outs, Point pt); 

#endif 


CPP FILE WITH FUNCTIONS I WROTE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Point.h"

// for the declaration of DIMENSION
#include "utility.h"

// TODO: implement two constructors, setX, getX, setY, getY, read, write, checkRange.
Point::Point() { // Default constructor
	x = 0;
	y = 0;
	
}

void Point::setX(int xVal) {
	x = xVal;
}

int Point::getX() {
	return x;
}

void Point::setY(int yVal) {
	y = yVal;
}

int Point::getY() {
	return y;
}

void Point::read(istream& ins) {
	char junk;
	char junk2;
	char junk3;

ins >> junk >> x >> junk2 >> y >> junk3;
	
}

void Point::write(ostream& outs) {
	
		outs << "(" << x << "," << y << ")";
	
}

int Point::checkRange(int val){
if ((val >= 0) && (val < DIMENSION)) {
	return val;
}
else {
	if ((val - (DIMENSION - 1)) > val) {
		return 0;
	}
	else {
		return (DIMENSION - 1)
	}
}
}

Point::Point(int xVal, int yVal) { // Second constructor
	x = xVal;
	y = yVal;
}





// Your code goes above this line.
// Don't change the implementations below!

istream& operator >> (istream& ins,  Point& pt)
{
    pt.read(ins);
    return ins;
}

ostream& operator<< (ostream& outs, Point pt)
{
    pt.write(outs);
    return outs;
}

Last edited on
first of all, please use code tags. Edit your original post, highlight the content of each of the files, and click the Format button that looks like "<>". This will make it much easier for us to comment on your code.

My first question is why won't my program even compile
? Well that that could be any number of things, ranging from you had a power outage and you computer is not running to your forgot to give the compile command to you having a syntax error in your code. I suspect it's the 3rd.

When you ran your compiler and it failed to compile, did it print out error messages? Those messages would make it much easier to help you understand what the problem is.
I clicked the format button-I hope it worked.

I found the error-I missed a semicolon. I could have sworn I had them all. Anyways, ok my question is now though, did I format my constructors right? Did I write the "read" and "write" codes correctly?
Last edited on
closed account (E0p9LyTq)
NICE8x wrote:
I clicked the format button-I hope it worked.

It didn't.

How to add code tags to previously posted comments:
http://www.cplusplus.com/articles/jEywvCM9/
The format did not work. You need to edit you original post, highlight (click/drag your mouse) the text for each of the files and click the "<>" Format button. When done correctly, the code will look like this:

1
2
3
4
5
#include <iostream>
int main()
{
    std::cout "Hello World!" << std::endl;
}


You can also manually add "[ code ]" and "[/ code ]" without the spaces and you will get the same effect.

You can also click the Preview button to see if you got what you were expecting.

did I format my constructors right?
I only see 1 constructor. It looks right. You can also go the initialization list route:
Point::Point() : x(0), y(0) {}

Did I write the "read" and "write" codes correctly?
They look ok. Do they do what you expect them to do:
I also have an overloaded constructor as well-is it necessary to put it at the top of the code with the default constructor? Also do I need to use const with the get functions? I wasn't sure.
Do I need to call checkRange for the read function? I feel like the function isn't doing anything if I never call it. But if I called it should I use an if statement?
So apparently I need to call the checkRange function before each set/get.

Would the following work???

1
2
3
4
5
6
7

void Point::setX(int xVal) {
	if (checkRange(xVal) = xVal) {
		x = xVal;
	}
}


Or do I need to call the function using point because it's in the class?
I also have an overloaded constructor as well-is it necessary to put it at the top of the code with the default constructor?

From a syntax perspective, no. The code can go anywhere in the .cpp file. From a style perspective, sometimes its nice to have functions in the .cpp file in the same order as they are declared in the header file. I usually put all my constructors at the top of the .cpp file.

Also do I need to use const with the get functions? I wasn't sure.

It would make sense to make the function const (int getX() const;). This will allow you to get the value out of a const Point object if you need to because the get function does not modify the object.

Do I need to call checkRange for the read function? I feel like the function isn't doing anything if I never call it. But if I called it should I use an if statement?

The checkRange function is supposed to return a value. Use it. BTW, line 49 is messed up. You only need to check if val < 0.

So apparently I need to call the checkRange function before each set/get.


Why would you need to call checkRange before a get?

Your earlier question was about the read function. Do you think that is a set function? I think it is.

You want to call checkRange any time you are about to set an X or Y value because you want to make sure your data is always valid. Once it's valid, you can be sure that your writes and gets give you valid output.


Or do I need to call the function using point because it's in the class?

There is no point. You could use this-> if you wanted to, but I don't think you've learned about that yet. So, no, you can just use x like you have. But I would suggest you replace lines 3 - 5 with simply x = checkRange(xVal);



closed account (E0p9LyTq)
NICE8x wrote:
Also do I need to use const with the get functions?

You are not required to use const with functions that don't make any changes to values in the function, but it is a good idea.

https://isocpp.org/wiki/faq/const-correctness
Topic archived. No new replies allowed.