Solved

{ENTER}

Posted on 2007-11-30
7
174 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now