Command Line Arguments, Weird Results

Hello, I'm working on an assignment to create a program that takes in 4 test grades and returns a letter grade. The first two grades are 0-10, the second two are 0-100. When I enter the command "./exercise3 10 10 100 100", it returns an A, just as it should. But when I try different grades, like "./exercise3 8 8 85 85", it returns an A as well. I know my math is correct because the program works fine with normal console input, so something is wrong in how I've written my input function involving command line arguments. It's probably something stupid but I'm out of ideas at this point. Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;

struct scores
{
    int quiz1, quiz2, midterm, final;
};

scores input(char *input1, char *input2, char *input3, char *input4);
char finalGrade(scores s);

int main(int argc, char* argv[])
{
    scores s;

    s = input(argv[1], argv[2], argv[3], argv[4]);

    cout << "\nThe student's final grade is a(n) " << finalGrade(s) << ".\n";

    return 0;
}

scores input(char *input1, char *input2, char *input3, char *input4)
{
    scores newScores;

    newScores.quiz1 = input1[1];
    newScores.quiz2 = input2[2];
    newScores.midterm = input3[3];
    newScores.final = input4[4];

    return newScores;
}

char finalGrade(scores s)
{
    char finalGrade;
    int gradePercentage;

    gradePercentage = ((((s.quiz1 + s.quiz2)*25)/20) + ((s.midterm*25)/100) + ((s.final*50)/100));

    if (gradePercentage >= 90)
    {
        finalGrade = 'A';
    } 
    else if ((gradePercentage < 90) && (gradePercentage >= 80))
    {
        finalGrade = 'B';
    }
    else if ((gradePercentage < 80) && (gradePercentage >= 70))
    {
        finalGrade = 'C';
    }
    else if ((gradePercentage < 70) && (gradePercentage >= 60))
    {
        finalGrade = 'D';
    }
    else
    {
        finalGrade = 'F';
    }

    return finalGrade;
}
Last edited on
newScores.quiz1 = input1[1]; What are you doing here (and on following lines)? How that was supposed to work?
You are assigning second character in input1 to quiz1. If input1 is 10 then it will be int('0') → 47. quiz2 and others gets some junk from buffer which can be anything from 0 to 255 (or -128 to 127 depending on your compiler).
Try to print scores data before calculating grade and you will see that.
Last edited on
I see... I know it looked funny but I couldn't get anything else to compile. That makes sense though. How would I go about passing in the entire character array?
Thanks so much! It works fine now. To anyone else who might come across this, I used the atoi function as follows:

1
2
3
4
    newScores.quiz1 = atoi (input1);
    newScores.quiz2 = atoi (input2);
    newScores.midterm = atoi (input3);
    newScores.final = atoi (input4);
Last edited on
Topic archived. No new replies allowed.