Input/Output conversion, seeking advice.

Hello all, first time on here, and I'm in a bit of a dilemma.
The code underneath this is code from a book that I am supposed to modify
for a class. The purpose of it is to change the code from File input/output
to Standard input/output. So far, what I have done is changed the inFile's and
outFile's in the middle to cin's and cout's. Since this is basically the first
time I am coming into contact with C++, I am not sure what else there really
is to do, to make the following code print. I have added the <iostream> to the
top and middle, but am confused as to what else I am supposed to change to
get this to work smoothly, the errors I am receiving are:



1
2
3
4
5
6
7
8
1
1>Compiling...
1>Fraction.cpp
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\fraction\fraction\fraction.cpp(60) : error C2059: syntax error : 'while'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\fraction\fraction\fraction.cpp(61) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\fraction\fraction\fraction.cpp(61) : error C2447: '{' : missing function header (old-style formal list?)
1>Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Fraction\Fraction\Debug\BuildLog.htm"
1>Fraction - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


NOTE: I have added lines 60 and 61 for better clarity.



Any help is greatly appreciated. Note that this is not a required exercise for the class, but rather just to try to get a bit more familiar with the programming language itself, and it would help greatly if I knew what I was doing wrong here. I appreciate any insight into this.












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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Fraction.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

class FractionType
{
public:
	void Initialize(int numerator, int demoninator);
	// Function: Initialize the fraction
	// Pre: Numerator and denominator are in reduced form
	// Post: Fraction is initialized

	int GetNumerator();
	// Function: Returns the value of the numerator
	// Pre: Fraction has been initialized
	// Post: Numerator is returned

	int GetDenominator();
	// Function: Returns the value of the numerator
	// Pre: Fraction has been initialized
	// Post: Denominator is returned

	bool IsZero();
	// Function: Determines if fraction is zero
	// Pre: Fraction has been initialized
	// Post: Returns true if numerator is zero, false otherwise

	bool IsNotProper();
	// Function: Determines if fraction is a proper fraction
	// Pre: Fraction has been initialized
	// Post: Returns true if fraction is greater than or equal to 1; false
	//		 otherwise

	int ConvertToProper();
	// Function: Converts the fraction to a whole number and a 
	// fractional part
	// Pre: Fraction has been initialized, is in reduced form, and
	//		is not a proper fraction
	// Post: Returns the whole number
	//		 Remaining fraction is original fraction minus the
	//		 whole number; fraction is in reduced form
private:
	int num;
	int denom;
};



#include <iostream>			

FractionType fraction;

while (command != "Quit")                                 
{
	if (command == "Initialize")
	{
		int numerator, denominator;
		cin  >> numerator;
		cin  >> denominator;
		fraction.Initialize(numerator, denominator);
		cout << "Numerator: "  << fraction.GetNumerator()
			<< " Denominator: " << fraction.GetDenominator()
			<< endl;
	}
	else if (command == "GetNumerator")
		cout << "Numerator: "  << fraction.GetNumrator()
		<< endl;
	else if (command == "GetDenominator")
		cout << "Denominator: " << fraction.GetDenominator()
		<< endl;
	else if (command == "IsZero")
		if (fraction.IsZero())
			cout << "Fraction is zero " << endl;
		else
			cout << "Fraction is not zero " << endl;
	else if (command == "IsNotProper")
		if (fraction.IsNotProper())
			cout << "Fraction is improper " << endl;
		else
			cout << "Fraction is proper " << endl;
	else
	{
		cout << "Whole number is " << fraction.ConvertToProper()
			<< endl;
		cout <<  "Numerator is: "  << fraction.GetNumerator()
			<< " Denominator: " << fraction.GetDenominator()
			<< endl;
	}

	:
}



// implementation file for class FractionType
#include <iostream>
void FractionType::Initialize(int numerator, int denominator)
// Function: Initialize the fraction
// Pre: numerator and denominator are in reduced form
// Post: numerator is stored in num; denominator is stored in 
//		 denom
{
	num = numerator;
	denom = denominator;
}
int FractionType::GetNumerator()
// Function: Returns the value of the numerator
// Pre: Fraction has been initialized
// Post: numerator is returned
{
	return num;
}
int FractionType::GetDenominator()
// Function: Returns the value of the denominator
// Pre: Fraction has been initialized
// Post: denominator is returned
{
	return denom;
}

bool FractionType::IsZero()
// Function: Determines if fraction is zero
// Pre: Fraction has been initialized
// Post: Returns true if numerator is zero; false otherwise
{
	return (num == 0);
}

bool FractionType::IsNotProper()
// Function: Determines if fraction is a proper fraction
// Pre: Fraction has been initialized
// Post: Returns true if num is greater than or equal to denom; false
//		 otherwise
{
	return (num >= denom);
}

int FractionType::ConvertToProper()
// Function: Converts the fraction to a whole number and a
//           fractional part
// Pre: Fraction has been initialized, is in reduced form, and
//		is not a proper fraction
// Post: Returns num divided by denom
//		 num is original num % denom; denom is not changed
{
	int result;
	result = num / denom;
	num = num % denom;
	if (num == 0)
		denom = 1;
	return result;
}



Last edited on
Please use tags for code (the # symbol under format to the right) otherwise we really can't tell where the problem is.
My apologies, should be all straightened out now.
On line 60 you're using a control structure in global scope. Control structures can only be used inside functions. I assume you meant to put it inside _tmain().
The other errors are a result of this.
On line 97 there's a stray colon.
command is undeclared.

Try to include the this-> syntax to refer to members of the class. While the compiler won't care, humans may have a harder time understanding you.
Last edited on
Thanks for the reply, but I'm not sure I understand. So you are saying that the
entire middle part should in fact go into the tmain()? How would all of that look then? I'm new to this so I'm not making sense of it.
Last edited on
1
2
3
4
5
int _tmain(/*...*/){
    while(/*...*/){
        //...
    }
}


The rest seems to be fine.
Oh! Make sure the class appears before it's first utilization or you'll get a compiler error.
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Fraction.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[]){
	while (command != "Quit"){
	
		if (command == "Initialize")
	{
		int numerator, denominator;
		cin  >> numerator;
		cin  >> denominator;
		fraction.Initialize(numerator, denominator);
		cout << "Numerator: "  << fraction.GetNumerator()
			<< " Denominator: " << fraction.GetDenominator()
			<< endl;
	}
	else if (command == "GetNumerator")
		cout << "Numerator: "  << fraction.GetNumrator()
		<< endl;
	else if (command == "GetDenominator")
		cout << "Denominator: " << fraction.GetDenominator()
		<< endl;
	else if (command == "IsZero")
		if (fraction.IsZero())
			cout << "Fraction is zero " << endl;
		else
			cout << "Fraction is not zero " << endl;
	else if (command == "IsNotProper")
		if (fraction.IsNotProper())
			cout << "Fraction is improper " << endl;
		else
			cout << "Fraction is proper " << endl;
	else
	{
		cout << "Whole number is " << fraction.ConvertToProper()
			<< endl;
		cout <<  "Numerator is: "  << fraction.GetNumerator()
			<< " Denominator: " << fraction.GetDenominator()
			<< endl;
	}

	:
}

}

class FractionType
{
public:
	void Initialize(int numerator, int demoninator);
	// Function: Initialize the fraction
	// Pre: Numerator and denominator are in reduced form
	// Post: Fraction is initialized

	int GetNumerator();
	// Function: Returns the value of the numerator
	// Pre: Fraction has been initialized
	// Post: Numerator is returned

	int GetDenominator();
	// Function: Returns the value of the numerator
	// Pre: Fraction has been initialized
	// Post: Denominator is returned

	bool IsZero();
	// Function: Determines if fraction is zero
	// Pre: Fraction has been initialized
	// Post: Returns true if numerator is zero, false otherwise

	bool IsNotProper();
	// Function: Determines if fraction is a proper fraction
	// Pre: Fraction has been initialized
	// Post: Returns true if fraction is greater than or equal to 1; false
	//		 otherwise

	int ConvertToProper();
	// Function: Converts the fraction to a whole number and a 
	// fractional part
	// Pre: Fraction has been initialized, is in reduced form, and
	//		is not a proper fraction
	// Post: Returns the whole number
	//		 Remaining fraction is original fraction minus the
	//		 whole number; fraction is in reduced form
private:
	int num;
	int denom;
};



#include <iostream>			

FractionType fraction;





// implementation file for class FractionType
#include <iostream>
void FractionType::Initialize(int numerator, int denominator)
// Function: Initialize the fraction
// Pre: numerator and denominator are in reduced form
// Post: numerator is stored in num; denominator is stored in 
//		 denom
{
	num = numerator;
	denom = denominator;
}
int FractionType::GetNumerator()
// Function: Returns the value of the numerator
// Pre: Fraction has been initialized
// Post: numerator is returned
{
	return num;
}
int FractionType::GetDenominator()
// Function: Returns the value of the denominator
// Pre: Fraction has been initialized
// Post: denominator is returned
{
	return denom;
}

bool FractionType::IsZero()
// Function: Determines if fraction is zero
// Pre: Fraction has been initialized
// Post: Returns true if numerator is zero; false otherwise
{
	return (num == 0);
}

bool FractionType::IsNotProper()
// Function: Determines if fraction is a proper fraction
// Pre: Fraction has been initialized
// Post: Returns true if num is greater than or equal to denom; false
//		 otherwise
{
	return (num >= denom);
}

int FractionType::ConvertToProper()
// Function: Converts the fraction to a whole number and a
//           fractional part
// Pre: Fraction has been initialized, is in reduced form, and
//		is not a proper fraction
// Post: Returns num divided by denom
//		 num is original num % denom; denom is not changed
{
	int result;
	result = num / denom;
	num = num % denom;
	if (num == 0)
		denom = 1;
	return result;
}




This is what I have so far, is this the format you meant. Sorry for being a bit slow at this, just trying to see the right format. The #include <iostream> and the Fractiontype are still there in the middle, not sure if I was supposed to move those as well? This is what I'm getting now for errors, just 2 at this time. And the class before utilization, does that mean I have to switch out the two huge blocks at the bottom as well, or? Thanks again.


1
2
3
4
5
6
7

1
1>Compiling...
1>Fraction.cpp
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\fraction\fraction\fraction.cpp(8) : error C2065: 'command' : undeclared identifier
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\fraction\fraction\fraction.cpp(8) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Fraction\Fraction\Debug\BuildLog.htm"
1>Fraction - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Like I told you before,
command is undeclared.

Line 45 will give you an error.

Move from line 50 to 94 before _tmain().
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Fraction.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

class FractionType
{
public:
	void Initialize(int numerator, int demoninator);
	// Function: Initialize the fraction
	// Pre: Numerator and denominator are in reduced form
	// Post: Fraction is initialized

	int GetNumerator();
	// Function: Returns the value of the numerator
	// Pre: Fraction has been initialized
	// Post: Numerator is returned

	int GetDenominator();
	// Function: Returns the value of the numerator
	// Pre: Fraction has been initialized
	// Post: Denominator is returned

	bool IsZero();
	// Function: Determines if fraction is zero
	// Pre: Fraction has been initialized
	// Post: Returns true if numerator is zero, false otherwise

	bool IsNotProper();
	// Function: Determines if fraction is a proper fraction
	// Pre: Fraction has been initialized
	// Post: Returns true if fraction is greater than or equal to 1; false
	//		 otherwise

	int ConvertToProper();
	// Function: Converts the fraction to a whole number and a 
	// fractional part
	// Pre: Fraction has been initialized, is in reduced form, and
	//		is not a proper fraction
	// Post: Returns the whole number
	//		 Remaining fraction is original fraction minus the
	//		 whole number; fraction is in reduced form
private:
	int num;
	int denom;
};

int _tmain(int argc, _TCHAR* argv[]){
	while (command != "Quit"){
	
		if (command == "Initialize")
	{
		int numerator, denominator;
		cin  >> numerator;
		cin  >> denominator;
		fraction.Initialize(numerator, denominator);
		cout << "Numerator: "  << fraction.GetNumerator()
			<< " Denominator: " << fraction.GetDenominator()
			<< endl;
	}
	else if (command == "GetNumerator")
		cout << "Numerator: "  << fraction.GetNumrator()
		<< endl;
	else if (command == "GetDenominator")
		cout << "Denominator: " << fraction.GetDenominator()
		<< endl;
	else if (command == "IsZero")
		if (fraction.IsZero())
			cout << "Fraction is zero " << endl;
		else
			cout << "Fraction is not zero " << endl;
	else if (command == "IsNotProper")
		if (fraction.IsNotProper())
			cout << "Fraction is improper " << endl;
		else
			cout << "Fraction is proper " << endl;
	else
	{
		cout << "Whole number is " << fraction.ConvertToProper()
			<< endl;
		cout <<  "Numerator is: "  << fraction.GetNumerator()
			<< " Denominator: " << fraction.GetDenominator()
			<< endl;
	}

	
}

}





#include <iostream>			

FractionType fraction;





// implementation file for class FractionType
#include <iostream>
void FractionType::Initialize(int numerator, int denominator)
// Function: Initialize the fraction
// Pre: numerator and denominator are in reduced form
// Post: numerator is stored in num; denominator is stored in 
//		 denom
{
	num = numerator;
	denom = denominator;
}
int FractionType::GetNumerator()
// Function: Returns the value of the numerator
// Pre: Fraction has been initialized
// Post: numerator is returned
{
	return num;
}
int FractionType::GetDenominator()
// Function: Returns the value of the denominator
// Pre: Fraction has been initialized
// Post: denominator is returned
{
	return denom;
}

bool FractionType::IsZero()
// Function: Determines if fraction is zero
// Pre: Fraction has been initialized
// Post: Returns true if numerator is zero; false otherwise
{
	return (num == 0);
}

bool FractionType::IsNotProper()
// Function: Determines if fraction is a proper fraction
// Pre: Fraction has been initialized
// Post: Returns true if num is greater than or equal to denom; false
//		 otherwise
{
	return (num >= denom);
}

int FractionType::ConvertToProper()
// Function: Converts the fraction to a whole number and a
//           fractional part
// Pre: Fraction has been initialized, is in reduced form, and
//		is not a proper fraction
// Post: Returns num divided by denom
//		 num is original num % denom; denom is not changed
{
	int result;
	result = num / denom;
	num = num % denom;
	if (num == 0)
		denom = 1;
	return result;
}




I moved it up there before like you said, and then I get these same two errors basically.




1
2
3
4
5
6
7
1
1>Compiling...
1>Fraction.cpp
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\fraction\fraction\fraction.cpp(50) : error C2065: 'command' : undeclared identifier
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\fraction\fraction\fraction.cpp(50) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Fraction\Fraction\Debug\BuildLog.htm"
1>Fraction - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.
command is undeclared.


What is command? An std::string? A C string? What?
I couldn't tell you, that's what the book had, and that's what I copied down, like I was supposed to, heh.
Topic archived. No new replies allowed.