Jump to content

Public Memlog 1

  • entries
    60
  • comments
    256
  • views
    26,636

C++ IS fun.


Tracker

958 views

 Share

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: Project1.zip Cover your ears.

 Share

11 Comments


Recommended Comments

JrMasterModelBuilder

Posted

C++ IS fun.

Just wait . . . ;P

See if you can figure out what happened

It probably ran forever.

Link to comment

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.

Link to comment

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.

Link to comment

I don't think that would have helped much here since this error occurred even with planning.

Link to comment
JrMasterModelBuilder

Posted

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;

    }

}

Link to comment

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...

Link to comment

Well, I just blew it and got the wrong version of MSVS 11, so now I may have to use Dev-C++.

Link to comment


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).
Link to comment

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.