-1

I don't know what is wrong with my code. It doesn't run as expected. I just want to encrypt the characters in my cstring using the conditions in the if statements, and output all the results each time the key is iterated (key from 1 to 100), but even the iteration doesn't work properly. It starts from 9. I get the following warnings during compilation, too:

[Warning] multi-character character constant [-Wmultichar] 
[Warning] overflow in implicit constant conversion [-Woverflow]

Example of output (as you see it starts iteration from 9 not 1 and then goes up to 100):

Encrypted using key (9) : n
Encrypted using key (10) : o
Encrypted using key (11) : p
Encrypted using key (12) : q
Encrypted using key (13) : r
Encrypted using key (14) : s
Encrypted using key (15) : t
Encrypted using key (16) : u
Encrypted using key (17) : v
Encrypted using key (18) : w
Encrypted using key (19) : x
Encrypted using key (20) : y
Encrypted using key (21) : z
Encrypted using key (22) : {
Encrypted using key (23) : |
Encrypted using key (24) : }
Encrypted using key (25) : ~
Encrypted using key (26) :
Encrypted using key (27) : !

Then Code:

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

string myfunction(char a[]);


int main()
{   
    //char a[] = ":mmZ\dxZmx]Zpgy";
    //char a[] = {':','m','m','Z',92,'d','x','Z','m','x',']','Z','p','g','y','\0'};
    //char a[] = {58,109,109,90,92,100,120,90,109,120,93,90,112,103,121,0};
    char a[] = {'e','o','i','/0'};
    cout << myfunction(a) << endl;

    return 1;
    system("pause");
}

string myfunction(char a[])
{   
    string encrypted;

    for (int i=0; i<strlen(a)-1; i++){
        cout << " Taking first character for encryption : " << a[i] << endl;
        for (int key =1; key<=100; key++)
        {
            if (a[i]+key > 126){
                encrypted= (a[i]+key)-95;
                cout << "Encrypted using key (" <<key<<") : " << encrypted << endl;}
            else{
                encrypted= a[i]+key;
                cout <<  "Encrypted using key (" <<key<<") : " << encrypted << endl;}
        }
    }

    return ("\nGood Job!");
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Alfie brown
  • 87
  • 2
  • 2
  • 9
  • 5
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Oct 12 '15 at 19:34
  • Thank you @NathanOliver very useful tool you are right. I will learn about it. – Alfie brown Oct 12 '15 at 19:35
  • 1
    If you expect help with the algorithm, then you should show example inputs, the outputs and which outputs you expected. Please [edit] your question accordingly. – Artjom B. Oct 12 '15 at 19:37
  • Alfie, please click "edit" below your question and add the information there. Thank you! – CodeMouse92 Oct 12 '15 at 19:43
  • @Alfiebrown There gonna be one or multiple characters in encrypted string for each character in the input? Please edit your question. – Osmani Oct 12 '15 at 19:46
  • 2
    You should not even try to run your program until you properly address the warnings. – n. m. could be an AI Oct 12 '15 at 19:48
  • @JasonMc92 if you type `[edit]` in your comment it becomes and [edit] link to the attached post. – NathanOliver Oct 12 '15 at 19:49
  • alright sorry guys, I just edited my question. – Alfie brown Oct 12 '15 at 19:52
  • Bonza, thanks for the info @NathanOliver. – CodeMouse92 Oct 12 '15 at 19:52
  • 2
    *"My main question is why my for loops do not work as expected?"* You haven't clearly shown what you expect. Please show *some* actual output and show an additional block of expected output. – Artjom B. Oct 12 '15 at 19:56
  • @ArtjomB. Just edited it thank you. – Alfie brown Oct 12 '15 at 20:00

1 Answers1

1
char a[] = {'e','o','i','/0'};   

you mean

char a[] = {'e','o','i','\0'};

that probably fixes your warnings. I have no idea if it fixes your problem tho - since you dont ask a clear question.

pm100
  • 48,078
  • 23
  • 82
  • 145