c++ - cin crashing my program after 12 for loop iterations? -
i've never posted here before i'm stuck thought i'd give try. i've been working on code while, aim input few students marks , output them tables averages , totals. given file 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 when inputting names program crashes after fred flinstone, know not problem formatting of next name (frances dupre) because when moved him list read fine.
i have located program crashing 'cerr' outputting @ different stages of read process , crashes when trying 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 = 0; <= 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 name cin >> name; // read in first name l = strlen(name); // strlength name[l] = ' '; // putting space between first , last name cin >> &name[l+1]; // read in last name cerr << endl << "i have read name!" << endl; // checking if there third name if(cin.peek() == ' ') cin >> ws; // checking , navigating past whitespace char next = cin.peek(); if( isalpha(next) ) // checking whether next cin char { l = 0; l = strlen(name); name[l] = ' '; cin >> &name[l+1]; } cerr << "i've checked third name!" << endl; // reading in marks for(int = 0; < 4; i++) { // checks if next cin newline if (cin.peek() == '\n') break; cin >> mark[i]; } cerr << "i've read in 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 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 see i'm doing wrong? :)
you're never allocating memory store student names.
in student constructor, add code:
name = new char[100]; // allows names 100 characters long
and uncomment line in destructor:
delete[] name;
you make code more sophisticated , robust measuring name length , allocating correct size, or use std::string suggested below.
Comments
Post a Comment