Solved

{ENTER}

Posted on 2007-11-30
7
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

751 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