Massive Noob question

This is going to be a Massive Noob question,but im trying to output to a file but im having trouble.Things to state:

yes,ive posted this code before
yes,i am horrible at programming

Main program:
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include<fstream>
#include "location.h"

ofstream outf("f:results-Output.txt", ios::out);
ifstream inf("f:results-Input.txt", ios::in);


int main()
{
    
    location x,y(1),z(1,2);
    
    std::cout<<"testing constructor"<<std::endl;
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"testing constructor"<<std::endl;
    outf<<"x="<<x<<std::endl;
    outf<<"y="<<y<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    
    std::cout<<"testing assign function"<<std::endl;
    outf<<"testing assign function"<<std::endl;
    x.assign(3,7);
    y.assign(4,3);
    std::cout<<"Enter a location for z (r,c)";
    outf<<"Enter a location for z (r,c)";
    std::cin>>z;
    
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"x="<<x<<std::endl;
    outf<<"y="<<y<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    std::cout<<"testing == function"<<std::endl;
    outf<<"testing == function"<<std::endl;
    if (x==y)
       std::cout<<"these points are equal"<<std::endl;
       outf<<"these points are equal"<<std::endl;
    else
       std::cout<<"these points are not equal"<<std::endl;
       outf<<"these points are not equal"<<std::endl;
    
   z=y;
    
    std::cout<<"testing = function"<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"testing = function"<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    std::cout<<"testing WithinRectangle Function"<<std::endl;  
    outf<<"testing WithinRectangle Function"<<std::endl;  
    x.WithinRectangle(1,1);
    y.WithinRectangle(10,10);
    std::cout<<"the top left point is "<<x<<std::endl;
    std::cout<<"The Bottom right point is "<<y<<std::endl;
    outf<<"the top left point is "<<x<<std::endl;
    outf<<"The Bottom right point is "<<y<<std::endl;


   
    
    x=x.Row();
    y=y.Row();
    z=z.Row();
    
    std::cout<<"testing row function"<<std::endl;   
    std::cout<<"x row="<<x<<std::endl; 
    std::cout<<"y row="<<y<<std::endl;
    std::cout<<"z row="<<z<<std::endl;    
    outf<<"testing row function"<<std::endl;   
    outf<<"x row="<<x<<std::endl; 
    outf<<"y row="<<y<<std::endl;
    outf<<"z row="<<z<<std::endl;    
   
   
   x.assign(3,7);
   y.assign(4,3);
   z.assign(4,3);
    
    x=x.Colm();
    y=y.Colm();
    z=z.Colm(); 
    
    std::cout<<"testing colm function"<<std::endl;   
    std::cout<<"x column="<<x<<std::endl; 
    std::cout<<"y column="<<y<<std::endl;
    std::cout<<"z column="<<z<<std::endl;  
    outf<<"testing colm function"<<std::endl;   
    outf<<"x column="<<x<<std::endl; 
    outf<<"y column="<<y<<std::endl;
    outf<<"z column="<<z<<std::endl;  
    
    
 
    x.assign(3,7);
    y.assign(4,3);
    z.assign(4,3);
   
    x.inc(1,2);
    y.inc(1,2);
    z.inc(1,2);   
    
    std::cout<<"testing inc function"<<std::endl;
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"testing inc function"<<std::endl;
    outf<<"x="<<x<<std::endl;
    outf<<"y="<<y<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    system("pause");
    return 0;
}  


I dont think you need it but here is the implementation and header
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
#include "location.h"


location::location(int r,int c)
{
           row=r;
           colm=c;
}
void location::assign(int r,int c)
{
           row=r;
           colm=c;
}
location location::operator = (location y)
{
         row = y.row;
         colm = y.colm;
         return y;
}
bool  location::operator == (location z)
{
     return((row==z.row)&&(colm==z.colm));
}
void location::WithinRectangle(int r, int c)
{
     row=r;
     colm=c;
}

int location::inc(int a, int b)
{
    row+=a;
    colm+=b;
    return ((row)&&(colm));
}    
std::istream& operator>>(std::istream &input , location &x)
{
         char a;
         input>>a
               >>x.row
               >>a
               >>x.colm
               >>a;
         return input;
} 
std::ostream& operator<<(std::ostream &output ,  location &x)
{
         output<<'('
               <<x.row
               <<','
               <<x.colm
               <<')';
         return output;
}


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
#ifndef location_H
#define location_H
#include <iostream>
class location
{
  
   friend std::istream& operator>>(std::istream & , location &);
   friend std::ostream& operator<<(std::ostream & , location &);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             void WithinRectangle(int, int);
             location operator =(location);
             bool operator ==(location);
             int inc(int, int);
             int Row(){return row;};
             int Colm(){return colm;}; 

};
#endif 


errors:
5 F:\testlocation.cpp `ofstream' does not name a type
6 F:\testlocation.cpp `ifstream' does not name a type
18 F:\testlocation.cpp `outf' undeclared (first use this function)

if someone can help me with this, id appreciate it.
Last edited on
Line 5 and 6
1
2
ofstream outf("f:results-Output.txt", ios::out);
ifstream inf("f:results-Input.txt", ios::in);


shud be:
1
2
std::ofstream outf("f:results-Output.txt", ios::out);
std::ifstream inf("f:results-Input.txt", ios::in);
ok changed that. Then i got errors for IOS and in and out not being part of the scope so i added the std to the IOS/in and out. now i got this:

44 F:\testlocation.cpp expected primary-expression before "else"
44 F:\testlocation.cpp expected `;' before "else"

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include<fstream>
#include "location.h"

std::ofstream outf("f:results-Output.txt", std::ios::out);
std::ifstream inf("f:results-Input.txt", std::ios::in);


int main()
{
    
    location x,y(1),z(1,2);
    
    std::cout<<"testing constructor"<<std::endl;
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"testing constructor"<<std::endl;
    outf<<"x="<<x<<std::endl;
    outf<<"y="<<y<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    
    std::cout<<"testing assign function"<<std::endl;
    outf<<"testing assign function"<<std::endl;
    x.assign(3,7);
    y.assign(4,3);
    std::cout<<"Enter a location for z (r,c)";
    outf<<"Enter a location for z (r,c)";
    std::cin>>z;
    
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"x="<<x<<std::endl;
    outf<<"y="<<y<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    std::cout<<"testing == function"<<std::endl;
    outf<<"testing == function"<<std::endl;
    if (x==y)
       std::cout<<"these points are equal"<<std::endl;
       outf<<"these points are equal"<<std::endl;
    else
       std::cout<<"these points are not equal"<<std::endl;
       outf<<"these points are not equal"<<std::endl;
    
   z=y;
    
    std::cout<<"testing = function"<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"testing = function"<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    std::cout<<"testing WithinRectangle Function"<<std::endl;  
    outf<<"testing WithinRectangle Function"<<std::endl;  
    x.WithinRectangle(1,1);
    y.WithinRectangle(10,10);
    std::cout<<"the top left point is "<<x<<std::endl;
    std::cout<<"The Bottom right point is "<<y<<std::endl;
    outf<<"the top left point is "<<x<<std::endl;
    outf<<"The Bottom right point is "<<y<<std::endl;


   
    
    x=x.Row();
    y=y.Row();
    z=z.Row();
    
    std::cout<<"testing row function"<<std::endl;   
    std::cout<<"x row="<<x<<std::endl; 
    std::cout<<"y row="<<y<<std::endl;
    std::cout<<"z row="<<z<<std::endl;    
    outf<<"testing row function"<<std::endl;   
    outf<<"x row="<<x<<std::endl; 
    outf<<"y row="<<y<<std::endl;
    outf<<"z row="<<z<<std::endl;    
   
   
   x.assign(3,7);
   y.assign(4,3);
   z.assign(4,3);
    
    x=x.Colm();
    y=y.Colm();
    z=z.Colm(); 
    
    std::cout<<"testing colm function"<<std::endl;   
    std::cout<<"x column="<<x<<std::endl; 
    std::cout<<"y column="<<y<<std::endl;
    std::cout<<"z column="<<z<<std::endl;  
    outf<<"testing colm function"<<std::endl;   
    outf<<"x column="<<x<<std::endl; 
    outf<<"y column="<<y<<std::endl;
    outf<<"z column="<<z<<std::endl;  
    
    
 
    x.assign(3,7);
    y.assign(4,3);
    z.assign(4,3);
   
    x.inc(1,2);
    y.inc(1,2);
    z.inc(1,2);   
    
    std::cout<<"testing inc function"<<std::endl;
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    outf<<"testing inc function"<<std::endl;
    outf<<"x="<<x<<std::endl;
    outf<<"y="<<y<<std::endl;
    outf<<"z="<<z<<std::endl;
    
    system("pause");
    return 0;
}  


I know the line for that code was right when i ran it.Did i make another mistake that im not seeing?Any help is appreciated.Sorry for being a pain,im just really not that good at programming.
You need to use braces if you're putting more than one statement between the if and else.

1
2
3
4
5
6
7
8
if (x==y) {
   std::cout<<"these points are equal"<<std::endl;
   outf<<"these points are equal"<<std::endl;
}
else {
   std::cout<<"these points are not equal"<<std::endl;
   outf<<"these points are not equal"<<std::endl;
}
thank you all,works perfectly.
Topic archived. No new replies allowed.