I'm trying to make a program that reads input from a file (it is named grades.txt) and make an output of (image attached)
Apparently, I'm converting strings to char arrays and my program's output quite unexpected (image attached) I have checked twice, the IDE doesn't show any errors as well.
I'm using this as the source code.
#pragma warning(disable:4996)
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
//Author: Hidden for privacy
const int MAXNAME = 20;
int main()
{
ifstream inData;
inData.open("grades.txt");
string rawInputString;
char name[MAXNAME + 1]; // holds student name
float average; // holds student average
inData.get(name, MAXNAME + 1);
while (inData)
{
bool firstSpace = false;
bool secondSpace = false;
char converter[23];
getline(inData, rawInputString, '\n');
strcpy(converter, rawInputString.c_str());
for (int a = 0; a <= 22; a++) {
if (converter[a] != ' ') {
cout << converter[a];
}
if (converter[a] == ' ') {
if (!firstSpace) {
firstSpace = true;
continue;
}
if (!secondSpace) {
secondSpace = true;
continue;
}
}
if (firstSpace) {
cout << converter[a];
if (secondSpace) {
cout << converter[a];
}
}
}
}
inData.close();
return 0;
}
Here is the grades.txt file:
Adara Starr 94
David Starr 91
Sophia Starr 94
Maria Starr 91
Danielle DeFino 94
Dominic DeFino 98
McKenna DeFino 92
Taylor McIntire 99
Torrie McIntire 91
Emily Garrett 97
Lauren Garrett 92
Marlene Starr 83
Donald DeFino 73
What I've tried: As seen in the source code, I tried to make the program print the output char by char. But apparently, the first few characters printed are not even in the file where the program is taking input from. I know that float average and char array name are uninitiated, but it doesn't affect the rest of the code (too much), so I'm just leaving those there. I tried reading the fstream library to maybe figure out if this was something caused by the ifstream, but that doesn't seem to be the case either.