-2

I wrote this and when I launch it, it gives me all the posibilities. I want it to just give me the one that corresponds with my char I've typed in. Can you tell me what's wrong here? Thanks in advance.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
char letter='b';
while(letter!='end')
{
    cout<<"What is the letter you wanna convert?"<<endl;
    cin >> letter;
    if(letter='A') {cout<<"01000001"<<endl;}
    if(letter='B') {cout<<"01000010"<<endl;}
    if(letter='C') {cout<<"01000011"<<endl;}
    if(letter='D') {cout<<"01000100"<<endl;}
    if(letter='E') {cout<<"01000101"<<endl;}
    if(letter='F') {cout<<"01000110"<<endl;}
    if(letter='G') {cout<<"01000111"<<endl;}
    if(letter='H') {cout<<"01001000"<<endl;}
    if(letter='I') {cout<<"01001001"<<endl;}
    if(letter='J') {cout<<"01001010"<<endl;}
    if(letter='K') {cout<<"01001011"<<endl;}
    if(letter='L') {cout<<"01001100"<<endl;}
    if(letter='M') {cout<<"01001101"<<endl;}
    if(letter='N') {cout<<"01001110"<<endl;}
    if(letter='O') {cout<<"01001111"<<endl;}
    if(letter='P') {cout<<"01010000"<<endl;}
    if(letter='Q') {cout<<"01010001"<<endl;}
    if(letter='R') {cout<<"01010010"<<endl;}
    if(letter='S') {cout<<"01010011"<<endl;}
    if(letter='T') {cout<<"01010100"<<endl;}
    if(letter='U') {cout<<"01010101"<<endl;}
    if(letter='V') {cout<<"01010110"<<endl;}
    if(letter='W') {cout<<"01010111"<<endl;}
    if(letter='X') {cout<<"01011000"<<endl;}
    if(letter='Y') {cout<<"01011001"<<endl;}
    if(letter='Z') {cout<<"01011010"<<endl;}

    if(letter='a') {cout<<"01100001"<<endl;}
    if(letter='b') {cout<<"01100010"<<endl;}
    if(letter='c') {cout<<"01100011"<<endl;}
    if(letter='d') {cout<<"01100100"<<endl;}
    if(letter='e') {cout<<"01100101"<<endl;}
    if(letter='f') {cout<<"01100110"<<endl;}
    if(letter='g') {cout<<"01100111"<<endl;}
    if(letter='h') {cout<<"01101000"<<endl;}
    if(letter='i') {cout<<"01101001"<<endl;}
    if(letter='j') {cout<<"01101010"<<endl;}
    if(letter='k') {cout<<"01101011"<<endl;}
    if(letter='l') {cout<<"01101100"<<endl;}
    if(letter='n') {cout<<"01101110"<<endl;}
    if(letter='o') {cout<<"01101111"<<endl;}
    if(letter='p') {cout<<"01110000"<<endl;}
    if(letter='q') {cout<<"01110001"<<endl;}
    if(letter='r') {cout<<"01110010"<<endl;}
    if(letter='s') {cout<<"01110011"<<endl;}
    if(letter='t') {cout<<"01110100"<<endl;}
    if(letter='u') {cout<<"01110101"<<endl;}
    if(letter='v') {cout<<"01110110"<<endl;}
    if(letter='w') {cout<<"01110111"<<endl;}
    if(letter='x') {cout<<"01111000"<<endl;}
    if(letter='y') {cout<<"01111001"<<endl;}
    if(letter='z') {cout<<"01111010"<<endl;}
    getche();
}
return 666;

}

2
  • 3
    Find out how to turn on warnings in your compiler/IDE. Commented Sep 12, 2015 at 12:44
  • 1
    @infiltrait0rN77, If you have so many if statement it is better to use switch and just check the cases. like switch (letter) {case 'A': cout<<"01000001"<<endl; break; ... } Commented Jan 21, 2019 at 15:06

3 Answers 3

8

As for your title, and ignoring the trivial errors in your code, the easiest solution is

cout<<"What is the letter you wanna convert?"<<endl;
cin >> letter;
cout << bitset<8>(letter).to_string() << endl;

No need for hard coded conversion.


Also note, if this question was about academic reasons, and you're asked to develop a solution to generate the string using a generic algorithm, your solution would gain an F school note from me.

You've been probably expected to use ℅2 modulo operations correctly within a loop (wasn't that part of your recent lectures?).

If not, I'd strongly recommend to go with the std::bitset solution, instead of reinventing the wheel.

Sign up to request clarification or add additional context in comments.

2 Comments

Nah, just me fooling around. Thanks for your response. :)
@Infiltrait0rN77 Updated my answer with more reference.
3

You want if (letter == ...), not if (letter = ...)

"Can you tell me what's wrong here?"

When you do:

if (foo = bar)

...you are affecting the value bar to foo. If the resulting bar value is non-zero, the condition is true.

Welcome to programming.

2 Comments

Legion would like to thank you for helping him. :D How come I missed that... Thanks again.
When your testing against a constant, a handy idiom is if (constant == variable): It will not compile if you mistyped '='. Sh*t happens, even worse when copy/pasted all over the place.
0

When you code -

if(a==anything) {statements}, it checks your code, if it is true it executes your statements

When you code -

If( a= anything) {statements} ,then it assigns the value ( anything) to the variable (a) and executes your statements

<pre><code>

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{char letter='b';
cout<<"letter --> binary\nenter 0 to exit\n\n";
(letter!='0')
{
cout<<"\nWhat is the letter you wanna convert? - ";
cin >> letter;
if(letter=='A') {cout<<"01000001"<<endl;}
if(letter=='B') {cout<<"01000010"<<endl;}
if(letter=='C') {cout<<"01000011"<<endl;}
if(letter=='D') {cout<<"01000100"<<endl;}
if(letter=='E') {cout<<"01000101"<<endl;}
if(letter=='F') {cout<<"01000110"<<endl;}
if(letter=='G') {cout<<"01000111"<<endl;}
if(letter=='H') {cout<<"01001000"<<endl;}
if(letter=='I') {cout<<"01001001"<<endl;}
if(letter=='J') {cout<<"01001010"<<endl;}
if(letter=='K') {cout<<"01001011"<<endl;}
if(letter=='L') {cout<<"01001100"<<endl;}
if(letter=='M') {cout<<"01001101"<<endl;}
if(letter=='N') {cout<<"01001110"<<endl;}
if(letter=='O') {cout<<"01001111"<<endl;}
if(letter=='P') {cout<<"01010000"<<endl;}
if(letter=='Q') {cout<<"01010001"<<endl;}
if(letter=='R') {cout<<"01010010"<<endl;}
if(letter=='S') {cout<<"01010011"<<endl;}
if(letter=='T') {cout<<"01010100"<<endl;}
if(letter=='U') {cout<<"01010101"<<endl;}
if(letter=='V') {cout<<"01010110"<<endl;}
if(letter=='W') {cout<<"01010111"<<endl;}
if(letter=='X') {cout<<"01011000"<<endl;}
if(letter=='Y') {cout<<"01011001"<<endl;}
if(letter=='Z') {cout<<"01011010"<<endl;}

if(letter=='a') {cout<<"01100001"<<endl;}
if(letter=='b') {cout<<"01100010"<<endl;}
if(letter=='c') {cout<<"01100011"<<endl;}
if(letter=='d') {cout<<"01100100"<<endl;}
if(letter=='e') {cout<<"01100101"<<endl;}
if(letter=='f') {cout<<"01100110"<<endl;}
if(letter=='g') {cout<<"01100111"<<endl;}
if(letter=='h') {cout<<"01101000"<<endl;}
if(letter=='i') {cout<<"01101001"<<endl;}
if(letter=='j') {cout<<"01101010"<<endl;}
if(letter=='k') {cout<<"01101011"<<endl;}
if(letter=='l') {cout<<"01101100"<<endl;}
if(letter=='n') {cout<<"01101110"<<endl;}
if(letter=='o') {cout<<"01101111"<<endl;}
if(letter=='p') {cout<<"01110000"<<endl;}
if(letter=='q') {cout<<"01110001"<<endl;}
if(letter=='r') {cout<<"01110010"<<endl;}
if(letter=='s') {cout<<"01110011"<<endl;}
if(letter=='t') {cout<<"01110100"<<endl;}
if(letter=='u') {cout<<"01110101"<<endl;}
if(letter=='v') {cout<<"01110110"<<endl;}
if(letter=='w') {cout<<"01110111"<<endl;}
if(letter=='x') {cout<<"01111000"<<endl;}
if(letter=='y') {cout<<"01111001"<<endl;}
if(letter=='z') {cout<<"01111010"<<endl;}
getch();
}
return 666;}

3 Comments

Your explanation is incomplete and does not actually explain the simple typo. Also, the code is formatted oddly.
Don't put details in comments! Edit the Answer and make your changes there. Answers should be stand-alone and comments can be deleted. I didn't ask for you to explain it to me in a comment, but rather present your answer in a clear and obvious way for everyone. (And to fix the broken formatting.) See How to Answer. (BTW, your explanation isn't quite right anyway. Do you know why is f=anything returning true?)
Ok, thanks for 👍 suggesting

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.