Solved

{ENTER}

Posted on 2007-11-30
7
173 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now