Blizzhackers

Return of the Jedi

* Login   * Register    * FAQ    * Search

Join us on IRC: #bh@irc.synirc.net (or Mibbit Web IRC)


MuleFactory


It is currently Sat May 25, 2013 5:07 am


All times are UTC [ DST ]





Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: [C++ help] Why is this being executed twice?
PostPosted: Wed Jul 18, 2012 9:40 am 
 
User
User
User avatar

Joined: Thu Aug 30, 2007 1:23 am
Location: Falcon, CO
// Project 10 - More Loops.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace std;


int forLoop()
{
   for(int x = 1; x++ <= 100; cout << "FOR!?\n");
   return 0;
}

int whileLoop()
{
   int x = 1;
   while (x++ < 100)
   {
      cout << "WHILE!!!\n";
   }

   return 5;
}

int doLoop()
{
   int x = 0;
   do(cout << "Doo doo\n");
      while (x++ <= 10);
   
      return 9001;
}

int ifCompact()
{
   int a = 9;
   int b = 11;

   (a >= b) ? cout << "Hello!\n" : cout << "Bye\n"; //?: = if true, do this, if not : do this

   return 2 * 13;
   
}


int main()
{
   forLoop();
   int x = forLoop();
   cout << x << endl;

   whileLoop();
   int a = whileLoop();
   cout << a << endl << endl;

   doLoop();
   int z = doLoop();
   cout << z << endl << endl;

   ifCompact();
   int b = ifCompact();
   cout << b << endl << endl;
   
   
   cout << "Press any key to exit.";
   char z;
   cin >> z;
   return 0;
}


Trying to learn c++ and I'm at this point where I'm learning about different kinds of loops.

In the main function where I call ifCompact() it should say "Hello" if the statement is true, and "Bye!" if the statement is false. I've looked over the whole thing and I can't figure out why when I compile+run it, "Bye!" gets said twice as if that function is being called twice, but I only called it once in main().

Any pros help a newb out? :D

using microsoft vc++
windows 7 x64

_________________
Image

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Wed Jul 18, 2012 1:00 pm 
 
User Gold
User Gold
User avatar

Joined: Sat Jun 02, 2012 1:33 am
Location: zone 6
Frostbite130 wrote:
"Bye!" gets said twice as if that function is being called twice, but I only called it once in main().

look alive there buddy... youre calling each function twice.
each of your calls looks like this...

ifCompact();//Call One
int b = ifCompact();//Call Two

you also have an issue with declaring the variable Z twice.
Image

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Wed Jul 18, 2012 1:07 pm 
 
User
User
User avatar

Joined: Thu Aug 30, 2007 1:23 am
Location: Falcon, CO
I see. Thanks. I guess i didn't realize that that was calling it again. But if thats the case why didnt the other functions get called twice as well?

Or maybe i just dont notice them getting called twice because there's a lot more output.

How would i go about adding the return value of those functions to variables without calling them?

Please forgive my newbness. This is day 2 for me.

_________________
Image

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Wed Jul 18, 2012 2:49 pm 
 
User Gold
User Gold
User avatar

Joined: Sat Jun 02, 2012 1:33 am
Location: zone 6
How would i go about adding the return value of those functions to variables without calling them?

int sum=0+5+9001+26;

oh wait. I misread....the answer is: You dont. If you want to sore a return value in a variable, you have to call the function so it can return the value.

Think of it like this, maybe it will help. Suppose you have a function doLoop() that just returns the value 9001. Every time you call doLoop(), you can think of it as 9001.

that is to say,
int a=doLoop();
is exactly the same as
int a=9001;

or
int a = doLoop()>>24|(doLoop() << 8 ) & 0x00FF0000|(doLoop() >> 8 ) & 0x0000FF00|doLoop()<<24;
//is exactly the same as
int a = 9001>>24|(9001 << 8 ) & 0x00FF0000|(9001 >> 8 ) & 0x0000FF00|9001<<24;

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Thu Jul 19, 2012 8:20 am 
 
Retired Mod
Retired Mod
User avatar

Joined: Tue Jan 01, 2002 1:00 am
Sitenote:

For beginners it's very important to get "friends" with debugging quickly in order to solve such problems.

Set a breakpoint at the beginning of the code and see what it does step by step, learn to step into / over functions, use conditional breakpoints etc. By doing this you'll learn how the code "thinks" and how to find the reasons for issues.

_________________
Image

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Thu Jul 19, 2012 8:46 am 
 
Retired Mod
Retired Mod

Joined: Thu Jul 26, 2001 1:00 am
Location: Sweden
Sitenote:

For beginners it's very important to get "friends" with debugging quickly in order to solve such problems.

Set a breakpoint at the beginning of the code and see what it does step by step, learn to step into / over functions, use conditional breakpoints etc. By doing this you'll learn how the code "thinks" and how to find the reasons for issues.

+1. I would say that knowing how to debug code is more important that knowing how to write it in the first place :)
And the visual studio debugger is very beginner friendly in my opinion.

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Thu Jul 19, 2012 11:54 am 
 
User
User

Joined: Tue Oct 12, 2004 6:13 pm
Location: Germany
Also trash the book you are using. Every single loop you have written there is like buttsex (and i am not fan of butt sex...)

// nevver do this, ugly piece of shit
// for(int x = 1; x++ <= 100; cout << "FOR!?\n");
// instead:
for (int x=1; x<=100; ++x) {
  cout << "for" << endl;
}

and even then you might want to change x -> i and count from 0 to <100 instead of 1 to <=100

E: and does this do-while loop even compile? oO
   do(cout << "Doo doo\n");
      while (x++ <= 10);

it should be
do {
  cout << "doo doo" << endl;
} while (x++ <= 10);

(note again that <= is misleading. in this case the last iteration is with x = 11)

_________________
Image


Last edited by K_OS on Thu Jul 19, 2012 12:18 pm, edited 1 time in total.
Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Thu Jul 19, 2012 12:06 pm 
 
User Gold
User Gold
User avatar

Joined: Sat Jun 02, 2012 1:33 am
Location: zone 6
Also trash the book you are using. Every single loop you have written there is like buttsex (and i am not fan of butt sex...)


E: and does this do-while loop even compile? oO

lol come on man, look alive. of course it compiles and runs just fine. its a single line so no need for curlies.

and the for loop, even if it looks like an abortion is fine too. but I would have used the "goes to" operator instead.

//for x equals ten: as x goes to 0, print FOR!?
for(int x = 10; x  --> 0; cout << "FOR!?\n");


ETA:
I do agree though he should get a new book. these shortcuts are something you should learn once you already know how to code. I almost regret posting the "goes to" operator.
Almost. Image

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Thu Jul 19, 2012 12:30 pm 
 
User
User

Joined: Tue Oct 12, 2004 6:13 pm
Location: Germany
you are right. it actually compiles - even though every single implication of the indentation is wrong. (foremost: do is not a function that is being called like do(..) ; ) It should at least be
do
  cout << "doo" << endl;
while (x++ <= 10);

even without curly braces. (so might as well include them to avoid problems in the future - especially since he is just starting to learn the language)

_________________
Image

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Thu Jul 19, 2012 1:07 pm 
 
Retired Mod
Retired Mod

Joined: Thu Jul 26, 2001 1:00 am
Location: Sweden
Of course it compiles. A compiler doesn't care about whitespace or indentation or extra parenthesis.
do(some_statement);

is the same thing as
do
(some_statement);

which is the same as
do
   some_statement;

Indentation was just invented so programmers could have yet another way to confuse the ones trying to read the code. ;)
I've seen so many people say that this would print "x = 6" because they just look at the indentation instead of the actual code.
int x = 0;
for(int i = 0; i < 5; ++i)
   x++,
x++;
cout << "x = " << x << endl;

Top
 Profile  
 Post subject: Re: [C++ help] Why is this being executed twice?
PostPosted: Sun Jul 22, 2012 11:17 pm 
 
User Gold
User Gold
User avatar

Joined: Mon Dec 02, 2002 8:55 pm
for(int x = 1; x++ <= 100; cout << "FOR!?\n");


that is fucking terrible. please never code like that again

_________________
Peter Griffin, ~esquire~

Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron