5 Replies - 942 Views - Last Post: 28 September 2009 - 01:25 PM Rate Topic: -----

#1 darkfox1990   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-September 09

Visual c++ problem?

Posted 28 September 2009 - 09:43 AM

I'm using Visual C++ 2008 Express. I made the program below but the program runs in an infinite loop (keeps asking for letter and displaying it) for some reason. I thought maybe I messed up the header file or something, so I made a new project and it gives me an error instead, after it shows the intended output. Also, all the other files and headers have the same code in both projects. Changing the "%1s" with "%c" fixes the problem, but why is this happening?

Error says: Debug Error! ... Run-time Check failure#2 - Stack around the variable 'a' was corrupted.

This is the code

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{  
	char a;
	printf("Enter letter: ");
	scanf("%1s", &a);
	printf("letter: %c\n",a);
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Visual c++ problem?

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

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

Re: Visual c++ problem?

Posted 28 September 2009 - 09:46 AM

You're corrupting your stack. You're using %s in scanf to read into a single character. Try using %c instead.
EDIT: Reading fail...note to self: don't just look at the code.

a is a location on the stack for a single character. By telling scanf you're reading a string, it's going to add a null terminator after the character it reads, which overwrites the byte right after location of a, causing stack corruption.

If warnings were turned up in the compiler, there likely would have been one issued.
Was This Post Helpful? 0
  • +
  • -

#3 darkfox1990   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-September 09

Re: Visual c++ problem?

Posted 28 September 2009 - 09:49 AM

Sorry I forgot to mention that I'm learning from a book in which they use the method above. Is the book wrong then?

Edit: Oh yeah, it adds the null terminator. But why isn't it giving that error in the first project. And why is the book using this method as an example (more than one place). Would this work with a C compiler? Thanks a lot.

This post has been edited by darkfox1990: 28 September 2009 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

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

Re: Visual c++ problem?

Posted 28 September 2009 - 10:01 AM

That's how they read a single character? What book is this???
Was This Post Helpful? 0
  • +
  • -

#5 Elcric   User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Visual c++ problem?

Posted 28 September 2009 - 10:09 AM

Hi, :D

This does not have anything to do with your infinite loop, but I think scanf has been replaced with scanf_s
Was This Post Helpful? 0
  • +
  • -

#6 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Visual c++ problem?

Posted 28 September 2009 - 01:25 PM

Maybe this can help brought to you be MSDN
In the case of characters, one may read a single character as follows:

char c;

scanf("%c", &c, 1);

When reading multiple characters for non-null terminated strings, integers are used as the width specification and the buffer size.

char c[4];

scanf("%4c", &c, 4); // not null terminated


You can see more info here :P
http://msdn.microsof...8et(VS.80).aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1