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

Join 149,415 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,215 people online right now. Registration is fast and FREE... Join Now!




I need help writing a c++ program

 
Reply to this topicStart new topic

I need help writing a c++ program, involves functions

bloodbrother778
26 Feb, 2007 - 09:30 PM
Post #1

New D.I.C Head
*

Joined: 26 Feb, 2007
Posts: 3


My Contributions
Hey guys I have a program that I need to write and I have no idea how to do it:
this program has to prompt the user in a menu to encrypt a message, decrypt or quit.
I'm supposed to use functions in this program
This is what i am supposed to use without removing any parts, just adding things where needed.
CODE

#include<iostream>
using namespace std;

int menu();
void encrypt(int);
void decrypt();


int main(){

int choice;
int shift = 7; // default shift
choice = menu();
while (choice != 3){
if (choice == 1){
cout << "Enter the 'shift' value: ";
cin >> shift;
encrypt(shift); }
else
decrypt();

cout<<endl<<endl<<endl;
choice = menu();
}
return 0;
}

I don't even know how to get started functions kind of confuse me
Can someone at least point me in the right direction?

When you enter a message its supposed to print out numbers and vice versa when decrypted, the numbers are as follows:

1= k 10= q 19 =t
2 =n 11 =s 20 =v
3 =h 12 =i 21= f
4 =b 13 =y 22 =e
5 =w 14 =x 23 =m
6 =a 15 =j 24 =u
7 =d 16 =r 25 c
8 =l 17 =z 26 p
9 =o 18= g 27 =. (period)
User is offlineProfile CardPM
+Quote Post

bloodbrother778
RE: I Need Help Writing A C++ Program
26 Feb, 2007 - 10:28 PM
Post #2

New D.I.C Head
*

Joined: 26 Feb, 2007
Posts: 3


My Contributions
ok heres what i have so far:
CODE

#include<iostream>
using namespace std;

int menu(){
    cout<<"Press 1 to encrypt, 2 to decrypt, 3 to quit)"<<endl;
    int x;
    cin>>x;
    return x;
    
}
void encrypt(char letter){
    cout<<"Enter a message to encrypt (! to quit)"<<endl;
    cin>>letter;
    while (letter!='!'){
        if (letter=='k') cout<<"1 ";
        if (letter=='n') cout<<"2 ";
        if (letter=='h') cout<<"3 ";
        if (letter=='v') cout<<"4 ";
        if (letter=='w') cout<<"5 ";
        if (letter=='a') cout<<"6 ";
        if (letter=='d') cout<<"7 ";
        if (letter=='l') cout<<"8 ";
        if (letter=='o') cout<<"9 ";
        if (letter=='q') cout<<"10 ";
        if (letter=='s') cout<<"11 ";
        if (letter=='i') cout<<"12 ";
        if (letter=='y') cout<<"13 ";
        if (letter=='x') cout<<"14 ";
        if (letter=='j') cout<<"15 ";
        if (letter=='r') cout<<"16 ";
        if (letter=='z') cout<<"17 ";
        if (letter=='g') cout<<"18 ";
        if (letter=='t') cout<<"19 ";
        if (letter=='v') cout<<"20 ";
        if (letter=='f') cout<<"21 ";
        if (letter=='e') cout<<"22 ";
        if (letter=='m') cout<<"23 ";
        if (letter=='u') cout<<"24 ";
        if (letter=='c') cout<<"25 ";
        if (letter=='p') cout<<"26 ";
        if (letter=='.') cout<<"27 ";
    cin>>letter;
    }
    return;
}
            


    void decrypt(){
        cout<<"Enter a message to decrypt (0 to quit)"<<endl;
        int code;
        cin>>code;
        while (code!=0){
            if (code==1) cout<<"k";
            if (code==2) cout<<"n";
            if (code==3) cout<<"h";
            if (code==4) cout<<"b";
            if (code==5) cout<<"w";
            if (code==6) cout<<"a";
            if (code==7) cout<<"d";
            if (code==8) cout<<"l";
            if (code==9) cout<<"o";
            if (code==10) cout<<"q";
            if (code==11) cout<<"s";
            if (code==12) cout<<"i";
            if (code==13) cout<<"y";
            if (code==14) cout<<"x";
            if (code==15) cout<<"j";
            if (code==16) cout<<"r";
            if (code==17) cout<<"z";
            if (code==18) cout<<"g";
            if (code==19) cout<<"t";
            if (code==20) cout<<"v";
            if (code==21) cout<<"f";
            if (code==22) cout<<"e";
            if (code==23) cout<<"m";
            if (code==24) cout<<"u";
            if (code==25) cout<<"c";
            if (code==26) cout<<"p";
            if (code==27) cout<<".";
        cin>>code;
        }
        return;
    }
        



    int main(){
    
      int choice;
      int shift = 7; // default shift
      choice = menu();
      while (choice != 3){
          if (choice == 1){
            cout << "Enter the 'shift' value: ";
            cin >> shift;
            encrypt(shift);              }
         else
            decrypt();
      
         cout<<endl<<endl<<endl;
         choice = menu();
      }

      return 0;
    }


The code doesnt really work right, when I enter the messages im supposed to enter it doesnt print out the right thing
also I need to somehow change the encrypt (char) function to encrypt (int) how do i do this?
and finally the program doesnt seem to run for as long as I want until im ready to press 3, which is what i need to have. How do i do that?
User is offlineProfile CardPM
+Quote Post

bloodbrother778
RE: I Need Help Writing A C++ Program
26 Feb, 2007 - 10:57 PM
Post #3

New D.I.C Head
*

Joined: 26 Feb, 2007
Posts: 3


My Contributions
ok actually I have most of it figured out
the only real problem left is that the program doesnt seem to take into account the shift value
when i type in a message it prints out the things that its supposed to correspond to, but what i need is the shift value added to that number
like say if i enter the shift value to be 7
and then i encrypt k, the printed value should be 8
but the program doesnt do that
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: I Need Help Writing A C++ Program
27 Feb, 2007 - 12:10 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
I *think* what you want to do in your "encrypt" routine is:

CODE

void encrypt(char letter){
int value;
cout<<"Enter a message to encrypt (! to quit)"<<endl;
cin>>letter;
while (letter!='!'){
if (letter=='k') value=1;
if (letter=='n') value=2;
if (letter=='h') value=3;
if (letter=='v') value=4;
if (letter=='w') value=5;
if (letter=='a') value=6;
if (letter=='d') value=7;
if (letter=='l') value=8;
if (letter=='o') value=9;
if (letter=='q') value=10;
if (letter=='s') value=11;
if (letter=='i') value=12;
if (letter=='y') value=13;
if (letter=='x') value=14;
if (letter=='j') value=15;
if (letter=='r') value=16;
if (letter=='z') value=17;
if (letter=='g') value=18;
if (letter=='t') value=19;
if (letter=='v') value=20;
if (letter=='f') value=21;
if (letter=='e') value=22;
if (letter=='m') value=23;
if (letter=='u') value=24;
if (letter=='c') value=25;
if (letter=='p') value=26;
if (letter=='.') value=27;
// Now we want to add in the shift...
//We can use the % operator to ensure that 28 ==> 1
// the  n % 27 will return the remainder of n/27
// which is in the set [0, 26], so to ensure that 1 shift 7 return 8
// and 27 shift 7 returns 7 we will map [1, 27] to [0, 26] and back again.
value = ((value - 1) + shift) % 27 + 1;
cout << value;
cin>>letter;
}
return;
}


This post has been edited by NickDMax: 27 Feb, 2007 - 12:10 AM
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: I Need Help Writing A C++ Program
28 Feb, 2007 - 01:12 PM
Post #5

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
I think you need to move the line: "choice = menu();"
inside of the while loop. This means "choice" needs to be initialized.

This post has been edited by ajwsurfer: 28 Feb, 2007 - 01:13 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: I Need Help Writing A C++ Program
28 Feb, 2007 - 01:12 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,347



Thanked: 51 times
Dream Kudos: 25
My Contributions
It appears as if it is inside the while loop already.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:58AM

Be Social

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

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month