Robert Ryan wrote:
> the original one I copied from Word 2007 which makes line spacing a problem.
> I copied this right from Linux.
> You only have enough allocated space for two characters and a null
> terminator. - please explain!!!
> #include <stdio.h>
> void swap(char*, char*);
> void order_chars(char [], int);
>
> int main()
> {
> char c[3], a, z;
> a = 'A';
> z = 'Z';
> printf("Enter three characters: \n");
> scanf("%s", &c);
> order_chars(c, 3);
> if(c[0] < a || c[3] > z) {
> printf("Please enter in capital characters\n");
> } else {
> printf("%s\n", c);
> //cout >> 'c';
> }
> }
Changing the name of the variable won't fix the problems (g -> c).
I have no idea how you are fitting Word into writing code. It isn't
natural, so don't do it.
scanf() with "%s" is dangerous. Stop using it.
To answer your question: A good debugger should be pointing out that
your program steps outside array bounds in 'c' when you enter three
characters (it is amazing your program didn't segfault on you). When
three characters are entered, scanf() will attempt to store the three
characters and a terminating null '\0' character into the array ('c').
You only have allocated 3 bytes and yet scanf() will try to write 4 bytes.
I seriously recommend finding a compiler with an easy-to-use debugger
that allows you to step through code line-by-line and hover your mouse
over variables to see what values they contain. To that end, seriously
look at installing Microsoft Visual C++ 2008 Express. I think I've
mentioned this recommendation before and you continue to ignore using
tools that will help you. Yes, your assignments probably have to work
under Linux but that doesn't mean they have to be _developed_ there.
Pure ANSI C/C++ code will compile both places just fine (especially the
simple code you are writing). Use a good IDE like VC++ Express, read a
few _GOOD_ books, and you will learn a lot faster than guessing your way
by trial-and-error...the single worst way to learn C/C++!
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/