2

I need my program to relate each answer to 1); however, when I use my if statement to keep my program from going above the highest vector index, it ends up repeating the values a and b in the 4th question, and the value a for the fifth question.

How I want my output to look:

First Question: 1)a 2)b 3)c 4)d

Second Question: 1)b 2)c 3)d 4)e

Third Question: 1)c 2)d 3)e 4)a

Fourth Question: 1)d 2)e 3)a 4)b

Fifth Question: 1)e 2)a 3)b 4)c

Actual Output:

First Question: 1)a 2)b 3)c 4)d

Second Question: 1)b 2)c 3)d 4)a

Third Question 1)c 2)a 3)b 4)c

Fourth Question: 1)a 2)b 3)a 4)b

Fifth Question: 1)a 2)a 3)a 4)a

How can I fix my code in order make the output look how I want it to?

#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <cmath>
using namespace std;

int main()
{


    vector<string> vecQuestions = //vector that will hold all questions
    {
        "First Question: \n",
        "Second Question:\n",
        "Third Question\n",
        "Fourth Question: \n",
        "Fifth Question:\n"
    };
    vector<string> vecAnswers =   // the index of vecAnswers[] correlates to vecAnswers[] //holds all answers
    {
        "a",
        "b",
        "c",
        "d",
        "e"
    };

    array<int, 4> answer_choices = {{1,2,3,4}}; //sets answer choices
    for (unsigned int i =0; i<vecAnswers.size(); i++) //as long as there are questions
    {
        int answers_index = i;
        cout << vecQuestions[i];
        for (int x:answer_choices) // for all four values of array answer choices
        {
            int values_left = vecAnswers.size() - i-1;
            if (values_left < answers_index) //attempt to keep from accessing invalid memory from too large of vector size
            {
                 answers_index =0;
            }
            cout << x << ")" << vecAnswers[answers_index] << " ";
            answers_index++;
        }
        cout <<"\n\n";
    }

    return 0;
}
Andrue
  • 688
  • 3
  • 11
  • 27
  • 1
    Are you sure your *entire program* is part of the relevant code? – David G Jan 16 '14 at 00:54
  • 2
    There's no question here. – chris Jan 16 '14 at 00:54
  • 1
    @0x499602D2 Good point. I'll edit it. I've just seen where people end up wanting to see the entire code, but I don't see a need in this case. – Andrue Jan 16 '14 at 00:55
  • 1
    `My problem with my code lies within the if statement from lines 48-55.` Then it's time to form a testcase using the logic found in those lines. :) – Lightness Races in Orbit Jan 16 '14 at 00:55
  • @Crysis: When people ask for the entire code they are asking for a [testcase](http://sscee.org). That's "entire code" but not "your entire production code from the actual project you're using". – Lightness Races in Orbit Jan 16 '14 at 00:56
  • @LightnessRacesinOrbit Okay. Makes sense. The testcase link you provided seems to be broken. And what exactly is a testcase? – Andrue Jan 16 '14 at 01:02
  • @Crysis: Typo; it's http://sscce.org and all is explained there. Also http://kera.name/articles/2013/10/nobody-writes-testcases-any-more/. Testcases are a critical part of your debugging process. Take a look at the code in [this question](http://stackoverflow.com/q/21118605/560648), for example. The problem has been narrowed down to precisely the area under scrutiny: the readers need to do no further work to examine the stated issue, and the chance of the submitter having made a silly mistake is greatly reduced. – Lightness Races in Orbit Jan 16 '14 at 01:08
  • (Also, many issue tracking systems are maintained by software developers who will insist that a testcase is posted on any new reported issue.) – Lightness Races in Orbit Jan 16 '14 at 01:11
  • @LightnessRacesinOrbit Is that better? I don't think it's a true testcase, but it does narrow the code down to the problem. – Andrue Jan 16 '14 at 01:29
  • @Crysis: It's _much_ better. :) I'm still struggling to really understand what your code is supposed to do here (I think you'll need to tell us the expected output), but this is now something I would try to answer (and have started looking at it a little, thought it is late in the evening now so...) – Lightness Races in Orbit Jan 16 '14 at 01:31
  • 1
    don't know what this question is asking for... – Peng Zhang Jan 16 '14 at 01:36
  • It's for quiz program I'm writing to help myself study. Each index in `vecAnswers` stores the answer to the same index in `vecQuestions`. If I take the `if` statement out, then my program attempts to access an invalid index, which results in a crash. Right now, I need the answer to the current question to be the first answer choice and refrain from attempting to access an invalid location. I'm not sure why I'm getting these output results (about to add). – Andrue Jan 16 '14 at 01:36
  • The problem arises in the fourth and fifth question. – Andrue Jan 16 '14 at 01:39
  • @PengZhang Does it make more sense now? – Andrue Jan 16 '14 at 01:45
  • Still not very clear. Why there is "f" in the output you want? – Peng Zhang Jan 16 '14 at 02:05
  • @PengZhang That was a typo. Sorry. Fixed... – Andrue Jan 16 '14 at 02:06

2 Answers2

2

Simpler logic and more readable code. Use modular % vecAnswers.size().

array<int, 4> answer_choices = {{1,2,3,4}}; //sets answer choices
for (unsigned int i =0; i<vecAnswers.size(); i++) //as long as there are questions
{
    int answers_index = i;
    cout << vecQuestions[i];
    for (int x:answer_choices) // for all four values of array answer choices
    {
        cout << x << ")" << vecAnswers[answers_index % vecAnswers.size() ] << " ";
        answers_index++;
    }
    cout <<"\n\n";
}
Peng Zhang
  • 3,475
  • 4
  • 33
  • 41
  • Thanks for the improved logic; that's the reason I selected this response as the answer. I never thought of using the modular to solve this. Thanks. – Andrue Jan 16 '14 at 02:28
  • @Cyris No thanks. It took me a while to understand what you need to implement. :-) – Peng Zhang Jan 16 '14 at 02:29
1

It's not entirely clear to me what you want this code to do but these lines look to be an issue:

int values_left = vecAnswers.size() - i-1;
if ( values_left < answers_index)
{
    answers_index =0; //--- ends up setting all values to 1
}

I believe you want:

if (answers_index >= vecAnswers.size())
{
    answers_index =0; //--- ends up setting all values to 1
}

or more concisely:

answers_index %= vecAnswers.size();

otherwise it just keeps resetting answers_index in later iterations of the inner loop which is why you're seeing the unexpected output.

StevieB
  • 982
  • 7
  • 15
  • The ouput for the first three questions is correct, but the problem still arises in the fourth and fifth question. `Fourth Question: 1)a 2)b 3)c 4)a` `Fifth Question: 1)a 2)b 3)c 4)a` – Andrue Jan 16 '14 at 01:48
  • Thanks. It works! I've been trying to figure this out for a week now. I can't believe the solution was that simple. Quick question: would the program run faster if I changed it to: `if (answers_index > vecAnswers.size()-1)` ? – Andrue Jan 16 '14 at 02:11
  • Possibly. It depends on how > and >= are implemented in your compiler. Any difference will be pretty minimal so I would go with whichever you find more readable as this is more beneficial in the longer term. Also, I wouldn't be concerned with performance until you actually see a slowdown in your program that concerns you. Once you have a performance issue there is no substitute for measuring the program's performance with profiling tools. Even pros with years of experience can rarely guess where the real performance issues lie. – StevieB Jan 16 '14 at 02:19