8 Replies - 714 Views - Last Post: 02 October 2014 - 04:58 PM Rate Topic: -----

#1 Mavereye   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 14

New User, Unknown Error/Problem

Posted 02 October 2014 - 03:44 PM

Below Is the code i have written so far, when i run it it works as intended until it gets to "Game Reset, Guess Again", it then says debug error "stack around chartwo was corrupted" idk what this problem is, how do i fix it? and no this isnt work, dont worry about it being cheating, thanks for help anyone can give!


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

int _tmain(int argc, _TCHAR* argv[])
{
	int answer = 42;	
	int input = -1;
	char redoChar[20];
	bool redo = true;
	char chartwo[21];

	cout << "Guess a Number \n";

	do 
	{
		{
			cin >> input;

			if (input == answer)
			{
				cout << "Correct!\n";
			}
			else
			{
				cout << "Wrong Answer, Do You Give Up? [Y]es/[N]o \n";
				cin >> redoChar;
			}
			if(redoChar[0] == 'y')
			{
				redo = true;
				cout << "The Answer Is 42! Would You Like to [R]etry?\n";
				cin >> chartwo[21];
			}
			
			if(chartwo[21] == 'r')
			{
				cout << "Game Reset, Guess Again!\n";
				cin >> input;
			}
		
			else if(redoChar[0] == 'n')
			{
				redo = false;
				cout << "Guess Again!\n";
			}

			else
			{
				redo = false;
			}

		}	

	}

	while (answer != input && redo == false);
			
	return 0;
}


Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: New User, Unknown Error/Problem

#2 Mavereye   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 14

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 03:45 PM

Below Is the code i have written so far, when i run it it works as intended until it gets to "Game Reset, Guess Again", it then says debug error "stack around chartwo was corrupted" idk what this problem is, how do i fix it? and no this isnt work, dont worry about it being cheating, thanks for help anyone can give!


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

int _tmain(int argc, _TCHAR* argv[])
{
	int answer = 42;	
	int input = -1;
	char redoChar[20];
	bool redo = true;
	char chartwo[21];

	cout << "Guess a Number \n";

	do 
	{
		{
			cin >> input;

			if (input == answer)
			{
				cout << "Correct!\n";
			}
			else
			{
				cout << "Wrong Answer, Do You Give Up? [Y]es/[N]o \n";
				cin >> redoChar;
			}
			if(redoChar[0] == 'y')
			{
				redo = true;
				cout << "The Answer Is 42! Would You Like to [R]etry?\n";
				cin >> chartwo[21];
			}
			
			if(chartwo[21] == 'r')
			{
				cout << "Game Reset, Guess Again!\n";
				cin >> input;
			}
		
			else if(redoChar[0] == 'n')
			{
				redo = false;
				cout << "Guess Again!\n";
			}

			else
			{
				redo = false;
			}

		}	

	}

	while (answer != input && redo == false);
			
	return 0;
}

This post has been edited by jimblumberg: 02 October 2014 - 08:13 PM
Reason for edit:: Added missing code tags please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 04:00 PM

char chartwo[21];

This creates an array of size 21, but the elements are indexed from 0, so 0..20.
cin >> chartwo[21];

This attempts to reference the element with index 21, but there isn't one.
Was This Post Helpful? 0
  • +
  • -

#4 Mavereye   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 14

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 04:02 PM

so how would i fix it? sorry for noobish-ness, only been learing c++ for a couple days
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 04:12 PM

You only have elements at indexes 0 through 20, so don't attempt to refer to an element at index 21.

If you need 22 elements, so that you could use chartwo[21], then make your array larger.
Was This Post Helpful? 0
  • +
  • -

#6 Mavereye   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 14

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 04:15 PM

i just tryed changing all the chartwo[21] to chartwo[19], its still giving me the same error and ending the program
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 04:25 PM

Did you change ALL 21s to 19s? Including the array declaration itself?
Was This Post Helpful? 0
  • +
  • -

#8 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 04:34 PM

On lines 33 and 36 you are using an index 1 off the end of your array, which is corrupting the stack. If you change that accordingly it should be fixed. I suggest looking at what you did for redoChar and doing something similar here.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: New User, Unknown Error/Problem

Posted 02 October 2014 - 04:58 PM

Merged duplicate topics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1