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;
}
|