-1
#include <iostream>

using namespace std;

int grade(char [][5], int);

int main()
{
    int tests, i,j;
    cout << "How many tests?"; cin >> tests;
    char answers[tests][5];
    cout << "What were the answers for all the tests (T/F)?";
    for (i=0;i<tests;i++)
        for (j=0;j<5;j++) cin >> answers[i][j];;
    int g[tests] = {grade(answers, tests)};

    for (i=1;i<=tests;i++)
    {
        cout << "\n   Test " << i << ": ";
        cout << g[i-1] << " out of 25";
    }
    cout << endl;

    return 0;
}

int grade(char ans[][5], int test)
{
    int k, l;
    int gr[test]={0};
    char sheet[5] = {'T', 'T', 'F', 'F', 'T'}; cout << sheet[1];
    for (k=0;k<test;k++)
            for (l=0;l<5;l++)
                    if (ans[k][l]==sheet[l]) gr[k]+= 5;;;

    return *gr;
}

I can't figure out why my program is so inconsistent. If I input T T F F T for the first test, everything turns out fine, but if I have any "wrong answers" for the first test, the values of my g array seem to always be 0.

Any ideas? Thanks!

  • 6
    Too complicated. Use `std::vector`. – Christian Hackl Nov 19 '17 at 20:49
  • what's with multiple semicolons? – Killzone Kid Nov 19 '17 at 20:57
  • 1
    `int gr[test]={0};` is not valid standard C++ – UnholySheep Nov 19 '17 at 20:58
  • “Use `std::vector`” is not a useful comment; it does not solve the problem(s), instead seeking to change OP’s structure without explaining what benefit it has/problem it obviates. Perhaps pointing out that `grade()` could use to take a single-dimension (sub)array to simplify instead... – Dúthomhas Nov 19 '17 at 21:00
  • Your function returns a single int . I guess you imagine to return the entire `gr` but you do not – M.M Nov 19 '17 at 21:00
  • 2
    @Dúthomhas: *"Use std::vector” is not a useful comment;"* - Yes, it is. Even if it does not solve the problem directly, it makes the code shorter, more readable and more debuggable, contributing to a solution in the long run. So let me reiterate it: **Use `std::vector`.** – Christian Hackl Nov 19 '17 at 21:10
  • I've no idea what std::vector does, so I cannot use this advice. M M So are you saying that when I return the gr array, it only sends one value? – Robert Paulson Nov 19 '17 at 21:16
  • 1
    @RobertPaulson: `std::vector` is practically the default container of C++. What book are you using that does not teach you how to use the default container of the language?! See http://en.cppreference.com/w/cpp/container/vector – Christian Hackl Nov 19 '17 at 21:21
  • @ChristianHackl At this point I doubt this is C++ related question at all – Killzone Kid Nov 19 '17 at 22:10

1 Answers1

0

Here's the problem. The return value of the function returns a single number, and then assigns that single number to all values of the array. So, whatever the first test's grade was, all grades read the same.

The {} after an array sets the initial values for the array, and only having one value in that makes that value be assigned to all parts of the array.

My compiler doesn't like your syntax of int g[tests], as it expects tests to be a constant, so I had to convert those dynamic array size declarations. The following code worked as expected for me on Visual Studio 2017. Try this.

#include <iostream>

using namespace std;

int* grade(char** ans, int test);

int main()
{
    int tests, i, j;
    cout << "How many tests?"; cin >> tests;

    char** answers = new char*[tests];
    for (int i=0;i<tests;i++)
        answers[i] = new char[5];

    cout << "What were the answers for all the tests (T/F)?";
    for (i = 0; i<tests; i++)
        for (j = 0; j<5; j++) cin >> answers[i][j];

    int* g = grade(answers, tests);

    for (i = 1; i <= tests; i++)
    {
        cout << "\n   Test " << i << ": ";
        cout << g[i - 1] << " out of 25";
    }
    cout << endl;

    for (int i = 0; i < tests; i++)
        delete[] answers[i];
    delete[] answers;
    delete[] g;

    system("pause");

    return 0;
}

int* grade(char** ans, int test)
{
    int k, l;
    int* gr = new int [test] {0};
    char sheet[5] = { 'T', 'T', 'F', 'F', 'T' }; 
    cout << sheet[1];
    for (k = 0; k<test; k++)
        for (l = 0; l<5; l++)
            if (ans[k][l] == sheet[l]) gr[k] += 5;;;

    return gr;
}
pepperjack
  • 627
  • 6
  • 20
  • I'm using codeblocks, if that helps anyhow. I'm confused at what you did. for (i=0;i – Robert Paulson Nov 19 '17 at 21:53
  • @RobertPaulson When I dynamically allocate the arrays, they must be deallocated manually, done by delete[] ing them. I do a for loop since I want to delete all the arrays within an array. The operating system would also do this automatically if you wanted to ignore it, but that would be bad coding practice, and could lead to a memory leak. – pepperjack Nov 19 '17 at 22:02
  • @RobertPaulson I return the int* in the function since I have already created the array in the function, then I pass the location to `int* g`, and use that in the output of the program. I then `delete[]` that since it won't delete automatically. More about delete[] and dynamic arrays: https://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers – pepperjack Nov 19 '17 at 22:04
  • @RobertPaulson Did this answer your question? – pepperjack Nov 19 '17 at 23:05
  • When I use int* for the function protocol, I can't use return*, and then my program outputs super high numbers for the g[tests] values. =( – Robert Paulson Nov 20 '17 at 04:24
  • @RobertPaulson I'm sorry but I don't understand what you just said. Can you elaborate with some code perhaps? – pepperjack Nov 20 '17 at 04:26
  • int grade(char [][5], int); when I use int* instead of int here, when I call my function, and at the bottom where I define my function, I can't use return* gr and my "Test # out of 25" statements shoot up to 49235. – Robert Paulson Nov 20 '17 at 19:33
  • @RobertPaulson I still don't entirely understand what you are saying, so I'll just try to hit a few points and see if I get it... First of all, you don't do `return* gr`, just `return gr`. `return* gr` is actually `return *gr`, which just returns a single integer instead of the array. Also, make sure the declaration, `int* grade(char** ans, int test);` matches with the definition `int* grade(char** ans, int test) { ...`. Next, make sure you are using the code I supplied; I gave you a working solution. Your `int grade(char [][5], int); ` is not going to work with the method I supplied. – pepperjack Nov 20 '17 at 22:42
  • If you are having further issues, please post the code you are using on something and give me the link to it. – pepperjack Nov 20 '17 at 22:48
  • I literally copy and pasted your program and it worked haha. I've got to go to class now, but I'll elaborate my question again. I still don't get how my program was problematic after I edited it with your help. Thank you so much! – Robert Paulson Nov 20 '17 at 23:51