GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2005, 01:52
snatbot snatbot is offline
New Member
 
Join Date: Mar 2005
Posts: 2
snatbot is on a distinguished road

C++ Homework Due Tomorrow. Help!


hello all. i'm having some trouble with my output. it seems every time it compiles i get the correct output but it adds the last int imput twice at the end. the imput reads 31 15 26 from a text file. i'm using bloodshed dev c++. thanks in advance for any and all help.

CPP / C++ / C Code:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void print_message (ofstream &, int);
void print_primes (ofstream &, int);
bool is_prime (int);


int main ()
{
 ifstream indata;
 ofstream outdata;
 int number;

 indata.open ("a:\\indata.txt");
 if (!indata)
    {
     cout << "failure to open indata.txt" << endl;
     return 1;
    }
 outdata.open ("a:\\outdata.txt");
 if (!outdata)
    {
     cout << "failure to open outdata.txt" << endl;
     return 1;
    }

 
//////////////////////////////////////////////////

while (indata)
{
 indata >> number;

 print_message (outdata, number);
 print_primes (outdata, number);

}

cout << "the end" << endl;
indata.close (); outdata.close ();
return 0;
}



void print_message (ofstream & outdata, int n)
{
 outdata<<"\nThe number is "<<n<<". The prime numbers less than or equal to "<<n<<" are:"<<endl;
}


void print_primes (ofstream & outdata, int n)
{
 for (int i = 2; i <= n; i++)
    {
    if(is_prime(i))outdata << i << " ";
    }
}


bool is_prime (int p)
{
 for (int j = 2; j < p; j++)
        {
        if ((p % j) == 0) return (false);
        }
        return (true);
}
Last edited by LuciWiz : 02-Mar-2005 at 02:04. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 02-Mar-2005, 06:44
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
Firstly, I recommend describing what is the output you expect, and what is the output that you actually observed. In this case, it is a fairly simple program that can be compiled and run easily, but many people will not even go that far if you do not make an effort to state your problem clearly. Perhaps that best choice, for your example, is to attach the actual indata.txt and outdata.txt from your test.

Now, the error in your program occurs in the while loop when you are reading from the input file. Notice that first you check the status of the input stream, then you read an int, and then you process that call some functions using that int.

Let me see if I can explain the without confusing. The problem here is that the status of the stream after a successful read operation is still okay. It is only after the a failure to read that the stream status will be bad. So, to detect the end-of-file (EOF) condition, we must attempt a read operation that fails. In your code, if we do that, we will call the print functions after the failed read, but before we check the stream status.

The result of a failed read is that nothing more could be read from the stream, so the existing contents of the buffer (e.g., the last value you read) will be extracted and assigned to your variable number.

The solution is to check the stream after the read operation, like this:
CPP / C++ / C Code:
  indata >> number;
while (indata)
{
  print_message (outdata, number);
  print_primes (outdata, number);

  indata >> number;
}
Or, if you do not want to type the read statement twice, you could do the following
CPP / C++ / C Code:
while (indata >> number)
{
  print_message (outdata, number);
  print_primes (outdata, number);
}
This is nice for brevity, but some programmers may argue that we lose clarity by combining two statements.

Hope this helps.

Matthew

Edit: I forgot to explain why the second code snippet works.
The prototype in class istream for the member function operator >> that you are invoking is
CPP / C++ / C Code:
istream& operator >> (int& x);
You can see that this operator returns something, namely a reference to the istream. Think about this and you will understand why the second while loop works.
  #3  
Old 02-Mar-2005, 15:14
snatbot snatbot is offline
New Member
 
Join Date: Mar 2005
Posts: 2
snatbot is on a distinguished road
your help is much appreciated. i completely forgot about the update of the while loop. i'll be more specific about the program in the future. thanks again.
  #4  
Old 02-Mar-2005, 15:34
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
You're quite welcome. ;-) Your program was fine but for that one, rather subtle issue. Good luck with this and future assignments.

Matthew
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with tele directory problem due tomorrow wbsquared03 CPP / C++ Forum 5 01-Nov-2004 23:10
help!!!!!!!! C++ homework newbie CPP / C++ Forum 2 20-Jan-2004 09:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:27.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.