0

my text is something like this

55<space>The User Input Sentence;

i got the 55 out via the code below and assigned that to a int variable via atoi

std::string stringKey = userInput.substr(0, 2);

but how to get the rest of the string and assign it to a string variable ????the code below

this is the complete code (haven't finished it yet)

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>

using namespace std;
//FUnction Prototypes
string encrypt(string, int);
string decrypt(string source, int key);
int main(int argc, char *argv[])
{
    string source;
    string userInput;
    int key;
    int choice;
    cout << "To encode a message type 1, to decode a message type 2: ";
    cin >> choice;
    if (choice == 1)
    {
        cout << "Enter the message to encode: ";
        cin >> userInput;
        std::string stringKey = userInput.substr(0, 2);
        key = atoi(stringKey.c_str());
        std::string source = userInput.substr(3);
        encrypt(source, key);

    }


        /*decrypt(source, key);

    cout << "Source: ";
    getline(cin, source);

    cout << "Key: ";
    cin >> key;

    cout << "Encrypted: " << decrypt(source, key) << endl;*/
    system("pause");
}

string encrypt(string source, int key)
{
    string Crypted = source;

    for (int Current = 0; Current < source.length(); Current++)
        Crypted[Current] = ((Crypted[Current] + key) - 32) % 95 + 32;
        return Crypted;
}

string decrypt(string source, int key)
{
    string Crypted = source;

    for (int Current = 0; Current < source.length(); Current++)
        Crypted[Current] = ((Crypted[Current] - key) - 32 + 3 * 95) % 95 + 32;
    return Crypted;
}
high 0n C
  • 57
  • 1
  • 11

1 Answers1

0

Same way as you got first part. Check out std::string substr

string second = userInput.substr(3);

----------------EDIT------------------

If you give 55<space>The User Input Sentence as input, only first string will be read. Use getline method to read entire input including spaces

GoldRoger
  • 1,263
  • 7
  • 10
  • yeah i did that and the program gives me this error Unhandled exception at 0x769DC41F in ASCII Cipher.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0019F638. – high 0n C Apr 11 '14 at 19:04
  • This probably means `userInput` has less number of characters – GoldRoger Apr 11 '14 at 19:07
  • the user input is supposed to be the key space and the encrypting or decrypting message i cannot predict the length of the user message. in this case what should i do ?? – high 0n C Apr 11 '14 at 19:11
  • You just need to know the length of key. Pass `(length+1)` to second `substr` call. This is the offset where your message starts – GoldRoger Apr 11 '14 at 19:13
  • I meant read the entire line with spaces, so you can split them later.Edited my reply – GoldRoger Apr 11 '14 at 19:19
  • like this getline (cin, userInput); – high 0n C Apr 11 '14 at 19:22
  • same error i guess Unhandled exception at 0x769DC41F in ASCII Cipher.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0029F854. – high 0n C Apr 11 '14 at 19:24
  • funny it keeps on giving me memory errors im using visual studio 2013 maybe thats the reason – high 0n C Apr 11 '14 at 19:40
  • try printing `userInput` after you read. Check if its same as you entered – GoldRoger Apr 11 '14 at 19:42
  • it just bold this std::string source = userInput.substr(3); and gives the memory error message. – high 0n C Apr 11 '14 at 19:47
  • yep its the same as i typed – high 0n C Apr 11 '14 at 19:53
  • i entered 55 Hello World. – high 0n C Apr 11 '14 at 20:35
  • and also if i use get line the c++ wont even let me type anything it will give me the memory error, but if i use the cin command i can atleast type the user input – high 0n C Apr 11 '14 at 20:41
  • if i just use two cin statements for key and the text my code works perfectly it will encode and decode without a problem – high 0n C Apr 11 '14 at 20:44
  • Once you enter choice, it consumes only `int` not the enter you pressed. Add `cin.ignore();` inside the `if` block. See [here](http://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer) – GoldRoger Apr 11 '14 at 20:50