• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

C++ Help?

Status
Not open for further replies.

AcciDante

Member
I'm in an 'Intro to C++' class, and have an assignment to write a program that basically takes answers to a test, and grades it. I finished writing it, but as I was going through and cleaning up errors, I got a 'Signed/Unsigned Mismatch' error. I've never run into this before and was looking for some help. The error is in the for loop at the very bottom of the last function.

Code:
#include <iostream>
#include <vector>
using namespace std;

void getAnswers(char [], int);
void standardAnswers(char [], int);
void gradeTest(char [], char [], int, vector<int>);
void outputGrade(vector<int>, int);

int main()
{
    const int SIZE = 20;
    char testAnswers[SIZE] = { 'B', 'D', 'A', 'A', 'C',
                                'A', 'B', 'A', 'C', 'D',
                                'B', 'C', 'D', 'A', 'D',
                                'C', 'C', 'B', 'D', 'A' };
    char testStudent[SIZE];
    vector<int> incorrect;

    cout << "This program will let you enter your answers for the Driver's \n"
        << "License Exam, and give you a grade. Enter your answers below.\n";
    cout << "Only A, B, C, and D are valid answers.\n";

    getAnswers(testStudent, SIZE);
    standardAnswers(testStudent, SIZE);
    gradeTest(testAnswers, testStudent, SIZE, incorrect);
    outputGrade(incorrect, SIZE);

    return 0;
}

void getAnswers(char answers[], int size)
{
    for (int count = 0; count < size; count++)
    {
        cout << "Question " << (count + 1) << ": ";
        cin >> answers[count];

        while (answers[count] != 'A' && answers[count] != 'a' &&
                answers[count] != 'B' && answers[count] != 'b' &&
                answers[count] != 'C' && answers[count] != 'c' &&
                answers[count] != 'D' && answers[count] != 'd')
        {
            cout << "ERROR: Only A,B,C, and D are valid.\n";
            cout << "Question " << (count + 1) << ": ";
            cin >> answers[count];
        }
    }
}

void standardAnswers(char answers[], int size)
{
    for (int count = 0; count < size; count++)
    {
        switch (answers[count])
        {
            case 'a': answers[count] = 'A';
            case 'b': answers[count] = 'B';
            case 'c': answers[count] = 'C';
            case 'd': answers[count] = 'D';
        }
    }
}

void gradeTest(char testAnswers[], char testStudent[], int size, vector<int> incorrect)
{
    for (int count = 0; count < size; count++)
    {
        if (testAnswers[count] != testStudent[count])
            incorrect.push_back(count + 1);
    }
}

void outputGrade(vector<int> incorrect, int questions)
{
    if (incorrect.size() <= 5)
        cout << "You passed the exam! Congratulation!\n";
    else
        cout << "You FAILED the exam.\n";

    cout << "You answered " << (questions - incorrect.size()) << " questions correctly, and\n"
        << incorrect.size() << " questions incorrectly.\n";

    cout << "The questions you answered incorrectly were numbers ";
    for (int i = 0; i < (incorrect.size() - 1); i++)     //signed/unsigned mismatch here
        cout << incorrect[i] << ", ";
    cout << "and " << incorrect[incorrect.size] << ".\n";
}

I figure its something to do with that vector size comparison? I just learned about vectors today, and this is my first use of them :lol

Thanks!
 
incorrect.size() returns an unsigned integer. You could either cast it to an int or just change the loop variable i to be unsigned as well, if you want to get rid of the warning.
 

Fersis

It is illegal to Tag Fish in Tag Fishing Sanctuaries by law 38.36 of the GAF Wildlife Act
rooflapimp said:
incorrect.size() returns an unsigned integer. You could either cast it to an int or just change the loop variable i to be unsigned as well, if you want to get rid of the warning.
Exactly.
Use :
for (unsigned int i = 0; i < (incorrect.size() - 1); i++)
insted of :
for (int i = 0; i < (incorrect.size() - 1); i++)

In before programming Languages wars!
 

Fersis

It is illegal to Tag Fish in Tag Fishing Sanctuaries by law 38.36 of the GAF Wildlife Act
poweld said:
But perl wouldn't make you...

Aw man, I'm just joshin' :D
VBScript FOR THE MOTHER EFFIN WIN!!!

DEAL WITH IT!
:lol

These threads always end up like this.
 

Kinitari

Black Canada Mafia
In programming class I am still way ahead of the curve, so I don't need help yet - although I'm going to guess within 2 months Ima be on the OT begging for help.
 

XiaNaphryz

LATIN, MATRIPEDICABUS, DO YOU SPEAK IT
Relix said:
*Hugs back*

Honestly, VB is the only language that makes sense to me followed by C# :lol
Pffft...any hardcore compiler guys around here? Let's throw some assembly into the mix. ;P
 

BreakyBoy

o_O @_@ O_o
Visual Basic? Really?

*shakes his head*


In all seriousness, I started learning with BASIC, Pascal & C, and lately I've moved on to PHP, Java and C#. Still, I don't think I'll ever enjoy coding in anything as much as I do in C++. It's got it's warts and all, but it's still wonderfully flexible/powerful, and I can probably credit my love for coding to my time learning it as a teenager.


XiaNaphryz said:
Pffft...any hardcore compiler guys around here? Let's throw some assembly into the mix. ;P

Fuck yeah! Z80 and 8086 in da house. :p
 

Celsior

Member
XiaNaphryz said:
Pffft...any hardcore compiler guys around here? Let's throw some assembly into the mix. ;P
I write in assembly, I am that hardcore.
I should learn VB one day but I am a C type of guy.
 

Chris R

Member
Relix said:
*Hugs back*

Honestly, VB is the only language that makes sense to me followed by C# :lol
Non-short circuit logic by default says WHAT.

Kinitari said:
In programming class I am still way ahead of the curve, so I don't need help yet - although I'm going to guess within 2 months Ima be on the OT begging for help.

Feel free to ask man, we really should just make a Programming Help |OT| or something.

Speaking of programming help...

On P.E. 267 is the entire problem from scratch supposed to be solvable in the time period or can I find f outside of that time limit? I mean I guess I could use something to find f mathematically, but I'd rather use something like a genetic algorithm :D
 

dogmaan

Girl got arse pubes.
XiaNaphryz said:
Pffft...any hardcore compiler guys around here? Let's throw some assembly into the mix. ;P

mov yourass
jmp intobedwithyourmum


on a serious note if you are going to use assembly, create a scalar function first, then wrap your asm in

#ifdef __ASMISNOTPORTABLEDONTBEANIDIOTTHINKOFTHECHILDEN
 
itxaka said:
Python ftw!:D
Yes sir. I never actually programmed in C++ but that syntax is seriously not pretty. I like Java too (and I'm probably more competent in it) but it's not as fun to use as Python. Loading files and reading them in Java is quite a pain. Same for the BigInteger and BigDecimal classes. They do have some nice built-in algorithms (like primality tests, GCD, divide with remainder etc) but it's pretty much unreadable if you want to do more complicated stuff.
 

survivor

Banned
My intro to programming class is Java. I wonder if that's a good thing or not.

Though this thread should help since we just went over loops and I think we will get a similar assignment to the one in the OP.
 

Gomu Gomu

Member
XiaNaphryz said:
Pffft...any hardcore compiler guys around here? Let's throw some assembly into the mix. ;P
Fuck assembly. I took an Assembly/CPU design course last semester. It was one of the hardest courses ever. I barely made it :lol .
 

BreakyBoy

o_O @_@ O_o
Gomu Gomu said:
Fuck assembly. I took an Assembly/CPU design course last semester. It was one of the hardest courses ever. I barely made it :lol .

Did your final involve making logic gate diagrams that covered multiple sheets of paper? Those are fun. :lol
 

Chris R

Member
Gomu Gomu said:
Fuck assembly. I took an Assembly/CPU design course last semester. It was one of the hardest courses ever. I barely made it :lol .
I actually liked that class. The ones I hated were Networks (due to the professor only) and half of my Algorithms/Automata (mostly the Big Oh stuff :( )
 

Gomu Gomu

Member
BreakyBoy said:
Did your final involve making logic gate diagrams that covered multiple sheets of paper? Those are fun. :lol
As bad as that was, writing an application using Assembly was 100 times worse. Thank god for high-level programming languages.
 

Ydahs

Member
I just restarted learning C++ yesterday and I must say that I don't think any language will replace my first love: Python.
 
You're accessing outside the bounds of your array. You should be able to see where if you step through it with the debugger but you should also be able to catch it with a quick look at the code.
 

m3k

Member
not that i can program for shit and i did it like 5 years ago but... people like vb? :lol perhaps my teacher was shit

i liked java :lol
 

AcciDante

Member
Code:
#include <iostream>
#include <vector>
using namespace std;

void getAnswers(char [], int);
void standardAnswers(char [], int);
void gradeTest(char [], char [], int, vector<int>);
void outputGrade(vector<int>, int);

int main()
{
    const int SIZE = 20;
    char testAnswers[SIZE] = { 'B', 'D', 'A', 'A', 'C',
                                'A', 'B', 'A', 'C', 'D',
                                'B', 'C', 'D', 'A', 'D',
                                'C', 'C', 'B', 'D', 'A' };
    char testStudent[SIZE];
    vector<int> incorrect;

    cout << "This program will let you enter your answers for the Driver's \n"
        << "License Exam, and give you a grade. Enter your answers below.\n";
    cout << "Only A, B, C, and D are valid answers.\n\n";

    getAnswers(testStudent, SIZE);
    standardAnswers(testStudent, SIZE);
    gradeTest(testAnswers, testStudent, SIZE, incorrect);
    outputGrade(incorrect, SIZE);

    return 0;
}

void getAnswers(char answers[], int size)
{
    for (int count = 0; count < size; count++)
    {
        cout << "Question " << (count + 1) << ": ";
        cin >> answers[count];

        while (answers[count] != 'A' && answers[count] != 'a' &&
                answers[count] != 'B' && answers[count] != 'b' &&
                answers[count] != 'C' && answers[count] != 'c' &&
                answers[count] != 'D' && answers[count] != 'd')
        {
            cout << "ERROR: Only A,B,C, and D are valid.\n";
            cout << "Question " << (count + 1) << ": ";
            cin >> answers[count];
        }
    }
}

void standardAnswers(char answers[], int size)
{
    for (int count = 0; count < size; count++)
    {
        switch (answers[count])
        {
            case 'a': answers[count] = 'A';
            case 'b': answers[count] = 'B';
            case 'c': answers[count] = 'C';
            case 'd': answers[count] = 'D';
        }
    }
}

/**************************
gradeTest below doesn't seem to be working.  I get that there were none incorrect, when there are.
**************************/

void gradeTest(char testAnswers[], char testStudent[], int size, vector<int> incorrect)
{
    for (int count = 0; count < size; count++)
    {
        if (testAnswers[count] != testStudent[count])
            incorrect.push_back(count + 1);
    }
}

void outputGrade(vector<int> incorrect, int questions)
{
    if (incorrect.size() <= 5)
        cout << "You passed the exam! Congratulations!\n";
    else
        cout << "You FAILED the exam.\n";

    cout << "You answered " << (questions - incorrect.size()) << " questions correctly, and\n"
        << incorrect.size() << " questions incorrectly.\n";


\****
The if statement below got rid of the 'vector over bounds' error, so I assume the error is in there.
*****/

    if (incorrect.size() > 0)
    {
        cout << "The questions you answered incorrectly were numbers ";
        for (int i = 0; i < static_cast<int>(incorrect.size()); i++)
            cout << incorrect[i] << ", ";
        cout << "and " << incorrect[incorrect.size()] << ".\n";
    }
}

I wrote some comments above the bottom if statement, and above the gradeTest function further up. I don't know what is wrong :(
 
Scripting languages are for boys. C++ is for men.

All of them have their uses.


OP:
Code:
void gradeTest(char testAnswers[], char testStudent[], int size, vector<int> incorrect)
{
    for (int count = 0; count < size; count++)
    {
        if (testAnswers[count] != testStudent[count])
            incorrect.push_back(count + 1);
    }
}
When you call this function (or any of the functions where you're passing a vector in), you're creating a copy of vector<> incorrect, changing it, and returning. The copy then goes out of scope and is destroyed. You're not modifying the original vector.

The cool, C++ way to do this is:

Code:
void gradeTest(char testAnswers[], char testStudent[], int size, vector<int>& incorrect)
{
    for (int count = 0; count < size; count++)
    {
        if (testAnswers[count] != testStudent[count])
            incorrect.push_back(count + 1);
    }
}
...pass by reference. What that means is that you're passing in a "reference" to some vector stored elsewhere, and using that. This is what languages like C# and Java do implicitly for you. C style would be to make it a pointer, but pointers invite unnecessary pain.
 

Mighty

Member
I took a very quick glance at your code and this is what I saw.

1. This line will give you the vector subscript out of range:

cout << "and " << incorrect[incorrect.size()] << ".\n";

Array indices in C++ start at 0 and go to size-1. That line of code is attempting to access an element one past the end of the array.

2. Parameters to functions in C++ are passed by value, unless you specify to pass by reference (using an ampersand '&'). In your case, the last parameter to gradeTest is being passed by value, so any modifications you do to the variable inside the function are actually modifying a copy of the variable. This is a fairly simple, yet important programming concept that any good book or website will explain in detail.

EDIT: Whoa, I was hecka beaten!
 

dabig2

Member
I used to be a hardcore C++ lover. Like I was doing object-oriented C++ and polymorphism and all that shit.

Then I started Java and it blew my mind on how harder I was making my life with C++ (blah blah memory pitfalls, etc)
 

fiz

Neo Member
I've always been partial to C. I work with Python and a proprietary format that's very similar to assembly, though. Occasionally I work a bit with C++ but probably only once every few weeks. Luckily I don't have to look at java. I work with memory (DRAM) though, so I'm all for the low level stuff :D .
 

Doytch

Member
after working the past 16 months on driver/kernel crap, ive really started to hate system-level code. fuck that noise, GUIs please.
 

C-Jo

Member
I just started learning C#. It's kicking my ass so far, but much to my surprise, I think I'm actually enjoying it.
 

Chris R

Member
C-Jo said:
I just started learning C#. It's kicking my ass so far, but much to my surprise, I think I'm actually enjoying it.
C# is very nice. Not great for everything obviously, but if you aren't needing the raw horsepower of something like C/C++ then C# isn't a bad choice.
 
Status
Not open for further replies.
Top Bottom