|
-
February 3rd, 2009, 06:39 PM
#1
Total newbie problem.. corrupted stack around variable
I've spent a long, long time trying to figure out what I'm doing wrong, and I still can't find it. I'm not used to Visual Studio's Debugger, and I hope you all won't mind me asking for help. I'm getting a stack around variable error, which I know is when something tries to write out of bounds. Anyway, here's the code (my first attempt at a C++ class)
Code:
#include <iostream>
#include <string>
using namespace std;
class Studentgrade
{
private:
char student[24];
int quizzes[4];
public:
void askName()
{
cout << "Please enter the student's name: ";
cin.getline(student, 24);
quizzes[0] = 0;
quizzes[1] = 0;
quizzes[2] = 0;
quizzes[3] = 0;
}
void setGrade(int sc, int x)
{
if(x>=0 && x<4)
{
quizzes[x] = sc;
}
}
void getName(char name[]) const
{
strcpy_s(name, strlen(name), student);
}
float getAve() const
{
int tot = 0;
tot = (quizzes[0] + quizzes[1] + quizzes[2] + quizzes[3]);
return (tot/static_cast<float>(4.0));
}
};
int main()
{
Studentgrade sg;
char student[24];
sg.askName();
sg.setGrade(100,0);
sg.setGrade(90,1);
sg.setGrade(100,5);
sg.setGrade(100,3);
sg.getName(student);
cout << student << "'s quiz average is " << sg.getAve() << endl;
return 0;
}
If I debug with "john smith," the string never even makes it into the sg.student instance. I guess I don't understand how member functions are supposed to write to the instance rather than original class declaration (if that makes sense)
Sigh.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|