Re: help with c++ if test

  • From: Joseph Lee <joseph.lee22590@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 09 Feb 2011 15:15:24 -0800

Hi,
In this case, I agree with Tyler: you can just put the output after the if statement (called a conditional) inside the braces. Also, to really test if it works, I think you might want to put some more printing (or output) string at the beginning of the program.
Other things (see my routine below):

#include <iostram
#include <string> // yeah, put this in
// here to use strings.
using namespace sdd; optional.

int main()
{
cout << "testing adventure game input..." << endl;
string test; cin >> test;
if (test == "fine") { cout << "Yeah, it works..." << endl;}

return (0);}

Sorry for the line breaks on some code (I'm using a notetaker device to write the code. Always remember: if you really wish to use strings, you need to include the library. Also, for ease of reading, you might want to put a one-liner comment about what you are doing on some of the lines - so that we can better assist you. Finally, as my code shows, you don't really need "goto" statement when dealing with conditionals (at this stage), as Tyler and others pointed out.
Cheers,
Joseph

----- Original Message -----
From: "Kristoffer Gustafsson" <kg84@xxxxxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx
Date sent: Wed, 9 Feb 2011 20:02:53 +0100
Subject: Re: help with c++ if test

Hi.
ok, some code of mine looks like
#include <iostream
int main()
{
string test;
cin >>test;
if (test=="fine" goto good;

good:
cout <<"it works!";
return 0;
}

I try this and I get expected `before goto.
What can be wrong?
/Kristoffer

----- Original Message -----
From: "Ken Perry" <whistler@xxxxxxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx
Sent: Wednesday, February 09, 2011 7:53 PM
Subject: RE: help with c++ if test


It is easy to do text adventures or any programming without goto. Note that my commercial mud has 0 goto statements in it. Now all classes in college will tell you to avoid goto unless you're dealing with programming operating systems and need to be able to jump out of scope. Which is one of the
reasons you do not want to use goto.  For example

Void myfunc(){
Goto two;
}

Void myFunc2(){
Two:

}

You can actually jump function to function with goto which breaks all kinds of things in functional and object oriented programming. Now that is necessary some times when doing error handling. The truth is though if your coding your own application you really should never need goto. If you find yourself using goto you have probably designed your program wrong.

I will not go into a deep why or why not to use it because if your thinking you cannot create an adventure game without it you will not understand a lot of my explanation. I think you need to go through some tutorials on c or
c++ which ever one you're using because you have a lot to learn.



Ken

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Kristoffer
Gustafsson
Sent: Wednesday, February 09, 2011 1:41 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: help with c++ if test

Hi.
Without goto it would be very hard to do text adventures.
I don't think it can be done, or can it?
Also, if I want my program to exit instead of continuing, how do I do? I mean for example, if I die in an adventure, I want to exit the program at
that time, not continue the game.
/Kristoffer
----- Original Message -----
From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx
Sent: Wednesday, February 09, 2011 7:28 PM
Subject: Re: help with c++ if test


That produces all sorts of problems. jumping to the bottom of a loop is no problem with a goto, and it avoids issues with your exiting variable. it's also quicker, because you just jump to the bottom. otherwise you may break, but it's still going to have to do one more check (two, actually, depending on how the compiler does things) to see if exiting is false.
On 2/9/2011 11:23 AM, Lex wrote:
09.02.2011 20:17, Littlefield, Tyler 極龜?댭둘?:
really really highly recommend you avoid goto. This isn't basic, and they're not very useful except for in some odd cases, far and few
between.  Such as jumping out of two nested loops like so:
int i, j;
for (i = 0; i < 100; i++)
{
for (j = 0; j < 100; j++)
{
if (i+j == 100)
goto botttom;
}
}
bottom:
//do something here

Actually, effect you're trying to achieve is more correctly by the conditional variable (at least in terms of procedural programming):
bool exiting=false;
for (i = 0; i < 100 && !exiting; i++)
{
for (j = 0; j < 100 && !exiting; j++)
{
if (i+j == 100)
exiting=true;
}
}


or may be even
for (j = 0; j < 100; j++)
{
if (i+j == 100)
{
exiting=true;
break;
}
}
}


Lex

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind




--

Thanks,
Ty

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: