1

I thought I would try and write some encryption program that converts input to numbers in a file.

I made a table, giving each letter its own number. The code itself went a little like this for each letter:

 if (Letter = "P") { FILEO.open("Encrypted.txt", ios::app); FILEO << " 259"; FILEO.close(); } 

It came up with "cannot convert from 'const char [2]' to 'char'"

Can anyone suggest how I would go about actually getting a number from a letter?

3
  • 1
    What is the data type of Letter? Is it a string, or a character array? Commented Mar 15, 2012 at 7:51
  • 1
    I'm guessing Letter is a char. Using double quotes makes a string literal. To just denote the character P, use single quotes 'P'. Also, are you really trying to set Letter (=), or check its value (==)? Commented Mar 15, 2012 at 7:55
  • 4
    And to answer your next question "Why is my program so slow?": don't open and close the output file for every letter. :) Commented Mar 15, 2012 at 8:01

6 Answers 6

2

If Letter is a char, use a char literal:

if (Letter == 'P') ... 
Sign up to request clarification or add additional context in comments.

Comments

2

Your conditional checking is wrong. It should be ==, not =. A single = means assignment whereas a == means conditional checking.

I am assuming Letter is a character array. In that case, you can use strcmp to compare it with P.

if(strcmp(Letter, "P") == 0) { // rest of the code } 

Take a look at the strcmp function reference here, if necessary.

If Letter is simply a char, then you need to compare it with P like this -

if(Letter == 'P') { // rest of the code } 

A single quote around a character makes it a character literal, which then can be compared against another character using ==.

Comments

2

You can not compare C++ char to C++ string! You should use single quote for chars, not double quotes. Also, the C++ equals operator is not =, it is ==. the single = is the assignment operator.

You should write the condition like this :

if (Letter == 'P') { FILEO.open("Encrypted.txt", ios::app); FILEO << " 259"; FILEO.close(); } 

Comments

1
(Letter = "P") 

This is an assignment, not comparison.

You probably meant (Letter == "P") which would also be wrong, you need strcmp.

Comments

1

you need to use strcmp to compare....as = is an assignment operator....

Comments

1

I would recommend that when you give us an error message as you did, you give us the full message - including line numbers so that we know where the error occurred (or tell us what line it occurred at). Paying attention to those line numbers can greatly help finding the true problem.

Given the error message I'm assuming Letter is of type char - you need to understand the difference between literal strings (enclosed in double quotes) and literal characters (enclosed in single quotes).

As Luchian also mentioned, you have an assignment rather than an equality test - unlike Visual Basic, if that is where you're coming from, the two have different symbols.

That should thus be:

if (Letter == 'P') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.