-2

I have to write a program that takes a Student's marks in the following format from a .dat file

COS1511 30 66 70 49
COS1512 25 76 75 67
COS1521 10 58 90 62
COS1501 50 62 50 57
INF1501 40 82 60 78
INF1511 20 24 80 55

The fields are:

Subject ID, Weight of assignment 1 in %, Assignment 1 in %, Weight of assignment 2 in %, Assignment 2 in %,

The output should be the final mark, written to .dat like this:

COS1511 54.10%
COS1512 69.25%
COS1521 61.60%
COS1501 59.50%
INF1501 79.60%
INF1511 48.80%

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

bool isNumber(string str)
{
    for (char c : str) {
        if (isdigit(c) == 0) return false;
    }
    return true;
}

int main()
{
    ifstream inStream;
    ofstream outStream;
    string str;
    vector<int> Marks;
    vector<string> Subjects;
    vector<double> calcdMarks;
    int i = 0;


    inStream.open("Input.dat");
    outStream.open("Output.dat");
    
    while (inStream >> str) {
        if (isNumber(str)) {
            Marks.push_back(stoi(str));
        }
        else
            Subjects.push_back(str);
    }

    for (i = 0; i <= Subjects.size()-1; i++) {
        calcdMarks.push_back(Marks[i] * (Marks[i + 1] / 100));
        calcdMarks.push_back(Marks[i + 2] * (Marks[i + 3] / 100));
    }

    for (i = 0; i <= Subjects.size()-1; i++) {
        cout << Subjects[i] << " " << calcdMarks[i] << " " << calcdMarks[i + 1] << endl;
    }

    inStream.close();
    outStream.close();
}

The problem I'm experiencing is that my program output's the following:

COS1511 0% 0%
COS1512 0% 0%
COS1521 0% 0%
COS1501 0% 0%
INF1501 0% 0%
INF1511 0% 0%

I can't seem to find the reason why. I suspect the problem is here:

 for (i = 0; i <= Subjects.size()-1; i++) {
        calcdMarks.push_back(Marks[i] * (Marks[i + 1] / 100));
        calcdMarks.push_back(Marks[i + 2] * (Marks[i + 3] / 100));
    }

I'm getting the following warning aswell with Marks[i] * (Marks[i + 1] / 100 and Marks[i + 2] * (Marks[i + 3] / 100)

"using operator '*' on a 4 byte value and then casting the result to a 8 byte value"

  • Does [this](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) help you get started on why the `0`s are popping up? – Nathan Pierson May 23 '21 at 15:53
  • And when you used your debugger to run your program, one line at a time, and inspect the values of all variables as they change, and produce the observe output, what did you see? – Sam Varshavchik May 23 '21 at 15:54
  • `Marks[i] * (Marks[i + 1] / 100)` is the same as `0`, since `Marks[x]` is between 0 and 100. – Eljay May 23 '21 at 15:56
  • 1
    Got it. I needed to cast my integers to doubles. – Christopher May 23 '21 at 15:58
  • FYI -- Your program does no checking to see if those vectors actually have values, and check that `Marks` has all of those entries. If the input file cannot be opened, that `for` loop will go into hyperspace. – PaulMcKenzie May 23 '21 at 16:09

1 Answers1

0

stoi convert String to Integer. try to convert result to double.

Return Value:
On success, the function returns the converted integral number as an int value.