Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,665 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,161 people online right now. Registration is fast and FREE... Join Now!




Decryption Problem

2 Pages V  1 2 >  
Reply to this topicStart new topic

Decryption Problem, Cant make program right.

Rating  1
sf242
post 17 Jul, 2007 - 08:37 AM
Post #1


New D.I.C Head

*
Joined: 17 Jul, 2007
Posts: 42


My Contributions


im back, with a similar problem, but this time im decrypting. it says "character constant too long for its type". heres the basic structure:
CODE
#include <iostream>

using namespace std;

int main()
{ while (1==1)
{ int letter = '0';
cout << "";
cin >> letter;
if (letter=='000101100010')
{ cout << "a\n";}
else if (letter=='10111100100100101101')
{ cout << "b\n";}
else if (letter=='01001000111111011000')
{ cout << "c\n";}
else if (letter=='01110011101000110001')
{ cout << "d\n";}

anyone?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 08:41 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


You have declared letter as an integer, but you are assigning it initially a single character, then potentially several characters. What is your intent here? What are you expecting the user to enter? A binary string? If so, an integer type is not the right data type to use.
User is online!Profile CardPM

Go to the top of the page

sf242
post 17 Jul, 2007 - 08:49 AM
Post #3


New D.I.C Head

*
Joined: 17 Jul, 2007
Posts: 42


My Contributions


QUOTE(Amadeus @ 17 Jul, 2007 - 09:41 AM) *

You have declared letter as an integer, but you are assigning it initially a single character, then potentially several characters. What is your intent here? What are you expecting the user to enter? A binary string? If so, an integer type is not the right data type to use.

i think you answered on my last post, aboutthe encryption program. well, in this one, you enter the encrypted text (binary string esssentually), and it decodes it.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 08:51 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Well currently, you are declaring an integer named letter, then assigning a character to it. this means it will hold the ascii value of that character. you are then prompting the user to enter a string of characters, which cannot be held by an integer data type, and attempting to compare it to another string of characters. You will likely need to use a character array or string object.
User is online!Profile CardPM

Go to the top of the page

zyruz
post 17 Jul, 2007 - 08:57 AM
Post #5


New D.I.C Head

*
Joined: 13 Aug, 2005
Posts: 31


My Contributions


nm

This post has been edited by zyruz: 17 Jul, 2007 - 09:02 AM
User is offlineProfile CardPM

Go to the top of the page

sf242
post 17 Jul, 2007 - 09:05 AM
Post #6


New D.I.C Head

*
Joined: 17 Jul, 2007
Posts: 42


My Contributions


never used strings. example please?

This post has been edited by sf242: 17 Jul, 2007 - 09:08 AM
User is offlineProfile CardPM

Go to the top of the page

zyruz
post 17 Jul, 2007 - 09:09 AM
Post #7


New D.I.C Head

*
Joined: 13 Aug, 2005
Posts: 31


My Contributions


just include <string>


then change
int letter = '0';
to
std::string letter;

and change all your
'10111100100100101101' to "10111100100100101101" ( from '' to "")
or you wil have a probleme smile.gif
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 09:13 AM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


zyruz is correct:

Additional infor about strings:

http://www.cprogramming.com/tutorial/string.html

http://www.cppreference.com/cppstring/index.html

http://cs.smu.ca/~porter/csc/ref/c_cpp_strings.html
User is online!Profile CardPM

Go to the top of the page

sf242
post 17 Jul, 2007 - 09:18 AM
Post #9


New D.I.C Head

*
Joined: 17 Jul, 2007
Posts: 42


My Contributions


QUOTE(zyruz @ 17 Jul, 2007 - 10:09 AM) *

just include <string>


then change
int letter = '0';
to
std::string letter;

and change all your
'10111100100100101101' to "10111100100100101101" ( from '' to "")
or you wil have a probleme smile.gif

thanks it worked :-)
User is offlineProfile CardPM

Go to the top of the page

sf242
post 17 Jul, 2007 - 09:28 AM
Post #10


New D.I.C Head

*
Joined: 17 Jul, 2007
Posts: 42


My Contributions


i am making a program to decrypt text.
if i put in the code for 1 letter, it works fine. 2 or more letters, nothing happens. heres my code so far:
CODE
#include <iostream>
#include <string.h>

using namespace std;

int main()
{   std::string letter;
    while (1==1)
    { cin >> letter;
    if (letter=="00010110001011111110")
    { cout << "a\n";}
    else if (letter=="10111100100100101101")
    { cout << "b\n";}
    else if (letter=="01001000111111011000")
    { cout << "c\n";}
}
    return 0;
}

if i input "0001011000101111110", it says "a". but if i do "0001011000101111111000010110001011111110" it does nothing.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 09:31 AM
Post #11


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Actually, I'm surprised you got that to run at all:

CODE

#include <string.h>

should be
CODE

#include <string>


When you say it does nothing, what do you mean? For your second possibility, you have not established an if else case to take that into account.
User is online!Profile CardPM

Go to the top of the page

PennyBoki
post 17 Jul, 2007 - 09:33 AM
Post #12


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


Hi, that is because you only predicted three cases
and those are
00010110001011111110 for a
10111100100100101101 for b
01001000111111011000 for c
but you haven't define anything
about
0001011000101111111000010110001011111110
so it does nothing.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/23/08 05:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month