C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

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




ecrypt type program

 

ecrypt type program

kokoro

29 Nov, 2008 - 03:54 PM
Post #1

New D.I.C Head
*

Joined: 29 Oct, 2008
Posts: 29


My Contributions
since learning in class isn't enough for me i've been taking questions from the internet and making a program for them and i found a cool one.. you allow the user to insert a word (30chars max) and then allow them to choose a rotate number (btw 1-5) and then the rotate number moves each char in the word to output an encrypted word
CODE

main (void)
{
    char word[31];// store word room for null
    int rotation; //store users number
    char validword;// make sure its not number etc
    int validRotate;//make sure its btw 1-5

printf("Please insert word and or phrase ");
fgets(speech, 100, stdin);

printf("Please insert rotation number between 1-5");
fgets(rotation, 100, stdin);



}

i'm making the validations atm but what i cant figure out is since you don't know how long the user word is then i cant do
CODE
word[1]=word[1]+3 //3 rotate number
to get say A to D... so i was wondering how one goes about doing that and also i think my second fgets is wrong but i only know how to do it that way. Its just suppose to output the encrypted word and the original
thanks in advance!

This post has been edited by kokoro: 29 Nov, 2008 - 04:29 PM

User is offlineProfile CardPM
+Quote Post


KYA

RE: Ecrypt Type Program

29 Nov, 2008 - 05:17 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,186



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
Where is speech declared? Also does this have to be done in C?

Here's an implementation I threw together, what a fun concept smile.gif

cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{

string word;
char* temp;
int rotation = 0;
cout << "Enter word: ";
getline(cin, word);
cout << "enter rotation #: " ;
cin >> rotation;
temp = new char[word.size()+1];
strcpy(temp, word.c_str());

for(int i = 0; i < word.size()+1; i++)
{
temp[i] += rotation;
}

cout << "\nOriginal: ";
for(int i = 0; i < word.size() ; i++)
{
cout << word.at(i) << " ";
}


cout << "\nEncrypted: ";
for(int i = 0; i < word.size()+1; i++)
{
cout << temp[i] << " ";
}

cout << "\nDecrpyted: ";
for(int i = 0; i < word.size()+1; i++)
{
temp[i] -= rotation;
cout << temp[i] << " ";
}

delete[] temp;
system ("pause");
return 0;
}




User is offlineProfile CardPM
+Quote Post

kokoro

RE: Ecrypt Type Program

30 Nov, 2008 - 11:28 AM
Post #3

New D.I.C Head
*

Joined: 29 Oct, 2008
Posts: 29


My Contributions
QUOTE(KYA @ 29 Nov, 2008 - 05:17 PM) *

Where is speech declared? Also does this have to be done in C?

Here's an implementation I threw together, what a fun concept smile.gif

cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{

string word;
char* temp;
int rotation = 0;
cout << "Enter word: ";
getline(cin, word);
cout << "enter rotation #: " ;
cin >> rotation;
temp = new char[word.size()+1];
strcpy(temp, word.c_str());

for(int i = 0; i < word.size()+1; i++)
{
temp[i] += rotation;
}

cout << "\nOriginal: ";
for(int i = 0; i < word.size() ; i++)
{
cout << word.at(i) << " ";
}


cout << "\nEncrypted: ";
for(int i = 0; i < word.size()+1; i++)
{
cout << temp[i] << " ";
}

cout << "\nDecrpyted: ";
for(int i = 0; i < word.size()+1; i++)
{
temp[i] -= rotation;
cout << temp[i] << " ";
}

delete[] temp;
system ("pause");
return 0;
}



yea i'm learning c i'm guessing you did it in c++ cus i got no clue what cout does lol
User is offlineProfile CardPM
+Quote Post

KYA

RE: Ecrypt Type Program

30 Nov, 2008 - 11:30 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,186



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
cout is essentially the C++ version of printf, but its an object.
User is offlineProfile CardPM
+Quote Post

LaFayette

RE: Ecrypt Type Program

30 Nov, 2008 - 12:37 PM
Post #5

D.I.C Regular
Group Icon

Joined: 24 Nov, 2008
Posts: 255



Thanked: 28 times
Dream Kudos: 25
My Contributions
What your doing is called a Caesar Cipher. Later on you could a write a program that encrypts a plaintext with the more sophisticated Vigenère Cipher method. You could also do decryption programs for both of them (the latter one will be a real challange).
User is offlineProfile CardPM
+Quote Post

kokoro

RE: Ecrypt Type Program

30 Nov, 2008 - 01:42 PM
Post #6

New D.I.C Head
*

Joined: 29 Oct, 2008
Posts: 29


My Contributions
let me see if i get what your doing.. your transferring over the word into a a char called temp using the strcpy and just by using the pre increment it'll boost each letter by whatever i value is placed to? what does the
CODE
.size() +1
do exactly?
and is this
CODE
fgets(rotation, 100, stdin);
right? is fgets the same thing even tho im gettin numbers only and not chars?
User is offlineProfile CardPM
+Quote Post

KYA

RE: Ecrypt Type Program

30 Nov, 2008 - 02:02 PM
Post #7

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,186



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
size() + 1 is because a C++ string doesn't have the null terminator appended to the end like in C -> '\0'

So when I copy the elements over to the temp array I need to allocate an additional space in memory.

I make a temp one so that I can do other stuff with the original if I so wanted.

rotation will be a single integer, why do you need 100 psaces for it?
User is offlineProfile CardPM
+Quote Post

kokoro

RE: Ecrypt Type Program

2 Dec, 2008 - 03:41 PM
Post #8

New D.I.C Head
*

Joined: 29 Oct, 2008
Posts: 29


My Contributions
QUOTE(KYA @ 30 Nov, 2008 - 02:02 PM) *

size() + 1 is because a C++ string doesn't have the null terminator appended to the end like in C -> '\0'

So when I copy the elements over to the temp array I need to allocate an additional space in memory.

I make a temp one so that I can do other stuff with the original if I so wanted.

rotation will be a single integer, why do you need 100 psaces for it?


becuase in C i get the too few argument call error.. i dont no how to use fgets for just integer only know this way that i've done it for strings.. when you put the original into the temp array will that fill you in for the space you needed?
say i cpy speech[101] into tempSpeech and the user only inputs 5 letters will it make tempSpeech into a 5 element array?
User is offlineProfile CardPM
+Quote Post

KYA

RE: Ecrypt Type Program

2 Dec, 2008 - 03:47 PM
Post #9

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,186



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
To pick up single integers use scanf
User is offlineProfile CardPM
+Quote Post

KYA

RE: Ecrypt Type Program

2 Dec, 2008 - 05:00 PM
Post #10

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,186



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
I saw your message, if you need it in C# or java, i would recommend posting in the appropriate forum.
User is offlineProfile CardPM
+Quote Post

kokoro

RE: Ecrypt Type Program

2 Dec, 2008 - 05:37 PM
Post #11

New D.I.C Head
*

Joined: 29 Oct, 2008
Posts: 29


My Contributions
QUOTE(KYA @ 2 Dec, 2008 - 05:00 PM) *

I saw your message, if you need it in C# or java, i would recommend posting in the appropriate forum.

lol no neither nor c++ using c only only language i'm learning atm
User is offlineProfile CardPM
+Quote Post

KYA

RE: Ecrypt Type Program

2 Dec, 2008 - 06:28 PM
Post #12

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,186



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
Oh ok, your message was confusing, one sec i'll see to porting it to pure C.

edited for a typo

use fgets(), jsut be careful to not overwrite the buffer

This post has been edited by KYA: 2 Dec, 2008 - 06:38 PM
User is offlineProfile CardPM
+Quote Post

kokoro

RE: Ecrypt Type Program

3 Dec, 2008 - 02:07 PM
Post #13

New D.I.C Head
*

Joined: 29 Oct, 2008
Posts: 29


My Contributions
CODE

    char speech[31];
    int rotation=0;
    char validSpeech;
    int validRotate;

printf("Please insert word and or phrase ");
fgets(speech, 30, stdin);

printf("Please insert rotation number between 1-5 ");
fgets(rotation, sizeof (rotation), stdin);
validRotate = validRotation;

validRotation is a function in another source module that just checks to see if rotation is btw 1-5

I get an error right after I enter the word something runtime error telling me to abort so i guess my fget for my rotation is incorrect
User is offlineProfile CardPM
+Quote Post

KYA

RE: Ecrypt Type Program

3 Dec, 2008 - 02:50 PM
Post #14

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,186



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
cpp

fgets(speech, 31, stdin);


That should work:

Example:
cpp

#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
void Encrypt(char arr[], int size, int rotation)
{
for(int i = 0; i < size; i++)
{
arr[i] += rotation;
}
}

void Decrypt(char arr[] , int size, int rotation)
{
for(int i = 0; i < size; i++)
{
arr[i] -= rotation;
}
}
int main()
{

//string word;
char* temp;
char arr[31];
int rotation = 0;
cout << "Enter word: ";
fgets(arr, 31, stdin); //C
printf("enter rotation #: ");
scanf("%d", &rotation);
temp = new char[31];
strcpy(temp, arr);

Encrypt(temp, 31, rotation);
for(int i = 0; i <31; i++)
{
printf("%c", temp[i]);
}
Decrypt(temp, 31, rotation);
cout << endl;
for(int i = 0; i <31; i++)
{
printf("%c", temp[i]);
}
delete[] temp;
system ("pause");
return 0;
}




edit: The user has to fill it up though or you'll get un expected behavior. Which is why the string method from page 1 is better. Unfortunately you are restricted to C.

This post has been edited by KYA: 3 Dec, 2008 - 02:51 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/7/09 04:22PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month