Solved

{ENTER}

Posted on 2007-11-30
7
179 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

615 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