Public Memlog 1

Sign in to follow this  
Followers 0
  • entries
    60
  • comments
    256
  • views
    23,623

C++ IS fun.

Entry posted

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


  Report Entry


11 Comments

Posted · Report

C++ IS fun.

Just wait . . . ;P

See if you can figure out what happened

It probably ran forever.

Share this comment


Link to comment

Posted · Report

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.

Share this comment


Link to comment

Posted · Report

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.

Share this comment


Link to comment

Posted · Report

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

Share this comment


Link to comment

Posted · Report

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;

    }

}

Share this comment


Link to comment

Posted · Report

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

Share this comment


Link to comment

Posted · Report

MS Visual studios 2012 is what I used. Not even a warning.

Share this comment


Link to comment

Posted · Report

Must just be a Dev C++ thing then...

Share this comment


Link to comment

Posted · Report

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

Share this comment


Link to comment

Posted · Report

The Express Version of Microsoft Visual Studio is free, you know.

Share this comment


Link to comment

Posted · Report

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

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now