lundi 16 mars 2015

cin crashing my program after 12 for loop iterations?


I've never posted here before but I'm really stuck so I thought i'd give it a try. I've been working on this code for a while, The aim is to input a few students with their marks and to output them into tables with averages and totals. I was given a file like this:


15 Albert Einstein 52 67 63 Steve Abrew 90 86 90 93 David Nagasake 100 85 93 89 Mike Black 81 87 81 85 Andrew Van Den 90 82 95 87 Joanne Dong Nguyen 84 80 95 91 Chris Walljasper 86 100 96 89 Fred Albert 70 68 Dennis Dudley 74 79 77 81 Leo Rice 95 Fred Flintstone 73 81 78 74 Frances Dupre 82 76 79 Dave Light 89 76 91 83 Hua Tran Du 91 81 87 94 Sarah Trapp 83 98


my problem is that when I am inputting the names the program crashes after Fred Flinstone, I know its not a problem with the formatting of the next name (Frances Dupre) because when i moved him up the list it read it fine.


I have located where the program is crashing with 'cerr' outputting at different stages of the read process and it crashes when it is trying to read in Frances's marks.


a1main.cpp



#include <iostream>
#include "student.h"
using namespace std;

int main()
{
int numStds;
cin >> numStds;

cerr << endl << "Num Stds: " << numStds << endl;

Student std[numStds+1];

for(int i = 0; i <= numStds; i++)
{
std[i].readData();
std[i].printStudent();
}

// delete [] std;

return 0;
}


student.h



#include <iostream>
using namespace std;

class Student {

private:

char* name;
int mark[4];
int num;

public:

Student();
~Student();

void readData();
void printStudent();
float getTotal();
float getAverage();
};


student.cpp



#include <iostream>
#include <cstring>
#include <cctype>
#include "student.h"
using namespace std;

Student::Student()
{
name = new char;
mark[0] = 0;
mark[1] = 0;
mark[2] = 0;
mark[3] = 0;
num = 0;
}

Student::~Student()
{
// Doesn't work?

// delete name;
}

void Student::readData()
{
int l = 0;

// Reading the Name
cin >> name; // Read in the first name
l = strlen(name); // get the strlength
name[l] = ' '; // Putting a space between the first and last name
cin >> &name[l+1]; // Read in the last name

cerr << endl << "I have read the name!" << endl;

// Checking if there is a third name
if(cin.peek() == ' ')
cin >> ws; // checking and navigating past the whitespace

char next = cin.peek();
if( isalpha(next) ) // Checking whether the next cin is a char
{
l = 0;
l = strlen(name);
name[l] = ' ';
cin >> &name[l+1];
}

cerr << "I've checked for a third name!" << endl;

// Reading in the marks
for(int i = 0; i < 4; i++)
{
// Checks if the next cin is a newline
if (cin.peek() == '\n')
break;

cin >> mark[i];
}

cerr << "I've read in the marks!" << endl;

//cerr << endl << "I have read " << name << "'s marks!" << endl << endl;

for(int m = 0; m < 4; m++)
{
if(mark[m] != 0)
{
num++;
}
}

cerr << "I've incremented num!" << endl << endl;
}

// Function for error checking
void Student::printStudent()
{
cout << endl << "Student Name: " << name << endl;
cout << "Mark 1: " << mark[0] << endl;
cout << "Mark 2: " << mark[1] << endl;
cout << "Mark 3: " << mark[2] << endl;
cout << "Mark 4: " << mark[3] << endl;
cout << "num marks: " << num << endl << endl;
}

float Student::getTotal()
{}
float Student::getAverage()
{}


Can anyone see what i'm doing wrong? thanks :)




Aucun commentaire:

Enregistrer un commentaire