Click to See Complete Forum and Search --> : How does one do if statements with arrays?


Xprotocol
01-21-2001, 12:28 AM
I was just wonder how to go about making if statements with arrays. Like

enter your name
if name == bob "Hey, you're cool"

I know the above isn't complete code but its just for an example.

------------------
Check out my webpage
http://xprotocol.homestead.com

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."
--Albert Einstein

Strike
01-21-2001, 01:27 AM
Um, what language?

nanode
01-21-2001, 03:29 AM
In java you could do something like this:

for(int i=0;i<myArray.size();i++) {
if(myArray[i] == 4) {
System.out.println("found it");
}
}

If you want to store objects, and not just primitives (int, float, char) then use a Vector or ArrayList.

prince_kenshi
01-21-2001, 04:53 AM
If you're wanting what I think you are, you could create a constant array and compare them one character at a time. I'm going to use nanode's code and make slight changes.

for(int i=0;i<myArray.size();i++) {
if(myArray[i] == constantArray[i]) {
System.out.println("found it");
}
}

I think this is what you're asking. That's the only way I can think of other than comparing one letter at a time. I'm afraid I don't know object oriented languages as well as assembler though.

------------------
Prince Kenshi
Son of Bahamut

Sterling
01-21-2001, 12:11 PM
Or, if you're using C, strcmp should do the trick. Under Linux or any other Unix variant, man strcmp should get you the details.

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."

Xprotocol
01-21-2001, 03:06 PM
Oops, thought I mentioned the language. http://www.linuxnewbie.org/ubb/rolleyes.gif C++

------------------
Check out my webpage
http://xprotocol.homestead.com

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."
--Albert Einstein

Whipping Boy
01-21-2001, 03:57 PM
If it's C++, then just use the string class and treat it like any other variable.

------------------
Kurt Weber
Shell scripts? Shell scripts? We don't NEED no stinkin' shell scripts!
White, heterosexual, middle-class, and proud!
I've never understood why Bill Gates would name his company after his penis

aph3x
01-21-2001, 05:24 PM
#include <iostream.h>
#include <string.h>

char name[20];

cout << "Enter your name: ";
cin.getline(name,20);

if( !strcmp(name,"Bob") )
cout << "Hi Bob!";
else
cout << "Where's Bob?";


------------------
----------------

bash # more beer
bash # less work

Xprotocol
01-21-2001, 07:17 PM
Thats exactly what I was looking for aph3x, thanks http://www.linuxnewbie.org/ubb/smile.gif.

Whipping Boy
01-21-2001, 08:37 PM
The string class would be much easier, imho. You could just:

#include <string>
#include <iostream>
.
.
.
string Name;
.
.
.
if (Howdy == "Bob"){
cout << "Hey, you're cool";
}

The string class makes it much easier and you don't have to deal with the arrays and all the nasty problems you run into when using them.

------------------
Kurt Weber
Shell scripts? Shell scripts? We don't NEED no stinkin' shell scripts!
White, heterosexual, middle-class, and proud!
I've never understood why Bill Gates would name his company after his penis

Xprotocol
01-21-2001, 08:57 PM
I'll have to settle for what aph3x proposed since I have no idea how to do what you just said http://www.linuxnewbie.org/ubb/tongue.gif. Thanks though.

[This message has been edited by Xprotocol (edited 21 January 2001).]

Stuka
01-22-2001, 12:48 AM
Xprotocol,
To do what aph3x did, using the string class:string name;
cout << "Enter your name: ";
cin >> name;
if (name == "Bob")cout << "You're cool!";
else cout << "Where's Bob?";
It's that easy. The string class is beautiful for this kind of thing.
<edit>BTW, for all concerned, the string class is NOT in <string.h>. IIRC, it's part of the standard libs...but I could be wrong! http://www.linuxnewbie.org/ubb/smile.gif</edit>

[This message has been edited by Stuka (edited 21 January 2001).]

nanode
01-22-2001, 10:55 AM
You know, I have to admit that overloading the '==' for evaluating 2 string class objects is nice in C++. There was some academic detail that caused the Java people to use the String.equals(String s) method instead. I suppose maybe Objects and primitives should be managed differently, even though it's more work for the coder.

Whipping Boy
01-22-2001, 06:42 PM
Stuka: It's not in string.h It's in "string"--just that. No .h

------------------
Kurt Weber
Shell scripts? Shell scripts? We don't NEED no stinkin' shell scripts!
White, heterosexual, middle-class, and proud!
I've never understood why Bill Gates would name his company after his penis

Sterling
01-22-2001, 06:45 PM
<rant>
Java's bizarre rules about operator overloading is one thing I cannot STAND about the language. All the docs say no operator overloading, EVER, which I could stand... But then they go and break that rule in half-a-dozen places with strings. And they don't break it consistantly, either.
</rant>

Anyway, if you're doing any decent amount of programming in C++, learn the STL. Even using just vectors and strings, and none of the more advanced stuff, will save you LOADS of work. C-style string and array manipulation has to be done very carefully, lest you create a buffer overflow bug. And its a pain anyway. C++ has classes that solve all these problems for you, for a slight (or not-so-slight, depending on implimentation) overhead.

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."

Xprotocol
01-22-2001, 08:37 PM
Stuka, something seems to be wrong, and I've tried including both string, and string.h, neither work. I just get errors like
"Undefined symbol 'string' in function main()"
Is there something I'm doing wrong?

Stuka
01-23-2001, 12:47 AM
Yeah, and the thing is that <string> and <string.h> are, IIRC, only for C-style strings. I think that the string class may actually be defined in <iostream>, but I'll check on that right now.<edit>OK, WB is dead on...it's in <string>...don't know how I missed it there, but it is...</edit><2nd edit>Do you have a 'using' directive? string may be part of the std namespace...if so, either add using namespace std; below your #include, or preface it like so:std::string One or both of those methods should work....</2nd edit>

[This message has been edited by Stuka (edited 23 January 2001).]

[This message has been edited by Stuka (edited 23 January 2001).]

Xprotocol
01-23-2001, 10:36 PM
Heh, no, my compiler has no idea what those are http://www.linuxnewbie.org/ubb/tongue.gif. But, I am compiling from windows using Borland C++, this really shouldn't make much of a difference though since C++ is standardized.

------------------
Check out my webpage
http://xprotocol.homestead.com

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."
--Albert Einstein

The King Ant
01-23-2001, 10:44 PM
C++ is supposed to be standardized, but I'm pretty sure the C++ string class has been around since the beginning of C++. I don't think the Borland compiler I used had libs for the string class. But that was a few years ago, and the Borland I was using was probably outdated anyhoo.

Just use char arrays, it's the way Bjorne wanted it, I think.

Er, sorry, bit of a typo there. Or, absense of a type... I meant, the string class has NOT been around the C++ was created. It was later implemented, probably because people were complaining that there wasn't one... I've heard it's actually faster than char arrays though...

[This message has been edited by The King Ant (edited 24 January 2001).]

nanode
01-24-2001, 01:44 PM
stuka:

I think I'm having trouble distinguishing the functional aspects from the OO aspects of C++.



Yeah, and the thing is that <string> and <string.h> are, IIRC, only for C-style strings. I think that the string class may actually be defined in <iostream>


So in the C++ language there are 2 things called 'string' - the C style (char array) and the string class.

This is less restrictive, but I can already see that it can lead to mistakes by novice coders. (like me)

Stuka
01-24-2001, 03:27 PM
Yeah, the naming was sorta inconvenient. Generally in C++ discussions, null-terminated strings are called "C-strings" or "C-style strings", while objects of the string class are called "strings". This helps, but of course it's still not terribly clear. I think the functional/OO differentiation is a little more difficult due to the required compatibility with C. I personally think that learning GOOD OO design in C++ is a great exercise for programmers - Java forces OO design on you, but C++ makes you work at it. Since there are relatively few standard objects (mostly the I/O streams, and the STL container classes, IIRC), you really have to design your program from the ground up, which the Java platform functionality really helps with. OK, I'm blabbering now...good luck learning, nanode, and feel free to ask questions, as I will ask you plenty of Java questions as I try to learn it!

Xprotocol
01-24-2001, 10:00 PM
Well, I guess I just need a new compiler then, the one I got is years old http://www.linuxnewbie.org/ubb/rolleyes.gif (Borland C++ 4.52). Thanks for all your helps guys, but I have one more quick question http://www.linuxnewbie.org/ubb/tongue.gif (I'm not saying the anwser will be quick http://www.linuxnewbie.org/ubb/smile.gif ). You guys keep talking about its good to write programs in OO ( I assume this means object oriented), but just what makes an OO program different from some other program?

------------------
Check out my webpage
http://xprotocol.homestead.com

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."
--Albert Einstein

[This message has been edited by Xprotocol (edited 24 January 2001).]

Sterling
01-25-2001, 11:35 AM
There are two major schools of structured programming. Object Oriented and Procedural. Object Oriented groups related functions and data types together into things called objects, which then get passed around and manipulated by your program. In theory, the object can hide all the guts from the program(s) using it, so they don't have to worry about that. Also in theory, it makes writing programs much quicker, as you can reuse objects from previous programs.

Procedural programming is the "traditional" style. You've got functions or procedures, sometimes divided up into modules or files, that operate on sets of structures and primitve data types.

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."

nanode
01-25-2001, 11:51 AM
I'll add a bit to what was already said:

Object vs. Functional

I like to think of the difference being in how you approach a problem. In the more traditional functional methodology (C, Perl, Pascal etc.) you break up your program into functional components. Each function does one thing.

OO goes a few steps further. You still have functions that each do a single 'function' but now those functions are the responsibility of objects.

A simple metaphor would be a person. You have a name, age, and maybe a current project you're working on. You also have the ability to communicate those facts to others.

If I want to know what your name is, I ask you. Since your name belongs to you, you might have restrictions that prevent anyone else from changing it, however your boss could ask you to change your project http://www.linuxnewbie.org/ubb/smile.gif Regardless, if any other party wants to get or set properies (facts) that you have, they must ask you to do it.

I know that sounds far out and spacey, but when you work with it for a while, it becomes the only way you can solve problems.

Good luck.

TheLinuxDuck
01-25-2001, 12:05 PM
Originally posted by nanode:
I know that sounds far out and spacey, but when you work with it for a while, it becomes the only way you can solve problems.

I wonder if I'll get to that stage.. ::sigh:: I've been doing linear programming since I started many years ago, and the whole OO stuff is still strange to me.. especially applying it to a project.

I still think very much linear and trying to learn C++ or java has been tricky. (especially since, with java, gui stuff is practically built in, and I never bothered to learn that stuff)

Given a problem, I can give you linear thoughts on how to solve the problem. Want me to OO it? Err.. give me a week to write out some notes and handle the basic concepts, then I'll get back to you with vague concepts and simplistic ideas. http://www.linuxnewbie.org/ubb/biggrin.gif

And maybe, if you're lucky, after that I'll actually have some ideas where to start coding. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

Stuka
01-25-2001, 04:46 PM
The moment I've been waiting for...I get to give TLD advice!!!! http://www.linuxnewbie.org/ubb/biggrin.gif
The basic approach to OO design is kinda obvious on the face of it - you look at a problem, and decide what objects it has. For example, in Hunt the Wumpus, you have a map, rooms, a player, a wumpus, a pit, and the bats. That's a decent start on what classes you need. Then decide what properties each class has (does a room have a pit? maybe a hasPit property would be useful...that kind of idea), and what methods/functions are needed to work on those properties. Does the room need a movePit method? Probably not - but you might want a setPit method to change the status of the property. Another important question is how the objects interact. This also helps define the necessary functions. There are, of course, more and more complex considerations, but these are the beginning steps to breaking down a problem in OO design. Just remember to design decently before you start writing code, and you should be OK!

Xprotocol
01-25-2001, 05:58 PM
So OO design is all in the way you write your code, not special commands you use?

------------------
Check out my webpage
http://xprotocol.homestead.com

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."
--Albert Einstein

jemfinch
01-25-2001, 10:58 PM
Originally posted by Sterling:
There are two major schools of structured programming.


You forget functional programming (haskell, erlang, and at the slightly less pure end, lisp and scheme).


Object vs. Functional

I like to think of the difference being in how you approach a problem. In the more traditional functional methodology (C, Perl, Pascal etc.) you break up your program into functional components. Each function does one thing.


C, Perl, and Pascal are procedural or imperative languages, not functional languages.

Procedural programming focuses on the problem in a top-down manner -- it takes a large program and continually breaks it into smaller chunks, when eventually the chunks become manageable.

Object Oriented programming focuses on the problem in a bottom-up manner -- it takes a large program and builds it from the objects it's composed of. Each object is manageable, and the idea is that they can be transferred to other problems more easily than functions.

I honestly don't know where functional programming fits in, since I don't do much of it yet.

Jeremy

Stuka
01-26-2001, 01:22 AM
Xprotocol:
That's dead-on. Certain languages (C++, Java, etc.) provide or require OO techniques. C++ can be used to write procedural code just like C. C can be used to write OO code - GTK+ was written that way. OO design is definitely a style, and not a technical thing. I'm sure that languages exist which do not allow OO programming, but who'd want to program in them? http://www.linuxnewbie.org/ubb/smile.gif