|
-
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.
-
February 3rd, 2009, 06:51 PM
#2
Re: Total newbie problem.. corrupted stack around variable
Oh... I guess this should be in "C++ Programming" and not "Visual C++."
-
February 3rd, 2009, 07:13 PM
#3
Re: Total newbie problem.. corrupted stack around variable
Save yourself a great deal of time and trouble: Use std::strings rather than char arrays. You're already including the header for them anyway.
-
February 3rd, 2009, 08:04 PM
#4
Re: Total newbie problem.. corrupted stack around variable
sg.setGrade(100,5);
you don't have fifth element in int quizzes[4];
should be:
sg.setGrade(100,2);
-
February 3rd, 2009, 08:51 PM
#5
Re: Total newbie problem.. corrupted stack around variable
-
February 3rd, 2009, 09:08 PM
#6
Re: Total newbie problem.. corrupted stack around variable
 Originally Posted by Lindley
^Not the problem.
Its not. Its bug.
void getName(char name[]) const
also have a problem/bug as you wish. You should pass parameter as reference.
-
February 3rd, 2009, 10:33 PM
#7
Re: Total newbie problem.. corrupted stack around variable
 Originally Posted by streamer
Its not. Its bug.
Maybe; or maybe it's a test to make sure that the "if" statement within the function properly catches bad input.
void getName(char name[]) const
also have a problem/bug as you wish. You should pass parameter as reference.
Reference passing is pretty much irrelevant in the case of arrays. If he were to substitute a std::string that would be true.
-
February 4th, 2009, 04:15 AM
#8
Re: Total newbie problem.. corrupted stack around variable
 Originally Posted by Georgie Fruit
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:
void getName(char name[]) const
{
strcpy_s(name, strlen(name), student); //are you sure?
}
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.
Well your askName() method works fine for me, are you sure it gives you a problem? Regardless, you ought to look again at the way you're using strcpy_s, particularly the way you're telling it how long the source string is, hmm? Also, in a little project like this it's a good idea to not give a local variable the same name as a member (as you have with student in main()), the compiler will cope perfectly well but it can confuse the human.
HTH
-
February 4th, 2009, 08:04 AM
#9
Re: Total newbie problem.. corrupted stack around variable
I believe GrimmRiffer is on the right track.
You're strcpy_s function is incorrect...the second parameter is the size of the DESTINATION buffer - in this case it should be 24.
By using
Code:
strcpy_s(name, strlen(name), student);
you're telling it that the buffer is only the same length as the name - which doesn't leave room for the terminating NULL character.
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
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
|