Solved

{ENTER}

Posted on 2007-11-30
7
177 Views
Last Modified: 2010-04-01

#include <iostream>

using namespace std;


int main ()
{
      
      string string1 = "{ENTER}";

      if ( string1[0] == "{" )
      {
            // so forth
      }
      
      
      
      
      
      return 0;
      
      
}





how do I make it work?

I just need to compare the n th char with a " // some string " 


i don't know, is this the way things are done in c++?
0
Comment
Question by:Troudeloup
  • 4
  • 2
7 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 20386771
Could you rephrase your question?
0
 

Author Comment

by:Troudeloup
ID: 20386774
     if ( string1[0] == "{" )



pops


test2.cpp:11: error: ISO C++ forbids comparison between pointer and integer




I need to know what the first character is in the string, so I used a if, but it's not allowed becaus I am comparing different type


thank you for cuing me to rephrase, I thought it was ok.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 20386787
ah, sure, it should be:
     if ( string1[0] == '{' )

with single quotes, you must compare character with character, not character with string
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
ID: 20387643
Just as a point of interest, to avoid accidental assignments when doing equality tests try putting the rvalue (constant) on the left as it will then generate an error if you get it wrong so you will know at compile time you have made a mistake. In your example the code would have built but you'd have had unexpected behavior.

To clarify...

You had this: if ( string1[0] = '{' )

If you'd written it like this: if ( '{' = string1[0] ) the compiler would have notified you of your accidental assignment: error C2106: '=' : left operand must be l-value

So you'd know there was an issue and you could then correct it: if ( '{' == string1[0] ) which is semantically identical to this: if ( string1[0] == "{" ) but with the advantage of the extra compile time check!

-Rx.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
ID: 20387646
>> You had this: if ( string1[0] = '{' )

Actually, (my mistake) you had this, if ( string1[0] == "{" ), but the tip is still valid and worth remembering as it can save you many hours of wasted time trying ti find an elusive accidental assignment!
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
ID: 20387655
...I meant to say once you follow Jaime's advice you'll have this: if ( string1[0] = '{' )

I'm not well today :(
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
ID: 20387661
ARRRGGG!!!

you'll have this: if ( string1[0] == '{' )

You know what I mean. I'm going back to bed :(
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question