5 Replies - 735 Views - Last Post: 04 September 2009 - 03:00 PM Rate Topic: -----

#1 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Control structures and strings

Posted 04 September 2009 - 01:30 PM

Hey everyone.

So I'm practicing some programming and I came up with an idea to make a simple program. What I want to do is make a program that asks the user to type in country or capital, and then the result will the corresponding country or capital depending on what the user types in.

So here is my question: how exactly would I combine a control structure such as an "if" statement with strings? I've been able to get integers running with if statements but I have no clue where to start when it comes strings.

Can anyone nudge me in the right direction on this? I am working in a Unix environment by the way.

- Thank-you

Is This A Good Question/Topic? 0
  • +

Replies To: Control structures and strings

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Control structures and strings

Posted 04 September 2009 - 01:37 PM

are you refering to the string class or to char arrays...

for char arrays you have to use cmpstr() to compare them, with C++ string you can use the compare function or the regular operators.

I actually made a snippet (Cstring comparison wrapper) that will wrap char arrays and allow you use do comparisons using the standard operators.
Was This Post Helpful? 1
  • +
  • -

#3 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: Control structures and strings

Posted 04 September 2009 - 01:52 PM

Well to be honest, I don't know exactly which I'm referring to. I'm just sort of trying to make programs on my own and learn along the way as my professor does more talking about theories than actual codes..

Basically the idea I have of how this code should be is that I would have to define which capitals and countries go together and have those be my variables. Then I would make some code that goes along the lines of "if input equals country A, then output will equal capital A." Now that's where I get lost as to how I would do that.

Here is basically what I want to do, only I think that I would have to use strings and getline statements in order to work with words. So something along these lines:
if (x == 100)
{
   cout << "x is ";
   cout << x;
}



How would I modify a code like this to work with words?


Edit: I'm looking at the comparing method right now. I think that may do actually. Thank-you! :)

This post has been edited by nautica17: 04 September 2009 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Control structures and strings

Posted 04 September 2009 - 02:37 PM

So if you are using C++ strings (i.e. #include <string>) then you can just do this:

string x = "Hello";
if (x == "Hello") { cout << "tada\n"; }


If you are NOT using C++ strings (i.e. you are using C strings -- char arrays) then you have to do this:
char x[] = "Hello";
if (strcmp(x, "Hello") == 0) { cout << "tada\n"; }


So this is why I made that snippet -- using the snippet code you can do this:
char x[] = "Hello";
if (Str(x) == Str("Hello")) { cout << "tada\n"; }


note that there are other peculiarties when comparing strings -- for example the comparison is case sensitive, it has a very odd ordering (based upon the ASCII table) so "A100" < "A11" (though I ran across someone who has gone out and fixed that with his alphanum comparison.
Was This Post Helpful? 0
  • +
  • -

#5 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: Control structures and strings

Posted 04 September 2009 - 02:53 PM

View PostNickDMax, on 4 Sep, 2009 - 01:37 PM, said:

So if you are using C++ strings (i.e. #include <string>) then you can just do this:

string x = "Hello";
if (x == "Hello") { cout << "tada\n"; }


If you are NOT using C++ strings (i.e. you are using C strings -- char arrays) then you have to do this:
char x[] = "Hello";
if (strcmp(x, "Hello") == 0) { cout << "tada\n"; }


So this is why I made that snippet -- using the snippet code you can do this:
char x[] = "Hello";
if (Str(x) == Str("Hello")) { cout << "tada\n"; }


note that there are other peculiarties when comparing strings -- for example the comparison is case sensitive, it has a very odd ordering (based upon the ASCII table) so "A100" < "A11" (though I ran across someone who has gone out and fixed that with his alphanum comparison.



Thank-you so much! That solved it! :) Exactly what I was looking for. I just didn't how to put it in code.
Was This Post Helpful? 0
  • +
  • -

#6 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: Control structures and strings

Posted 04 September 2009 - 03:00 PM

del

Nevermind, I got it working.

This post has been edited by nautica17: 04 September 2009 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1