Thread: Arrays problem

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Arrays problem

    Here is my code which compares answers in two files and outputs correct answer and wrong. The problem is why I can't output all elements of array inside function.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    //prototypes of functions
    void openAnswerKeyFile(ifstream&);
    
    // put into array
    void readAnswerKeyFile(string answerArray[], ifstream&);
    
    void openStudentFile(ifstream&);
    //void openOutputFile(ofstream&);
    
    void readStudentAnswers(string studentAnswersArray[], ifstream&);
    int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]);
    
    int main() {
    
        // creates object answerFile of type ifstream
        string answers[25];
        string studentAnswersArray[27];
        bool missedQuestions[25] = {false};
        
        ifstream answerFile;
        openAnswerKeyFile(answerFile);
    
        ifstream studentAnswers;
        openStudentFile(studentAnswers);
    
        //ofstream outputFile;
        //openOutputFile(outputFile);
    
        readAnswerKeyFile(answers, answerFile);
        readStudentAnswers(studentAnswersArray, studentAnswers);
       int numberOfWrong= compare(answers, studentAnswersArray, missedQuestions);
    
    
        //outputFile.close();
        return 0;
    }
    
    // opens answer file
    
    void openAnswerKeyFile(ifstream& answerFile) {
    
        // filenames are declared as an array because 
        // ifstream class method open requires char as parameter
    
        char answerFilename[] = "Answer Key 1.txt";
        char answerFilename2[] = "Answer Key 2.txt";
        char answerFilename3[] = "Answer Key 3.txt";
        string userInput = "";
    
        while (userInput != answerFilename && userInput != answerFilename2 && userInput != answerFilename3) {
            cout << "Enter answer key filename: " << endl;
            getline(cin, userInput);
        }
    
        // take from user number of file. Number is located at index 11 
    
    
        answerFile.open(answerFilename);
    }
    
    void openStudentFile(ifstream& studentAnswers) {
    
        // filenames are declared as an array because 
        // ifstream class method open requires char as parameter
    
        char studentAnswerFilename[] = "Student Answers 1.txt";
        char studentAnswerFilename2[] = "Student Answers 2.txt";
        char studentAnswerFilename3[] = "Student Answers 3.txt";
        string userInput = "";
        while (userInput != studentAnswerFilename && userInput != studentAnswerFilename2 && userInput != studentAnswerFilename3) {
            cout << "Enter student answers filename: " << endl;
            getline(cin, userInput);
        }
    
        // takes from user number of file. Number is located at index 16
        studentAnswers.open(studentAnswerFilename);
    
    }
    
    void openOutputFile(ofstream& outputFile) {
    
        char outputFilename[] = "";
    
        cout << "Enter output filename (max 25 characters): " << endl;
    
        //takes input from user
        cin.getline(outputFilename, 25);
        // open file
    
        outputFile.open(outputFilename);
        if (!outputFile.is_open()) {
            cout << "Cant open file";
        }
    }
    
    void readAnswerKeyFile(string answerArray[], ifstream& answerFile) {
        string line;
        short i = 0;
        while (answerFile.good()) {
            getline(answerFile, line);
            answerArray[i] = line;
            i++;
        }
    
        answerFile.close();
    }
    
    void readStudentAnswers(string studentAnswersArray[], ifstream& studentAnswers) {
        string line;
        short i = 0;
        while (studentAnswers.good()) {
            getline(studentAnswers, line);
            studentAnswersArray[i] = line;
            i++;
        }
    
        studentAnswers.close();
    }
    
    int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]) {
        short numberOfWrong=0;
        for (short i = 0, j = 2; i < 25, j < 27; i++, j++) {
            
            if (answers[i] != studentAnswersArray[j]) {
                missedQuestions[i] = true;
                numberOfWrong++;
                cout << answers[i];
            }
       
        }
        return numberOfWrong;
    }

    Part of code which is confusing is this :

    if (answers[i] != studentAnswersArray[j]) {
    missedQuestions[i] = true;
    numberOfWrong++;
    cout << answers[i];
    }
    I got as output only 1 element

    If you want to compile make 2 text files named Answer Key 1.txt with following content:
    A
    B
    B
    D
    and so on (25 items)
    Next file is student Answers 1.txt with following:

    id
    name
    A
    D
    C
    D
    (27 items)
    Last edited by nedim; 04-01-2012 at 02:01 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 88: char outputFilename[] = "";
    This doesn't have room for 1 character, let alone 25
    Use a string instead.

    And when it comes to opening the file, use
    outputFile.open(outputFilename.c_str());
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    I get error Unable to resolve identifier c_str

    I have imported #include <cstring>
    Last edited by nedim; 04-01-2012 at 02:19 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You have to declare outputFilename as a std::string, as stated.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Quote Originally Posted by iMalc View Post
    You have to declare outputFilename as a std::string, as stated.
    I forgot to do that

    After I made changes it outputs only one element instead of all

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you attach examples of your two input files?

    > for (short i = 0, j = 2; i < 25, j < 27; i++, j++)
    The comma operator in the comparison is not doing what you seem to think it should be doing.
    Perhaps you meant &&
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    11

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Did you bother to read Salem's post?

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    I have just tested it on windows machine and I got correct output.
    It looks like my linux compiler is messed up.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If code works with one compiler but not another then, about 99.999% of the time, the code is at fault, not the compiler.

    No matter how much novices insist on blaming a compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Part of the problem is your broken code to read a file - it's just a variant of how NOT to use feof().

    Code:
    (gdb) print i
    $1 = 25
    (gdb) print line
    $2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
        _M_p = 0x604058 ""}}
    (gdb) bt
    #0  0x00007ffff7b7221e in std::string::assign(std::string const&) () from /usr/lib/libstdc++.so.6
    #1  0x00000000004019f5 in readAnswerKeyFile (answerArray=0x7fffffffdd70, answerFile=...) at bar.cpp:107
    #2  0x00000000004013b3 in main () at bar.cpp:35
    Note the value of i
    It's 25, so you're off the end of your array!

    Replacing both loops with
    while (getline(answerFile, line))
    and outputting some extra detail gives
    Answer to question 5 should be A
    Answer to question 6 should be A
    Answer to question 7 should be A
    Answer to question 24 should be B
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Quote Originally Posted by Salem View Post
    Part of the problem is your broken code to read a file - it's just a variant of how NOT to use feof().

    Code:
    (gdb) print i
    $1 = 25
    (gdb) print line
    $2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
        _M_p = 0x604058 ""}}
    (gdb) bt
    #0  0x00007ffff7b7221e in std::string::assign(std::string const&) () from /usr/lib/libstdc++.so.6
    #1  0x00000000004019f5 in readAnswerKeyFile (answerArray=0x7fffffffdd70, answerFile=...) at bar.cpp:107
    #2  0x00000000004013b3 in main () at bar.cpp:35
    Note the value of i
    It's 25, so you're off the end of your array!

    Replacing both loops with
    while (getline(answerFile, line))
    and outputting some extra detail gives
    Answer to question 5 should be A
    Answer to question 6 should be A
    Answer to question 7 should be A
    Answer to question 24 should be B
    Changed both loops but it still outputs only 1 value. Even when I write to printall values it shows only one.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    11

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But if you watch carefully, you actually go into the inner loop 4 times (as mine does).

    Perhaps when you print a single letter, you should also print a newline to flush the output.
    Code:
                cout << "Answer to question " << i << " should be " << answers[i] << endl;
    The other question, were the results I posted correct?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays problem
    By hamidhqs in forum C Programming
    Replies: 5
    Last Post: 03-06-2012, 12:41 AM
  2. Problem with Arrays
    By dldsob in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2009, 08:43 PM
  3. Problem with arrays
    By dogbert234 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 03:06 AM
  4. arrays problem
    By swapnil_sandy in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 04:14 PM

Tags for this Thread