Jump to content





Photo

C++ IS fun.

Posted by Tracker , 09 January 2013 · 192 views

To day I had my first "lesson" and this is what I made, which was a lot more than asked (a simple "Hello World" type program):

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"



//
#include "iostream"
using namespace std;


int main()
{ int in0 = 0;
int counter = 0;
cout << counter << endl;

do
{


counter = counter + 1;
if (counter == 5)
{
in0 = 1;
}
if (counter == 4)
{
cout << "Hello, this is simple for those who know what they are doing. counter = 4" << endl;
}
cout << counter << endl;
cout << "Hello, this is simple for those who know what they are doing." << endl;

}
while (in0 == 0);
system("pause");



}

Getting this to work was a b****, however. It actually stumped the instructor. The main problem? if (counter == 5) was if (counter = 5) See if you can figure out what happened :). Need less to say, I may have given some ideas to the instructor with regards to the next lesson goal.

Also: Attached File  Project1.zip (128.99KB)
downloads: 17 Cover your ears.



Photo
JrMasterModelBuilder
Jan 09 2013 11:19 AM

C++ IS fun.

Just wait . . . ;P

See if you can figure out what happened

It probably ran forever.
Nope, it did the opposite. Debugging the counter suggested the loop ran properly, but nothing else seemed to loop. When I said guess, I mean guess what it did and why. I figured it out. My instructor did not see the mistake before I pointed it out to him. It was just one of those things that one would often miss.

Nope, it did the opposite. Debugging the counter suggested the loop ran properly, but nothing else seemed to loop. When I said guess, I mean guess what it did and why. I figured it out. My instructor did not see the mistake before I pointed it out to him. It was just one of those things that one would often miss.


counter = 5 sets int counter to 5
counter == 5 checks if counter is equal to 5, and if so returns TRUE, otherwise returning FALSE. That's why, when using if statements or do { } while loops, you're supposed to use yourintegerhere == somevalue. Most compilers will catch this and return a compile error, however some may not.

A good habit to get into is planning your program out in pseudocode before beginning to write the actual program. That way you can catch these mistakes before they become a problem.
I don't think that would have helped much here since this error occurred even with planning.
Photo
JrMasterModelBuilder
Jan 10 2013 01:39 AM

Nope, it did the opposite. Debugging the counter suggested the loop ran properly, but nothing else seemed to loop. When I said guess, I mean guess what it did and why. I figured it out. My instructor did not see the mistake before I pointed it out to him. It was just one of those things that one would often miss.

You right, I mis-read the code.

counter = 5 sets int counter to 5
counter == 5 checks if counter is equal to 5, and if so returns TRUE, otherwise returning FALSE. That's why, when using if statements or do { } while loops, you're supposed to use yourintegerhere == somevalue. Most compilers will catch this and return a compile error, however some may not.

That's acutally valid code, you can assign variables inside of an if statement. It takes the first variable and decides if it is truth-y or false-y. Take this code for example. i is set to 0 which evaluates to false, then assigned to 2 which evaluates to true.

#include "iostream"

using namespace std;

int main()
{
    int i;
    if(i = 0)
    {
        cout << "true" << endl;
    }
    else
    {
        cout << "false" << endl;
    }
    if(i = 2)
    {
        cout << "true" << endl;
    }
    else
    {
        cout << "false" << endl;
    }
}

That's acutally valid code, you can assign variables inside of an if statement. It takes the first variable and decides if it is truth-y or false-y. Take this code for example. i is set to 0 which evaluates to false, then assigned to 2 which evaluates to true.


Strange. Every compiler I've used throws an error...
MS Visual studios 2012 is what I used. Not even a warning.
Must just be a Dev C++ thing then...
Well, I just blew it and got the wrong version of MSVS 11, so now I may have to use Dev-C++.
The Express Version of Microsoft Visual Studio is free, you know.

[code=auto:0]

This is valid because instead of assigning just a integer, you could be assigning something a function returns, while silly because you don't accomplish much by doing it this way (which is why it's odd, you don't often see it).

Categories