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"