Run-Time Failure -- stack around variable was corrupted...?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 11867 Views - Last Post: 03 June 2011 - 10:52 PM Rate Topic: -----

#1 theant4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-February 11

Run-Time Failure -- stack around variable was corrupted...?

Posted 01 June 2011 - 11:20 AM

So this is a program I've been working on for awhile and I'm really close to finishing it. I finally got rid of all the syntax/IntelliSense errors and now I'm getting a debug error. Basically the program reads information from a file (the names of 10 school clubs, the names of the club presidents, and the number of members in the club) and then assigns each club $75 per member. In the end I'm just supposed to print the information out and display it in an orderly fashion. It actually works initially but then I get a bunch of random characters and then the debug error "Run time check failure #2: stack around the variable 'clubname' was corrupted." What does this mean and how can I fix it? I'm soooo close!

Below is my output followed by the code:

Computer Systems Society Kim Cares 49 3675.00
Society of Women Engineers Jeanie Queen 51 3825.00
Sigma Tau Gamma Storm Drain 241 18075.00
Trekkies C. Kirk 230 17250.00
Home Brewers Ross Coe 15 1125.00
High Altitude Ballooning Justin Time 19 1425.00
Rugby Ryan Johns 25 1875.00
IEEE Marc Bansmere 36 2700.00
International Club Kong Mbonkum 102 7650.00
Dance Club Will Shaver 64 4800.00
0 0.00
╠╠╠╠╠╠╠╠
Society of Women Engineers -858993460-64424509440.00
╠╠╠╠╠╠╠╠
Sigma Tau Gamma 3748896281167200.00╫ ╠╠╠╠╠╠╠╠

Trekkies -858993460-64424509440.00 ╠╠╠╠╠╠╠╠
Home Brewers -858993460-64424509440.00 ╠╠╠╠╠╠╠╠

High Altitude Ballooning -858993460-64424509440.00
╠╠╠╠╠╠╠╠
Rugby -858993460-64424509440.00 ╠╠╠╠╠╠╠╠
IEEE -858993460-64424509440.00 ╠╠╠╠╠╠╠╠
International Club -858993460-64424509440.00 ╠╠
╠╠╠╠╠╠
Dance Club -858993460-64424509440.00Press any key to continue . . .

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::right;
using std::left;
using std::setw;
using std::setprecision;
using std::ifstream;
using std::ofstream;

const float MONEY = 75.00;

int main()
{
	ifstream infile( "data.txt" );

	char clubname[10][70];
	char presname[10][70];
	char numofmems[10][70];
	int number[10];
	int i = 0;

	if ( infile.is_open() )
	{
		infile.getline(clubname[i], 70, ',');
		infile.getline(presname[i], 70, ',');
		infile.getline(numofmems[i], 70, ',');
		number[i] = atoi(numofmems[i]);

		while ( !infile.eof() )
		{
			++i;
			infile.getline(clubname[i], 70, ',');
			infile.getline(presname[i], 70, ',');
			infile.getline(numofmems[i], 70, ',');
			number[i] = atoi(numofmems[i]);
		}

		for ( int i = 0; i < 20; i++ )
		{
			cout << setw(30) << left << clubname[i];
			cout << setw(20) << left << presname[i];
			cout << setw(15) << right << number[i];
			cout << setw(10) << fixed << setprecision(2) << MONEY * number[i];
		}
		
	
		infile.close();
	}

	else
		cout << "Error opening file!" << endl;


return 0;
}



Edit: this didn't copy and paste quite right, but the club names/presidents/members/money are all in neat columns and evenly spaced when they appear in the command prompt

This post has been edited by theant4: 01 June 2011 - 11:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Run-Time Failure -- stack around variable was corrupted...?

#2 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2394
  • View blog
  • Posts: 4,528
  • Joined: 30-May 10

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 01 June 2011 - 11:46 AM

> 36 while ( !infile.eof() )
This does not limit the input to the 10 elements that your arrays will support.

> 45 for ( int i = 0; i < 20; i++ )
And neither does this.
Was This Post Helpful? 1
  • +
  • -

#3 theant4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-February 11

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 01 June 2011 - 11:51 AM

Hmmm okay... I changed that 20 in the for loop to a 10 and that got rid of all the extra garbage in the output, but I'm still getting the run time failure... so what do I do about the priming read? It's weird cuz our professor showed us an example of this program in class and I thought I got everything just like he had it and his worked fine :/
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2394
  • View blog
  • Posts: 4,528
  • Joined: 30-May 10

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 01 June 2011 - 11:59 AM

What about limiting the input to not overflow your array.

Post your latest code, and maybe your input file.
Was This Post Helpful? 1
  • +
  • -

#5 theant4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-February 11

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 11:06 AM

Here's the latest code... there aren't many changes.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::right;
using std::left;
using std::setw;
using std::setprecision;
using std::ifstream;
using std::ofstream;

const float MONEY = 75.00;

int main()
{
	ifstream infile( "data.txt" );

	char clubname[10][70];
	char presname[10][70];
	char numofmems[10][70];
	int number[10];
	int i = 0;

	if ( infile.is_open() )
	{
		infile.getline(clubname[i], 70, ',');
		infile.getline(presname[i], 70, ',');
		infile.getline(numofmems[i], 70, ',');
		number[i] = atoi(numofmems[i]);


		while ( !infile.eof() )
		{
			++i;
			infile.getline(clubname[i], 70, ',');
			infile.getline(presname[i], 70, ',');
			infile.getline(numofmems[i], 70, ',');
			number[i] = atoi(numofmems[i]);
		}

		cout << "Club Name";
		cout << setw(30) << "President";
		cout << setw(20) << "Members";
		cout << setw(10) << "Money\n" << endl;
		

		for ( int i = 0; i < 10; i++ )
		{
			cout << setw(30) << left << clubname[i];
			cout << setw(20) << left << presname[i];
			cout << setw(15) << right << number[i];
			cout << setw(10) << fixed << setprecision(2) << MONEY * number[i] << endl;
		}
		
	
		infile.close();
	}

	else
		cout << "Error opening file!" << endl;


return 0;
}



And this is my input file:
Computer Systems Society, Kim Cares, 49,
Society of Women Engineers, Jeanie Queen, 51,
Sigma Tau Gamma, Storm Drain, 241,
Trekkies, C. Kirk, 230,
Home Brewers, Ross Coe, 15,
High Altitude Ballooning, Justin Time, 19,
Rugby, Ryan Johns, 25,
IEEE, Marc Bansmere, 36,
International Club, Kong Mbonkum, 102,
Dance Club, Will Shaver, 64,


So I'm still getting that the stack around "clubname" is corrupted... I tried using a for loop above the first input and I didn't get any errors but for some reason it skipped over the second entry and put 0.00 in the money column so I'm not sure what that was all about. I also have another minor problem in the output-- the top entry is offset by one space. The club name is fine but the K in "Kim Cares" is in line with the "e" in Jeanie Queen on the next line and everything after that is bumped over...

This post has been edited by theant4: 03 June 2011 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 NinjaCoder   User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 17
  • Joined: 23-May 11

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 11:37 AM

you need to limit the amount of data that is allowed to be read from the final.

	while ( !infile.eof() && i<10)
		{
			++i;
			infile.getline(clubname[i], 70, ',');
			infile.getline(presname[i], 70, ',');
			infile.getline(numofmems[i], 70, ',');
			number[i] = atoi(numofmems[i]);
		}



Was This Post Helpful? 1
  • +
  • -

#7 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2394
  • View blog
  • Posts: 4,528
  • Joined: 30-May 10

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 11:38 AM

http://sourceforge.n....php?title=Feof
Your while loop will go round 11 times, not 10.
On the last iteration, all the getfile() calls will fail, but that doesn't stop it trashing the non-existent array entry.

Try
std::string buffer;
while ( i < 10 && getline( infile, buffer ) ) {
  istringstream ss(buffer);
  // now use >> on ss to extract the information you want from the line
  
  // if everything is OK, then do
  i++;

  // otherwise, report some kind of error, and perhaps exit the loop.
}


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,666
  • Joined: 25-December 09

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 11:39 AM

You are accessing your array out of bounds. Your array is defined with a zize of 10 and you have 10 items in your file and you are trying to store the last item into index number 10. Arrays in C/C++ start at 0 and end at size - 1, 9 in this case. The line causing the problems is line 39. In this line you increment your array index before you extract your variables, therefore you are using array indexes 1 - 10. This line should be at the end of the loop, after your extractions. Also you should check the value of i before you use it to insure that it is within the bounds of your array.


Jim
Was This Post Helpful? 1
  • +
  • -

#9 theant4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-February 11

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 11:40 AM

Thanks Jim! That did the trick... the only problem now is that the first entry, the Computer Systems Society, has disappeared entirely in the output... there's just a blank line where it should be. This seems to me like a loop problem...

This post has been edited by theant4: 03 June 2011 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,666
  • Joined: 25-December 09

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 12:02 PM

Post your latest code.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 theant4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-February 11

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 04:11 PM

while ( !infile.eof() )  
{  
      infile.getline(clubname[i], 70, ',');  
      infile.getline(presname[i], 70, ',');  
      infile.getline(numofmems[i], 70, ',');  
      number[i] = atoi(numofmems[i]);  
      ++i;  
}  


This is the only thing I changed (incrementing i at the end of the loop rather than at the beginning like you said)

This post has been edited by theant4: 03 June 2011 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

#12 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 05:10 PM

> while ( !infile.eof() )

Why are you conditioning on eof? Your getline calls fail, but you write to number[i] anyway, trashing memory.

EDIT: Was pointed out by salem. Did you not read his post?

This post has been edited by Oler1s: 03 June 2011 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#13 theant4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-February 11

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 06:45 PM

That's just how we were taught to do it...

"While it's not at the end of the file, do the following stuff"
Was This Post Helpful? 0
  • +
  • -

#14 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 07:58 PM

> That's just how we were taught to do it...

It's obviously not correct.

> "While it's not at the end of the file, do the following stuff"

That's your high level logic in English. But that's not what you wrote in code.
Was This Post Helpful? 0
  • +
  • -

#15 theant4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-February 11

Re: Run-Time Failure -- stack around variable was corrupted...?

Posted 03 June 2011 - 10:22 PM

Well I finally got some help from my professor and he had me move the ++i back to the beginning of the while loop and then change all my arrays from 10 to 11. I did both of these things and now everything looks great.

Here's the final program, for future reference:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::right;
using std::left;
using std::setw;
using std::setprecision;
using std::ifstream;
using std::ofstream;

const float MONEY = 75.00;

int main()
{
	ifstream infile( "data.txt" );

	char clubname[11][70];
	char presname[11][70];
	char numofmems[11][70];
	int number[11];
	int i = 0;

	if ( infile.is_open() )
	{
		infile.getline(clubname[i], 70, ',');
		infile.getline(presname[i], 70, ',');
		infile.getline(numofmems[i], 70, ',');
		number[i] = atoi(numofmems[i]);

		while ( (!infile.eof()) && ( i < 11 ))
		{
			++i;
			infile.getline(clubname[i], 70, ',');
			infile.getline(presname[i], 70, ',');
			infile.getline(numofmems[i], 70, ',');
			number[i] = atoi(numofmems[i]);
			
		}

		cout << "CLUB NAME";
		cout << setw(30) << "PRESIDENT";
		cout << setw(20) << "MEMBERS";
		cout << setw(10) << "MONEY\n" << endl;
		
		for ( int i = 0; i < 11; i++ )
		{
			cout << setw(30) << left << clubname[i];
			cout << setw(20) << left << presname[i];
			cout << setw(10) << right << number[i];
			cout << setw(10) << fixed << setprecision(2) << MONEY * number[i] << endl;
			
		}
	
		infile.close();
	}

	else
		cout << "Error opening file!" << endl;


return 0;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2