File io problem - need help

This is a discussion on File io problem - need help within the C++ Programming forums, part of the General Programming Boards category; I have a problem with this source code: every time it reaches a file that cannot be found the next ...

  1. #1
    Unregistered
    Guest

    Cool File io problem - need help

    I have a problem with this source code:

    every time it reaches a file that cannot be found the next file even though it is there gives an error message to.

    Please help.

    DirX

    (deu to problems with my website i have to post the code here)

    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>

    int main()
    {
    int Random0to16 = 0, Check = 0, counter = 1, Correct = 0, Wrong = 0;
    int QeustionsAnswered = 0;
    int temp;
    double Percent;
    char file[9], AnswerCorrect[20], AnswerUser[20], Qeustion[125];

    ifstream fin;

    file[0] = 'q';

    while(1)
    {
    counter++;

    if (counter > 0 && counter <= 10)
    file[1] = 't';
    else if(counter > 10 && counter <= 20)
    file[1] = 'm';
    else if(counter > 20 && counter <= 30)
    file[1] = 'w';
    else if(counter > 18)
    {
    break;
    }

    srand(time(NULL));

    Random0to16 = rand()%16;

    Check = Random0to16/10;

    file[2] = (Check + 1 + 48);
    file[3] = Random0to16 - (Check * 10) + 48;

    temp = rand()%6 + 1 + 48;
    file[4] = temp;

    file[5] = '.';
    file[6] = 't';
    file[7] = 'x';
    file[8] = 't';
    file[9] = '\0';


    fin.open(file, ios::nocreate);

    if(fin)
    {
    fin.getline(AnswerCorrect, 20);

    while(!fin.eof())
    {
    fin.getline(Qeustion, 125);
    cout << endl << endl << Qeustion << endl;
    }

    cin.getline(AnswerUser, 20);

    fin.close();

    if( !strcmpi(AnswerCorrect, AnswerUser) )
    {
    cout << "Your answer was correct!" << endl;
    QeustionsAnswered++;
    Correct++;
    }

    else if( strcmpi(AnswerCorrect, AnswerUser) )
    {
    cout << "Your answer was Wrong!" << endl;
    cout << "The correct answer was: " << AnswerCorrect << endl;
    QeustionsAnswered++;
    Wrong++;
    }
    }
    else if(!fin)
    {
    cout << "Fatal Error! " << endl;
    cout << "Could open file: " << file << endl;
    cout << "Press enter to continue... " << endl;
    getchar();

    fin.close();

    continue;
    }
    }

    if(QeustionsAnswered == 0)
    cout << "No qeustions Answered therefor no score..";
    else
    {
    Percent = (100.0 / QeustionsAnswered) * Correct;

    cout << "\nYou have finished the Quiz!" << endl;
    cout << "You had " << Percent << "% of the qeustions correct!" << endl;
    }

    return 0;
    }

  2. #2
    Unregistered
    Guest
    Hey if you wheter read the code nicely formatted here it is!
    www.lan-48.com/~dirk/index.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Problem reading file
    By coder_009 in forum C Programming
    Replies: 10
    Last Post: 01-15-2008, 01:22 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Totally puzzling IO problem
    By Vorok in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 07:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21