6 Replies - 712 Views - Last Post: 17 March 2010 - 01:51 PM Rate Topic: -----

#1 Jahmbo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 17-March 10

Using variables and pointers

Posted 17 March 2010 - 11:37 AM

Hello,

I am teaching myself C++ as an exercise to keep my brain active and I have a question about using pointers to pass information between functions.

In the following code example I want to pass the 'mode' variable to a function, that function in turn will pass it along to some reuseable code. eg. the all functions will call a cleanup routine, the cleanup routine will behave differently depending on the mode the user has selected.

None of the pointer tutorials I have found make sense to me, or maybe I am thinking about it in the wrong way. I want to pass the mode forward to the other functions that may require it, then return the data backwards to the calling functions for processing.

If someone could point me to a tutorial that addresses my questions it would be greatly appreciated, or even a small code snippet demonstrating a solution to my query would also help greatly.

P.S. is putting 'mode' in the return of menu a proper way to do things?

Thanks,
Jahmbo

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <vector>

using namespace std;

// Prototypes
char menu();
void encrypt();
void decrypt();
void crack();
void get_data();
void load_data();

void main (){
    
    menu();
    
}

char menu(){
    
    char mode;
 
    bool quit = false;
    while(quit == false) {
        cout << "\n"
            "(e)ncrypt a file/string \n"
            "(d)ecrypt a file/string \n"
            "(c)rack an encrypted file/string \n"
            "(q) Quit\n"
            "Select a Menu Option :";
        char mode;
        cin >> mode;
        switch(mode){
            case 'e': encrypt(); break;
            case 'd': decrypt(); break;
            case 'c': crack(); break;
            case 'q': quit = true;  break;
            default : cout << "\n" << "try again" << "\n";
        }
     }
return mode;
}

void encrypt(){
     
     get_data();// data to be handled depending on what mode the user selected
     //some code here
     
}

void decrypt(){
     
     get_data();// data to be handled depending on what mode the user selected
     //some code here
     
}

void crack(){
     
     get_data();// data to be handled depending on what mode the user selected
     //some code here
     
}

void get_data(){
     
     load_data();/* data to be loaded depending on what mode the user selected,
                 will be stored in a string for processing by other functions
                 a pointer to the string will be needed*/         
     //some code here
     
}

void load_data(){
     
     // some code here     
     
}



Is This A Good Question/Topic? 0
  • +

Replies To: Using variables and pointers

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Using variables and pointers

Posted 17 March 2010 - 01:05 PM

You need to look up how functions receive and return values. You are not passing any value to any function.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Using variables and pointers

Posted 17 March 2010 - 01:08 PM

Here is a pointers tutorial, but frankly after reading your post several times I still have no clue as to how you are trying to use pointers or what problem(s) you are having.

Regarding your 'mode' question: I see nothing wrong with having each function return its mode value, but -- right now your main function ignores the return value of menu and there is no indication of how you propose to use it.

Also, I see no clue as to what it is that you want to "clean up."
Was This Post Helpful? 0
  • +
  • -

#4 Jahmbo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 17-March 10

Re: Using variables and pointers

Posted 17 March 2010 - 01:19 PM

View PostseeP+, on 17 March 2010 - 12:05 PM, said:

You need to look up how functions receive and return values. You are not passing any value to any function.


The code I posted was just a skeleton, I know nothing is being passed. My confusion comes from not knowing or understanding where to create the pointer. eg. when i do something like


void somefunction(*somestring);

void somefunction(*somestring){

string somestring;

    //code to manipulate the string

}



Is that the proper way to create and use a pointer so I can access the string from inside another function?
Was This Post Helpful? 0
  • +
  • -

#5 Jahmbo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 17-March 10

Re: Using variables and pointers

Posted 17 March 2010 - 01:30 PM

View Postr.stiltskin, on 17 March 2010 - 12:08 PM, said:

Here is a pointers tutorial, but frankly after reading your post several times I still have no clue as to how you are trying to use pointers or what problem(s) you are having.

Regarding your 'mode' question: I see nothing wrong with having each function return its mode value, but -- right now your main function ignores the return value of menu and there is no indication of how you propose to use it.

Also, I see no clue as to what it is that you want to "clean up."


I was afraid that it might be hard to understand what I am trying to do from my example, so here is the actual code of the program I am working on. Its an encrypter/decrypter/cracker for Vigenere ciphers. Right now I am working on the cracking function, but I want to be able to reuse some of the code related to concatenating/converting to uppercase/stripping non-alpha characters in the encrypt and decrypt functions. The code works, and correctly determines the key length from a raw ciphertext as long as it is purely alpha file with a key of less than 26 (arbitrary # I chose) characters in length.

I guess what I am asking is for help on trying to figure out how to break apart the code into helper functions. I will need access to the strings and variables between the functions.

I have attached an example of cipher text if anyone wants to compile and run against the cipher to see how it works.

/* Programming example to show passing of data between functions */
/* Function 1 will crack a Vigenere Cipher by calculating the Coincidence 
Index to calculate the key length then use standard deviation analysis to 
predict the most likely key before decrypting the message. */

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <vector>

using namespace std;

// Prototypes
void function_1();
void function_2();
void function_3();
char load_data();

int main (){
    bool quit = false;
    while(quit == false) {
        cout << "\n"
            "1) Function 1\n"
            "2) Function 2\n"
            "3) Function 3\n"
            "q) Quit\n"
            "Select a Menu Option :";
        char selection;
        cin >> selection;
        switch(selection){
            case '1': function_1(); break;
            case '2': function_2(); break;
            case '3': function_3(); break;
            case 'q': quit = true;  break;
            default : cout << "\n" << "try again" << "\n";
        }
     }
return 0;
}


void function_1(){
    cout << "This is Function 1, calling load_data()" << endl;
    load_data();
}

void function_2(){
    int number; 
    cout << "This is Function 2" << endl;
}

void function_3(){
    cout << "This is Function 3" << endl;
}

char load_data(){

    char filename[20];
    ifstream data_in;
    vector<string> words;
    string word;
    string data_conc;
    string data_alpha;

        cout << "Filename: " ;
        cin.ignore();
        cin.getline (filename,20); // get the filename from the user
        data_in.open (filename);  //open the file
        if(!data_in.is_open()){ // check to make sure it's open
           cout << "\n" << "Error opening file." << "\n" << endl;
           exit(0); // if not open, exit
        }
        else{
           cout << "\n" << "File read success." << "\n" << endl;
        }
        while(data_in >> word) // until we reach the end
           words.push_back(word); // push the file into memory word by word
        for(int i = 0; i < words.size(); i++){ // until we reach the end
           data_conc += words[i]; // clear out the whitespace
        }
        int i = 0;
        for (i = 0; data_conc[i]; i++){ // until we reach the end
           if(isalpha(data_conc[i])){ // check to see if char is alpha
              data_conc[i] = toupper(data_conc[i]); // if it is convert to upper
              data_alpha += data_conc[i]; // and put it in a new string
            }
        }
/*----------------------------------------------------------------------------*/
// Calculate Key Length


    int k=1;
    int num_shifts=26;
    float index=0;
    int key_len;
        for(k = 1; k < num_shifts; k++){
            int l = 0;
            int shift=k;
            float matches=0;
                for(l = 0; data_alpha[l + shift]; l++){
                    if(data_alpha[l] == data_alpha[l + shift]){
                        matches++;
                    }
                }
            index=(100 * matches / l);
               
            if (index >= 6) {break;}
    }
key_len=k;
cout << "Probable Key Length: " << key_len << endl;
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// Calculate Key Characters
/*
float en_std_dist[] = {8.12, 1.49, 2.71, 4.32, 12.02, 2.30, 2.03, 5.92, 7.31,
0.10, 0.69, 3.98, 2.61, 6.95, 7.68, 1.82, 0.11, 6.02, 6.28, 9.10, 2.88, 1.11,
2.09, 0.17, 2.11, 0.07};

int key_pos;
float char_dev;
int z=0;


    for(z = 0; z < 26; z++) {
          
        
        
    }
*/
return 0;
}




/* The standard distribution of french letters. */
float fr_std_dist[] = {};

/* The standard distribution of german letters. */
float gr_std_dist[] = {};

/* The standard distribution of italian letters. */
float it_std_dist[] = {};


Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Using variables and pointers

Posted 17 March 2010 - 01:45 PM

Here is an example. Hope you can understand it.
//This program implements the area of the painted room.
#include <iostream>
#include <string>
using namespace std;
void getPointer(string *); //function prototype
int main ()
{
        string *name;                 //declare pointer
        string first = "";            //
        
        cout<<"Enter your name: ";
        cin>>first;                   
        name = &first;                //stores gives address of first to name
        //
        getPointer(name);             //passes the pointer
      
        

        system("pause");
        return 0;
        }
//
void getPointer(string *you)
{
     cout<<"Hello "<<*you<<endl;      //make sure the asterisk is included or it 
                                      //will show give you the address
}



This post has been edited by seeP+: 17 March 2010 - 01:45 PM

Was This Post Helpful? 1
  • +
  • -

#7 Jahmbo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 17-March 10

Re: Using variables and pointers

Posted 17 March 2010 - 01:51 PM

View PostseeP+, on 17 March 2010 - 12:45 PM, said:

Here is an example. Hope you can understand it.


I think I understand it, seeing it in action like that. I will play around with it later tonight in my cracker program and see if I can break apart the code into smaller functions and still make it work.

Thanks a ton for the example.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1