Friday, March 20, 2015

Error reading in c program



#include 
#include 
 
typedef int Bool;
 
const Bool TRUE = 1;
const Bool FALSE = 0;
 
char printableBool (Bool i)
 {
  if (i==FALSE) 
    return 'F';
  else return 'T';
}
 
 
int main () 
{
 
  ifstream f1, f2, f3check;
  ofstream f3;
  f1.open("exam");
  if (!f1) 
{
    cout << "Cannot open file exam" << endl;
    return 1;
  }
  f2.open("key");
  if (!f2) 
{
    cout << "Cannot open file key" << endl;
    return 1;
  }
  f3check.open("result");
  if (f3check)
 {
    cout << "File result already exists" << endl;
    return 1;
  }
  f3.open("result");
  if (!f3) 
{
    cout << "Cannot open file result" << endl;
    return 1;
  }
  int runningSum;
  int ans, key, score;
  runningSum = 0;
  f1 >> ans;
  if (!f1)
 {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
{
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key)
 {
    runningSum = runningSum + 25;
    score = TRUE;
  }
  else score = FALSE;
  f3 << printableBool(score) << endl;
  if (!f3) 
{
    cout << "Error writing to file result" << endl;
    return 1;
  }
 
  f1 >> ans;
  if (!f1)
 {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
{
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key)
 {
    runningSum = runningSum + 25;
    score = TRUE;
  }
  else score = FALSE;
  f3 << printableBool(score) << endl;
  if (!f3) 
{
    cout << "Error writing to file result" << endl;
    return 1;
  }
 
  f1 >> ans;
  if (!f1)
 {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
{
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key) 
{
    runningSum = runningSum + 25;
    score = TRUE;
  }
  else score = FALSE;
  f3 << printableBool(score) << endl;
  if (!f3) 
{
    cout << "Error writing to file result" << endl;
    return 1;
  }
 
  f1 >> ans;
  if (!f1)
 {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
{
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key)
 {
    runningSum = runningSum + 25;
    score = TRUE;
  }
  else score = FALSE;
  f3 << printableBool(score) << endl;
  if (!f3)
 {
    cout << "Error writing to file result" << endl;
    return 1;
  }
 
  f3 << endl << runningSum << endl;  
  
}

No comments:

Post a Comment