Reputation: 29
So this is my code, I keep getting "unexpected end of file while looking for precompiled header. did you forget to add #include stdafx.h to your source?" so I added it to all headers and files and it still says that same error. How do I get rid of the error?
P.S. if you dont mind checking to see if this code answers these questions(The multiplication and division overloading I barely started, but the relational and equality should be ok)
Hugeint.h
#ifndef HUGEINT_H
#define HUGEINT_H
#include <array>
#include <iostream>
#include <string>
#include "stdafx.h";
class Hugeint
{
friend std::ostream &operator<<(std::ostream &, const Hugeint &);
public:
static const int digits = 30;
Hugeint(long = 0);
Hugeint(const std::string &);
Hugeint operator+(const Hugeint &) const;
Hugeint operator+(int) const;
Hugeint operator+(const std::string &) const;
Hugeint operator*(const Hugeint &) const;
Hugeint operator/(const Hugeint &) const;
//relation
bool operator<(const Hugeint &) const;
bool operator<=(const Hugeint &) const;
bool operator>(const Hugeint &) const;
bool operator>=(const Hugeint &) const;
//equal
bool operator==(const Hugeint &) const;
bool operator!=(const Hugeint &) const;
private:
std::array< short, digits > integer;
//short arrayInt[ max ];
};
#endif
Hugeint.cpp
#include <cctype>
#include "HugeInt.h"
#include "stdafx.h"
using namespace std;
Hugeint::Hugeint(long value)
{
for (int i = 0; i<digits; i++)
integer[i] = 0;
for (size_t j = digits - 1; value != 0 && j >= 0; --j)
{
integer[j] = value % 10;
value /= 10;
}
}
Hugeint::Hugeint(const string &number)
{
for (int i = 0; i<digits; i++)
integer[i] = 0;
size_t length = number.size();
for (size_t j = digits - length, k = 0; j < digits; ++j, ++k)
if (isdigit(number[k]))
integer[j] = number[k] - '0';
}
Hugeint Hugeint::operator+(const Hugeint &op2) const
{
Hugeint temp;
int carry = 0;
for (int i = digits - 1; i >= 0; --i)
{
temp.integer[i] = integer[i] + op2.integer[i] + carry;
if (temp.integer[i] > 9)
{
temp.integer[i] %= 10;
carry = 1;
}
else
carry = 0;
}
return temp;
}
Hugeint Hugeint::operator+(int op2) const
{
return *this + Hugeint(op2);
}
Hugeint Hugeint::operator+(const string &op2) const
{
return *this + Hugeint(op2);
}
bool Hugeint::operator<(const Hugeint &hugeInt) const
{
bool less = false;
for (int i = digits - 1; i >= 0 && !less; --i)
{
if (integer[i] <hugeInt.integer[i])
{
less = true;
}
}
return less;
}
bool Hugeint::operator<=(const Hugeint &hugeInt) const
{
bool LessThan = false;
for (int i = digits - 1; i >= 0 && !LessThan; --i)
{
if (integer[i] <= hugeInt.integer[i])
{
LessThan = true;
}
}
return LessThan;
}
bool Hugeint::operator>(const Hugeint &hugeInt) const
{
bool greater = false;
for (int i = digits - 1; i >= 0 && !greater; --i)
{
if (integer[i] >hugeInt.integer[i])
{
greater = true;
}
}
return greater;
}
bool Hugeint::operator>=(const Hugeint &hugeInt) const
{
bool GreaterThan = false;
for (int i = digits - 1; i >= 0 && !GreaterThan; --i)
{
if (integer[i] >= hugeInt.integer[i])
{
GreaterThan = true;
}
}
return GreaterThan;
}
bool Hugeint::operator==(const Hugeint &hugeInt) const
{
bool eqaul = true;
for (int i = digits - 1; i >= 0 && eqaul; --i)
{
if (integer[i] != hugeInt.integer[i])
{
eqaul = false;
}
}
return eqaul;
}
ostream& operator<<(ostream &output, const Hugeint &num)
{
size_t i;
for (i = 0; (i < Hugeint::digits) && (0 == num.integer[i]); ++i);
if (i == Hugeint::digits)
output << 0;
else
for (; i < Hugeint::digits; ++i)
output << num.integer[i];
return output;
}
main.cpp
#include<iostream>
#include<string>
#include "Hugeint.h"
#include "stdafx.h"
using namespace std;
int main(){
char value1[30];
char value2[30];
cout << "enter HugeInt1 : ";
cin >> value1;
Hugeint hugeInt1(value1);
cout << "enter HugeInt2 : ";
cin >> value2;
Hugeint hugeInt2(value2);
cout << "< opertor " << endl;
if (hugeInt1 < hugeInt2) {
cout << hugeInt1 << " is less than " << hugeInt2 << endl;
}
else {
cout << hugeInt1 << " is not less than " << hugeInt2 << endl;
}
cout << "<= opertor " << endl;
if (hugeInt1 <= hugeInt2) {
cout << hugeInt1 << " is less than or eqaul " << hugeInt2 << endl;
}
else {
cout << hugeInt1 << " is not less than or equal " << hugeInt2 << endl;
}
cout << "> opertor " << endl;
if (hugeInt1 > hugeInt2) {
cout << hugeInt1 << " is greater than " << hugeInt2 << endl;
}
else {
cout << hugeInt1 << " is not greater than " << hugeInt2 << endl;
}
cout << ">= opertor " << endl;
if (hugeInt1 >= hugeInt2) {
cout << hugeInt1 << " is greater than or eqaul " << hugeInt2 << endl;
}
else {
cout << hugeInt1 << " is not greater than or equal " << hugeInt2 <<
endl;
}
cout << "== opertor " << endl;
if (hugeInt1 == hugeInt2) {
cout << hugeInt1 << " is eqaul to " << hugeInt2 << endl;
}
else {
cout << hugeInt1 << " is not equal to " << hugeInt2 << endl;
}
system("pause");
return 0;
}
Upvotes: 0
Views: 7934
Reputation: 436
In Visual Studio you could create C++ project -> EmptyProject and than just add main.cpp file with int main() {}
function. In that case all be work fine, and no precompiled headers needed.
Upvotes: 1
Reputation: 12999
#include "stdafx.h"
must be the first include in every source file that is configured to use the precompiled header (delete your extraneous ;
character by the way). These files must have the -Yu
compilation flag.
There must also be a single source file that creates the precompiled header. Traditionally this is called stdafx.cpp
and only contains the single line #include "stdafx.h"
. This file must have the -Yc
compilation flag.
Upvotes: 4