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 Thu Jun 20, 2013 11:54 am


All times are UTC [ DST ]





Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: C++ loops... help me!
PostPosted: Wed Jun 20, 2012 6:00 am 
 
User
User

Joined: Sun Jun 03, 2012 8:34 pm
im a few days into learning my first programing language...C++. i wrote a very basic console application to see what i could do...it was originally part of the book i was using and i took it a step farther and then another step farther and so on...basically im stuck trying to get the program to loop. at the beginning of the program the user has to enter a value and after several if statments it displays some text depening on the inputed value. i want to insert a sort of "would you like to do this again? Y/N. type function where Y would start the program at the entering the first value...i used a goto loop: statment and placed loop: at the before the first int is declared. problem is it loops using the already entered ...i want the user to have to enter another int

sort of like
//short example of my ramblings above
int main( int nNumberofargs, char* pszArgs[])

{
   loop:
int X;
  cout << "enter value for X:";
  cin >> X;

if (0 < x && x<2)
{
     cout << "you are awesome";
            << endl;
}
else
{
    cout<< "you are not awesome";
          <<endl;
}
int y = true, N = false;
{
    cout << "would you like to enter another number? Y/N"
           <<endl;
     cin >> Y,N;
}
if (true)
   goto loop:;
}

Edit by Gorthal: code tags

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Wed Jun 20, 2012 10:33 am 
 
User
User

Joined: Sat Mar 17, 2012 5:39 pm
Try using code tags next time. Anyway, using goto is bad practice (why, I don't know, but whatever). You'll want to do something more like:

int main(int argc, char *argv[], char *envp[])
{
   for(;;) // while(true)
   {
      cout << "blah";
      cin >> x;

      switch(x)
      {
      case 0:
         cout << "blah";
         break;
      case 1:
         cout << "blah";
         break;
      default:
         cout << "blah";
         break;
      }

      cout << "blah";
      cin >> y;

      y = tolower(y);

      while(z == true)
      {
         if(strcmp(y, "y") == 0)
         {
            z = true;
            continue;
         }
         else if(strcmp(y, "n") == 0)
         {
            z = false;
            break;
         }
         else
         {
            cout << "blah";
            z = true;
            continue;
         }
      }
   }

   return 0;
}


Not entirely sure if the break will break the for or just the while. You'll have to play with it. Typed it up in about 2 minutes, so good luck. Honestly, this could be coded a lot better, but it's 5:30 am.

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Wed Jun 20, 2012 4:31 pm 
 
User
User

Joined: Tue Mar 01, 2005 8:31 pm
How old is that book? You should not use goto to create loops that's an ancient way.
Rather you should use goto to escape deeply nested loops or to free memory.


for (int a = 0; a < 100000; a++)
{
    for (int b= 0; b < 100000; b++)
    {
        for (int c = 0; c < 100000; c++)
        {
            ...
            if (escapetheloop)
                goto outofhell;
            ....
        }
    }
}
outofhell:


or take an "if wall" as another example.


Quote:
Anyway, using goto is bad practice (why, I don't know, but whatever).

Then have a short read :wink:
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Thu Jun 21, 2012 4:48 am 
 
User
User

Joined: Sat Mar 17, 2012 5:39 pm
goto isn't any different than jmp label, so I don't see how it is "bad", I just know it isn't "good practice".

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Thu Jun 21, 2012 8:47 am 
 
Moderator
Moderator
User avatar

Joined: Thu Apr 30, 2009 12:31 pm
Location: %scrdir%
goto isn't any different than jmp label, so I don't see how it is "bad", I just know it isn't "good practice".
every single major compiler turns loops into labels, and in fact goto can be used for optimization (threaded-execution). It was considered bad because it can lead to memory leaks and it could lead to destructors not being called (due to jumping out of scope), but now the former is solved with smart pointers, and the latter is sovled but it being treated as a bug by compilers (so it can't compile). Apart from that, it just makes readability difficult if overused.

Vampirewolve wrote:
How old is that book? You should not use goto to create loops that's an ancient way.
Rather you should use goto to escape deeply nested loops or to free memory.
if you are nesting deeply enough to warrant goto, you should consider refactoring first (makes things more manageable, and more readable).

_________________
Learn C++, not Crap++ http://tinyurl.com/so-cxxbooks
Hackito Ergo Sum
Cthulhon: No, I am a dancer. I am in charge of popping and locking.

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Thu Jun 21, 2012 9:47 am 
 
User
User

Joined: Tue Mar 01, 2005 8:31 pm
Of course there are more elegant solutions but it's just a simple example where a goto is useful.

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Fri Jun 22, 2012 3:12 am 
 
Retired Mod
Retired Mod

Joined: Thu Jul 26, 2001 1:00 am
Location: Sweden
Personally, I have never needed to use goto in C++. It has it's uses in C for cleanup and exception emulation but C++ has those things built in already. There is nothing inherently wrong with using goto if you are aware of the limitations, it's just that there almost always (in my opinion) a more elegant solution.

@FireFromTheSky: break and continue only work on the innermost loop.

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Fri Jun 22, 2012 4:43 pm 
 
User Gold
User Gold
User avatar

Joined: Tue Jul 09, 2002 5:39 am
Location: Arkansas
Back in CS1 Freshman year I used a goto in a c++ application. My prof docked me like 10 points and wrote "There is only one acceptable use for goto.. and this is not it."

I still don't know what the acceptable use is..

_________________
lord2800 wrote:
I can, but I don't want to run an AV in the first place
I'm as likely to get a virus as Bush is to shoot himself in the head, intentionally.

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Sat Jun 23, 2012 12:18 am 
 
User
User

Joined: Thu Dec 03, 2009 10:21 pm
Back in CS1 Freshman year I used a goto in a c++ application. My prof docked me like 10 points and wrote "There is only one acceptable use for goto.. and this is not it."

I still don't know what the acceptable use is..

that makes me giggle

/e mostly because my programming teacher in high school was also my e-commerce teacher, he obviously never read anything I wrote in that class I would in mid sentence write things like "and the little balloon flew high into the sky" or "meow meow meow meow meow meow". 100% on everything.

Top
 Profile  
 Post subject: Re: C++ loops... help me!
PostPosted: Tue Jul 03, 2012 10:08 am 
 
User
User

Joined: Wed Apr 19, 2006 3:59 pm

Quote:
Anyway, using goto is bad practice (why, I don't know, but whatever).

Then have a short read :wink:
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF


Ha, thats my uni,,, and my prof just assigned us to read that paper.

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

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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