Need help

Uzor

... A Man For All Seasons
Adventurer
Joined
Aug 10, 2004
Messages
10,285
Age
39
Location
Sweden
Nation Banner
Sandy
Main Job
WhiteMage-Male
Secondary Job
RedMage-Male
Third Job
BlackMage-Male
Server
Fenrir
Linkshell
Avaritas
FFXIV
Uzor
ok... I have a project due Friday, where we're supposed to make "cyphered messages". Basically, the user is supposed to be able to insert a number, and then the program will generate numbers based on user input.

Example: User inputs the number 3.

The program generates 3 random numbers between 0 to 9.

Those part works fine, even though I'll look through them later to make it work better. The thing is, I can't get it to cypher a message you input. I was thinking you need to use the string.size() function in order to count the lenght of the word, and then use a for-loop to loop through each of the numbers 1 at a time and add the number to the letter. An example would be a + 3 = d. Since c++ supposedly follows the ascii standard, I'd figure the number a(97) would become 100. The problem is, that it obviously isn't as simple as that, for more then 1 reason

First being that z would become }, and Z would become ]. Second is that I can't even get it to translate the letter a and b correctly.

Help appreciated ~.~ if I don't get this to work by Friday I'll fail ; ;

Code:
#include <iostream>  // Loading library containing cout
#include <ctime>     // Loading library containing rand()
#include <string>    // Loading the library containing string, size etc.

using namespace std;

void menu();
void cypher();

int main()
{
    menu();
    int choice;
    cin >> choice;

    while(choice != 3){
        switch (choice)
        {
        case 1  : cypher();
            break;
        
        case 2  : 2;
            break;

        case 3  : cout << "quit";
            break;
        
        default : cout << "That option does not exist. Please chose another one.";
        
        }
        menu();
        cin >> choice;
    }
}

void menu() // Function which creates the menu.
{
    cout << "Type in a number corresponding to the choices below.\n"
    "1) Generate a new cypher message.\n"
    "2) Decypher a cyphered message.\n"
    "3) Quit\n";
}

void cypher() // Function which lets you cypher the word
{
    int length;
    string word;
    srand((unsigned)time(NULL));
    int ckey[10];

    cout << "How many numbers do you want the cypher key to be? ";
    cin >> length;
    cout << "\nYour numbers are: ";

    for (int i=1; i<=length; i++)
    {

        ckey[i] = rand() % 10; // Generates a number between 0 to 9 and saves them
        cout << ckey[i] << ", ";
    }

    cout << "\n\nEnter message you wish to be cyphered: ";
    cin >> word;

    cout << "\nCyphered, your message is the following: ";
    for (int i=0; i<= word.size()-1; i++)
    {
        word[i] = word[i] + ckey[i];
    }
    cout << word << endl;
}
Yes, the code is incomplete. I've worked 1 step at a time, and I got stuck on the last part, so I haven't completet it yet <.<;

The problem lies in the cypher() function, btw, so you don't have to read through it all xD
 
Uzor said:
I was thinking you need to use the string.size() function in order to count the lenght of the word, and then use a for-loop to loop through each of the numbers 1 at a time and add the number to the letter. An example would be a + 3 = d. Since c++ supposedly follows the ascii standard, I'd figure the number a(97) would become 100. The problem is, that it obviously isn't as simple as that, for more then 1 reason

Your logic is correct, but you need to remember that the string type kinda wraps standard C strings (which are indeed nothing more than character arrays). Use the c_str() function to get it in character array form: word.c_str().

So here's an example of stepping through a string of string.h type:
Code:
int size = word.size();
const char *z = word.c_str();
for( int i=0; i<size; i++)
{
    printf ("%c\n", z[i]);  //prints each character on a new line.
}

Now, a thing to note is that the return type coming from c_str() is immutable; it's a const char*, meaning you won't be able to change it directly.
You'll have to construct your new cypher string as you read through the character array. I suggest just concatinating onto a new empty string as you step through:

Code:
string cypher;
...
cypher += (z[i] + 1);
...

Easy Peasy :)

Hope this helps.
 
Last edited:
thanks ^^

Also, what exactly does c_str() do? Our teacher never mentined that one >.>; I need to be able to explain in case he asks <.<
 
Uzor said:
thanks ^^

Also, what exactly does c_str() do? Our teacher never mentined that one >.>; I need to be able to explain in case he asks <.<

It returns a pointer to the actual character array that represents the string.
(has he explained pointers?)

A string in it's most primative form is just a character array:
char s[128] = "foobar";

What string.h does is give you functions to work on strings easily. When you use string.h in conjunction with the std namespace, you can use C++ magic and declare a "string" type. So instead, you can say:
string s = "foobar";

But at that point you've wrapped the data in the "string" type and are not dealing with the primative character array anymore; so you need some way to get to it. That's where c_str() comes in. It returns the string in its primative character array form so you can directly interact with it's contents. Such as treating it like a regular array.

Let me know if any of that was unclear.
 
Raziella said:
Let me know if any of that was unclear.

That was unclear. lol :wink:

I remember taking a course in this stuff, and it looks kind of familiar, but I think you got way past where I got to lol
 
Raziella said:
It returns a pointer to the actual character array that represents the string.
(has he explained pointers?)

A string in it's most primative form is just a character array:
char s[128] = "foobar";

What string.h does is give you functions to work on strings easily. When you use string.h in conjunction with the std namespace, you can use C++ magic and declare a "string" type. So instead, you can say:
string s = "foobar";

But at that point you've wrapped the data in the "string" type and are not dealing with the primative character array anymore; so you need some way to get to it. That's where c_str() comes in. It returns the string in its primative character array form so you can directly interact with it's contents. Such as treating it like a regular array.

Let me know if any of that was unclear.

Yeah, he explained pointers, and I loaded the string library :p He never explained c_str ; ;

Thanks for the help ^^ It really helped xD I'll be sure to ask you again if I get stuck <.< You're far more knowledgable than I :biggrin:
 
He might have just forgot or expected you to do exactly what you did and search online for an answer.

Heh, you know what's funny? As much as I love C/C++, I ended up writing in Perl for a living lol.
 
Back
Top