Link Bar

Home | Games | Programming | Tutorials | Donate | Legal (These are mostly empty right now. Placeholders!)

warning code

This website contains adult content.

Tuesday, March 6, 2012

Day 17: Program runs!

Well, after much hemming and hawing, I finally sat through the MS Visual Studio tutorial and followed along instead of just watching it.  Within minutes, I got this:
Fig. 1  A window that asks for a 1 and everything!

It works!  Working code!  But then when I entered "Hot cha!" as you can see above in Figure 1, I got a run-time check failure - apparently, some stack around the memory of "myLine" was corrupted.  I can Break or Continue, and I don't know what those mean.  Glad I can JFGI before I decide!

OK, I didn't find anything in two minutes and I'm too excited, so I clicked "break."  Apparently, breaking things is the wrong thing to do.  I don't know how I could have possibly known this in my previous life experience.  Now I can't close the window.  I ctrl+alt+deleted and tried to end the process, and it just won't die.

I was all set to reboot, and then VC++ asked me if I wanted to stop debugging when I closed it.  Then the window closed on its own.  Not spooky at all.  This magic box is haunted.

Back up and running, and the machine tells you you've played even when you press 2.  Hmm... I thought that maybe that "if(myLine[100]=1" bit might mean "if it's true," so I tried putting the 1 in quotes, and got, "1>c:\users\d\documents\visual studio 2010\projects\helloworld\helloworld\helloworld.cpp(23): error C2440: '=' : cannot convert from 'const char [2]' to 'char'; 1> There is no context in which this conversion is possible."  That's at least intelligible, but it doesn't fix my problem.

OK, that's it for today.  I also did a bunch of art stuff, but it's sketches at this point and nothing really presentable.  Toodles!

2 comments:

  1. okay. so. let's talk about arrays.

    the most important thing you need to know about arrays is that they're zero-indexed. That means that the first item in the array sits at [0], not [1].

    However, when you create an array (like char myLine[100]), that specifies the SIZE. Not the position of the last index. That means that if you try to access myLine[100] you're actually accessing something outside of the bounds of the array, which is bad. you want to look at myLine[99].

    Except in this case, you don't. Strings entered via cin start at 0. naturally. So when you want to see if the first character entered was a 1, you'd check myLine[0].

    Next, you're confusing operators. = does not mean the same thing in math as it does in programming. in C/C++, "=" means "assign". That's ALL it means. If you want to compare equivalency you need to use "==". That's two equal signs. That will do the comparison that you want.

    Continuing on, type DOES matter, so you do still want to escape the 1 in quotes.

    Finally, I've never seen that format of if before in my life. I don't think if is supposed to have a comma in it. how it goes is:

    if (condition) { doSomething(); }

    The squiggly braces are sort of important; they define the scope, limiting it so that the things in brackets occur ONLY IF the condition is true. You can also use "else" to extend this:

    if (condition) { doSomething(); } else { doSomethingElse(); /* condition resolved as false */ }

    Remember, white space doesn't technically matter, so you can space them out however you like. The Zend Standard for PHP says that opening braces occur on the same line for if conditions and a newline for function/class definitions, and that the closing brace always occurs on a newline.

    so what your code should look like around the if is:

    if (myLine[0] == '1') {
    std::cout << "Thanks for playing!" << std::endl;
    }

    In other matters, debugging. "Break" means to stop execution of the program and switch to the debugger so you can get info. "Continue" means to ignore the error and continue operating as though the debugger weren't on. I don't know enough about the VC++ debugger to tell you why it "froze", but I imagine focus just switched to a different window.

    ReplyDelete
  2. Thanks again for all the help, Z! I have a whopping three choices in my game now!

    ReplyDelete