Thread: Geniunely lost...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    Geniunely lost...

    Hey folks. I'll just say from the off that I'm well aware that I can't ask you guys to do my work for me and that's not what I'm asking. I got a C++ coursework assignment yesterday for my programming class (in which the lecturer cannot teach at all). The coursework he sets is beyond the level that any of our class are even slightly capable of.

    Usually I'm quite good at figuring out programming, like with my last assignment, but after 12 hours straight yesterday and a few hours this morning, this one is just stressing me out and nothing I seem to try works. I was wondering if someone on here could give me a rough guide to what way I should program this. I just don't even know where to begin. At this point if I could get half marks I would be happy.

    Once again, I'm not looking for someone to do my code for me, I'm just lookin for a rough path or guide to follow to get this program done as I'm running out of time. Thanks in advance!

    I've included the task as an attachment.
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You mean you can't even figure out to start with.
    Code:
    class Question {
      private:
        string theQuestion;
      public:
    };
    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
    Oct 2011
    Posts
    7
    No I understand that stuff, I just don't understand the concept of the two classes, one holding the questions, the methods between them... I'm just really, really confused by all of this

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Obviously you don't yet understand about classes in C++. You should go through some simple examples (actually typing in the code and compiling and running, even modifying) to thoroughly understand how things work. Your textbook should have something, and I know the internet does.

    Classes (I) - C++ Documentation

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Okay so I've managed to code this but I'm pretty lost and I keep getting a 'no operator "<<" matches these operands' error when I try to compile. This is to do with me trying to print the question variable. If I could just get some feedback on this code, it would be great.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <conio.h>
    
    using namespace std;
    
    const int ARRAY_SIZE = 20;
    
    class Quiz
    {    
        public:
        string questAndAns[ARRAY_SIZE][5];              
        int correctAnswer[ARRAY_SIZE];
    
        Quiz();
        string getQuestion(int num);
    };
    
    Quiz::Quiz()
    {
        questAndAns[0][0] = "What is 3 + 4?";
        questAndAns[0][1] = "7";
        questAndAns[0][2] = "3";
        questAndAns[0][3] = "4";
        questAndAns[0][4] = "34";
        correctAnswer[0] = 1;
    
        questAndAns[1][0] = "What is 9 - 1?";
        questAndAns[1][1] = "10";
        questAndAns[1][2] = "8";
        questAndAns[1][3] = "91";
        questAndAns[1][4] = "0";
        correctAnswer[1] = 2;
    
        questAndAns[2][0] = "What is 4 / 2";
        questAndAns[2][1] = "42";
        questAndAns[2][2] = "8";
        questAndAns[2][3] = "2";
        questAndAns[2][4] = "4";
        correctAnswer[2] = 3;
    
        questAndAns[3][0] = "What is 3 * 1";
        questAndAns[3][1] = "1";
        questAndAns[3][2] = "4";
        questAndAns[3][3] = "0";
        questAndAns[3][4] = "3";
        correctAnswer[3] = 4;
    
    }//Quiz constructor
    
    string Quiz::getQuestion(int num)
    {    return questAndAns[num][0];
    }
    
    //Quiz Class//////////////////////////
    
    //METHOD - DISPLAY QUESTION
    //METHOD - GET ANSWER INPUT
    //METHOD - RETURN SCORE
    //METHOD - DISPLAY COMMENTS
    
    
    class Question 
    {   
        public:
        int questionNum, score, input;
        string question, answer1, answer2, answer3, answer4, comment;
    
        string getQuestion(Quiz x, int n);
        int getAnswer();
    
    };
    
    string Question::getQuestion(Quiz x, int n)
    {    return x.getQuestion(n);
    }
    
    int Question::getAnswer()
    {    cout << "Select answer (1-4) :> ";
        cin >> Question::input;
        return Question::input;
    }
    
    
        int main()
        {    string question, answer1, answer2, answer3, answer4, comment;
            int input, score = 0;
            
            Quiz quiz1;
            Question question1;
            Question question2;
            Question question3;
            Question question4;
    
            cout << "Welcome to the Quiz!\n";
            cout << "Question 1\n";
    
            question = question1.getQuestion(quiz1, 1);
            cout << question;
            input = question1.getAnswer();
    
            if (input==quiz1.correctAnswer[0])
                score+10;
    
            question = question2.getQuestion(quiz1, 2);
            cout << question;
            input = question2.getAnswer();
    
            if (input==quiz1.correctAnswer[1])
                score+10;
        }

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    #include <string>

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do NOT use this include if it is NOT needed. It should never be needed in a good C++ code written using a less than 10 year old compiler.

    Code:
    #include <conio.h>
    Tim S.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You use std::string, but haven't included its header. Like DaveH implies,
    Quote Originally Posted by DaveH View Post
    #include <string>
    you must include that header.
    Try that, then return if there are more problems (with updated code and error descriptions!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    So I've put some more thought and work into this. I think it's almost up to scratch but I know it isn't perfect. I'm struggling with the score variable i.e. where to store it and how to update it. Can't get it working as intended at all. I thought I had it but I guess not. Any suggestions to improve the overall code would be great as I'm sure it is a mess in the eyes of you experts. This is a learning process for me and I appreciate any of your help! Thanks!

    Code:
    
    
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    const int ARRAY_SIZE = 20;
    
    class Quiz
    {    
        public:
        string questAndAns[ARRAY_SIZE][5];              
        int correctAnswer[ARRAY_SIZE];
        int score;
    
        Quiz();
        string getQuestion(int num);
        void setScore();
        int getScore();
    };
    
    Quiz::Quiz()
    {    
        questAndAns[0][0] = "What is 3 + 4?";
        questAndAns[0][1] = "7";
        questAndAns[0][2] = "3";
        questAndAns[0][3] = "4";
        questAndAns[0][4] = "34";
        correctAnswer[0] = 1;
    
        questAndAns[1][0] = "What is 9 - 1?";
        questAndAns[1][1] = "10";
        questAndAns[1][2] = "8";
        questAndAns[1][3] = "91";
        questAndAns[1][4] = "0";
        correctAnswer[1] = 2;
    
        questAndAns[2][0] = "What is 4 / 2";
        questAndAns[2][1] = "42";
        questAndAns[2][2] = "8";
        questAndAns[2][3] = "2";
        questAndAns[2][4] = "4";
        correctAnswer[2] = 3;
    
        questAndAns[3][0] = "What is 3 * 1";
        questAndAns[3][1] = "1";
        questAndAns[3][2] = "4";
        questAndAns[3][3] = "0";
        questAndAns[3][4] = "3";
        correctAnswer[3] = 4;
    
    }//Quiz constructor
    
    string Quiz::getQuestion(int num)
    {    
        return questAndAns[num][0];
    }//getQuestion
    
    void Quiz::setScore()
    {
        score = score+10;
    }//setScore
    
    int Quiz::getScore()
    {
        return score;
    }//getScore
    
    
    /**************************************************************************************/
    
    
    class Question 
    {   
        public:
        int input;
        string question;
    
        Question(Quiz x, int n);
        void printQuestion(Quiz x, int qNum);
        int getAnswer();
        void checkAnswer(Quiz x, int pInput, int qNum);
    
    };
    
    Question::Question(Quiz x, int n)
    {    question = x.getQuestion((n-1));
    }//Question constructor
    
    void Question::printQuestion(Quiz x, int qNum)
    {    cout << "\nQuestion " << qNum << "\n\n";
        cout << question << "\n";
        cout << "1) " << x.questAndAns[(qNum-1)][1] << "\n";
        cout << "2) " << x.questAndAns[(qNum-1)][2] << "\n";
        cout << "3) " << x.questAndAns[(qNum-1)][3] << "\n";
        cout << "4) " << x.questAndAns[(qNum-1)][4] << "\n";
    }//Question::printQuestion
    
    int Question::getAnswer()
    {    cout << "Select answer (1-4) :> ";
        cin >> Question::input;
        return Question::input;
    }//Question::getAnswer
    
    void Question::checkAnswer(Quiz x, int pInput, int qNum)
    {    if (input==x.correctAnswer[(qNum-1)])
        {    cout << "\nCorrect! 10 points!\n\n";
            x.setScore();
        }
        else
        {    cout << "\nWrong!\n\n";
        }
    }//Question::checkAnswer 
    
        int main()
        {    int score;
            
            Quiz quiz1;
            Question question1(quiz1, 1);
            Question question2(quiz1, 2);
            Question question3(quiz1, 3);
            Question question4(quiz1, 4);
    
            cout << "Welcome to the Quiz!\n";
    
            question4.printQuestion(quiz1, 1);
            question4.checkAnswer(quiz1, question4.getAnswer(),1);        
            
            question4.printQuestion(quiz1, 2);
            question4.checkAnswer(quiz1, question4.getAnswer(),2);        
            
            question4.printQuestion(quiz1, 3);
            question4.checkAnswer(quiz1, question4.getAnswer(),3);
    
            question4.printQuestion(quiz1, 4);
            question4.checkAnswer(quiz1, question4.getAnswer(),4);
    
            score = quiz1.getScore();
            cout << "You scored " << score << " points out of a possible " << score * 4;
    
            _getch();
            return 0;
        }//main
    
    
        //access restrictions? public private?
    Last edited by Salem; 10-31-2011 at 06:45 AM. Reason: privacy edit

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about using a
    SetScore(int newscore)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Would anyone be able to explain to me why my score variable always comes out as zero. I've debugged the program and can see where the problem is occuring but I can't figure out why. I'm also wondering if I've done the best thing when it comes to my visibility modifiers. Thanks! Here's the code:

    Code:
    
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    const int ARRAY_SIZE = 20;
    
    class Quiz
    {    
        public:
        string questAndAns[ARRAY_SIZE][5];              
        int correctAnswer[ARRAY_SIZE];
        
        string getQuestion(int num);
        void setScore(int num);
        int getScore();
        Quiz();
    
        private:
        int score;
        
    };
    
    Quiz::Quiz()
    {    
        score = 0;
        questAndAns[0][0] = "What is 3 + 4?";
        questAndAns[0][1] = "7";
        questAndAns[0][2] = "3";
        questAndAns[0][3] = "4";
        questAndAns[0][4] = "34";
        correctAnswer[0] = 1;
    
        questAndAns[1][0] = "What is 9 - 1?";
        questAndAns[1][1] = "10";
        questAndAns[1][2] = "8";
        questAndAns[1][3] = "91";
        questAndAns[1][4] = "0";
        correctAnswer[1] = 2;
    
        questAndAns[2][0] = "What is 4 / 2";
        questAndAns[2][1] = "42";
        questAndAns[2][2] = "8";
        questAndAns[2][3] = "2";
        questAndAns[2][4] = "4";
        correctAnswer[2] = 3;
    
        questAndAns[3][0] = "What is 3 * 1";
        questAndAns[3][1] = "1";
        questAndAns[3][2] = "4";
        questAndAns[3][3] = "0";
        questAndAns[3][4] = "3";
        correctAnswer[3] = 4;
    }//Quiz constructor
    
    string Quiz::getQuestion(int num)
    {    
        return questAndAns[num][0];
    }//getQuestion
    
    void Quiz::setScore(int num)
    {
        score = score+num;
    }//setScore
    
    int Quiz::getScore()
    {
        return score;
    }//getScore
    
    
    /**************************************************************************************/
    
    
    class Question 
    {   
        public:
        int input;
        string question;
    
        Question(Quiz x, int n);
        void printQuestion(Quiz x, int qNum);
        int getAnswer();
        void checkAnswer(Quiz x, int pInput, int qNum);
    };
    
    Question::Question(Quiz x, int n)
    {    question = x.getQuestion((n-1));
    }//Question constructor
    
    void Question::printQuestion(Quiz x, int qNum)
    {    cout << "\nQuestion " << qNum << "\n\n";
        cout << question << "\n";
        cout << "1) " << x.questAndAns[(qNum-1)][1] << "\n";
        cout << "2) " << x.questAndAns[(qNum-1)][2] << "\n";
        cout << "3) " << x.questAndAns[(qNum-1)][3] << "\n";
        cout << "4) " << x.questAndAns[(qNum-1)][4] << "\n";
    }//printQuestion
    
    int Question::getAnswer()
    {    cout << "Select answer (1-4) :> ";
        cin >> Question::input;
        return Question::input;
    }//getAnswer
    
    void Question::checkAnswer(Quiz x, int pInput, int qNum)
    {    
        if (input==x.correctAnswer[(qNum-1)])
        {   cout << "\nCorrect! 10 points!\n\n";
            x.setScore(10);
        }
        else
            cout << "\nWrong!\n\n";
        
    }//checkAnswer
    
    int main()
    {    int score;
         
        Quiz quiz1;
        Question question1(quiz1, 1);
        Question question2(quiz1, 2);
        Question question3(quiz1, 3);
        Question question4(quiz1, 4);
    
        cout << "Welcome to the Quiz!\n";
    
        question1.printQuestion(quiz1, 1);
        question1.checkAnswer(quiz1, question1.getAnswer(),1);        
            
        question2.printQuestion(quiz1, 2);
        question2.checkAnswer(quiz1, question2.getAnswer(),2);        
           
        question3.printQuestion(quiz1, 3);
        question3.checkAnswer(quiz1, question3.getAnswer(),3);
    
        question4.printQuestion(quiz1, 4);
        question4.checkAnswer(quiz1, question4.getAnswer(),4);
    
        score = quiz1.getScore();
        cout << "You scored " << score << " points out of a possible 40";
    
        _getch();
        return 0;
    }//main
    Last edited by Salem; 10-31-2011 at 06:45 AM. Reason: privacy edit

  12. #12
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Try sticking this in there and running it.
    Code:
        ~Quiz() { cout << "Bye Bye" << endl; }
    Also main() score is not the same as class Quiz score.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Well I'm finished now guys, thanks for all your help. I'm just wondering, is it possible to delete this thread or edit my posts, as it contains MY code and I don't want this available to anyone who may be doing the same coursework as me. I made the mistake of copying and pasting all of my code from my compiler when making a few posts. This means that my registration number and coursework title was included by mistake. I have since read the homework policy but I hadn't when I initally began making these posts. I'm now quite worried about having my work available to all and that it is clearly labelled as my work so there is no anonymity. I had originally made this thread with the intention of removing it after I had received my answers and finished my coursework, and while I'm now aware that this is against forum policy, I was not aware of this when I began posting (my own fault I know). I feel this thread is too specific to be any use to others anyway and I hope you can see fit to help me in this situation! I'm kind of new to the whole forum thing, don't really get it.
    Last edited by heretic397; 10-31-2011 at 06:33 AM.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I've removed the course ID's, but the bulk of the posts will remain.
    People who copy have their own problems - you should be OK since you published first.
    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.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    That's great, thanks so much! I'll be more careful in future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lost need help
    By robertran88 in forum C Programming
    Replies: 2
    Last Post: 10-30-2010, 12:37 PM
  2. lost value??
    By Hussain Hani in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2009, 06:55 AM
  3. I'm lost
    By rainman39393 in forum C++ Programming
    Replies: 8
    Last Post: 02-24-2008, 03:38 PM
  4. Somewhat Lost...
    By Matty_Alan in forum C Programming
    Replies: 4
    Last Post: 06-22-2007, 10:09 PM
  5. help I'm lost
    By Draco in forum C Programming
    Replies: 11
    Last Post: 08-20-2003, 04:01 PM