Hopefully this should be an easy problem I have looked all over my code and I cannot find where the problem lies. The program reads to large ints from a text file adds them and then outputs the result. Ive included the code below.
Code:
int main()
{
bigint a, b, c;
std::ofstream outfile("results.txt");
std::ifstream infile("data1-1.txt");
if(infile.is_open())
{
std::cout << "file open \n";
while (infile >> a)
{
infile >> b;
std::cout << a;
std::cout << b;
std::cout << "+__________________________________________\n";
c = a + b;
std::cout << c;
}
}
else std::cout << "Unable to open file\n";
outfile.close();
infile.close();
return 0;
}
This is a simple ADT exercise the input operator will read until the semicolon. The program works fine without reading from the file in a loop, but that is a requirement for this assignment.
Any help would be greatly appreciated
Last edited by originalmoose; September 29th, 2010 at 06:08 PM.
Reason: resolved
What is bigint? What provision does its operator>> make for semicolons? If none, you'll need to get the semicolon out of the file stream before you read the next value. Also, your couts should probably be separated by a newline.
Hopefully this should be an easy problem I have looked all over my code and I cannot find where the problem lies. The program reads to large ints from a text file adds them and then outputs the result. Ive included the code below.
You didn't include the most important part -- what is "bigint"? Please post this class, as it is being used both as input and for addition.
The problem likely lies in incorrect logic within the bigint class. You haven't shown us enough of it to be certain, but offhand, I am suspicious because:
1) The operator>> appears to be reading digits in most-to-least significant order, but operator+ appears to be proceeding in the same direction. When doing addition, you should be adding the least significant digits first. I say "appears" because I don't have enough information yet to be certain this is what's happening.
2) You do not appear to be specifying how many digits "result" should have anywhere, which could very well lead to a segfault depending on the data type of "digits".
First, did you test the bigint class with hard-coded values?
Second, you know that if you're file reading fails, that program fails regardless of whether your bigint addition works. When I run your original program in Visual Studio 2010, the file reading goes haywire. Haywire meaning this line:
Code:
while (infile >> a)
goes into an infinite loop after two iterations.
Maybe you should just hard-code those values into strings, and just call c = a+b twice to make sure it isn't bigint addition that is causing the problem first.
Ive hardcoded the values in without any issues(well none that i didnt fix).
I can also read in two sets of values for a and b and calculate c and repeat the same for two new values for a and b and calculate c again without any problem as long as im not running the input in a loop.
Last edited by originalmoose; September 29th, 2010 at 05:37 PM.
Reason: clarification
std::istream& operator>> (std::istream& in, bigint& num)
{
num.initialize();
char ch;
in >> ch; // what if in is invalid here?
while (ch != ';')
{
num.push_back(ch-'0');
in >> ch; // what if in becomes invalid here?
}
return in;
}
You do no check to see if the stream is in a valid state. You assume it is, and you keep reading.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 29th, 2010 at 05:48 PM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.