2

I am trying to run encryption program of some sort but i can't call void enc by switch function, Source Code:

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <algorithm>
#include <string>

using namespace std;

int x,y,z,a,b,c,n;
char tabs[26][26],temp[512];
string input,output,Key;

void open();
void tableau();
void inchar();
void enc();
void dec();

int main() {
  open();
  cout << "1.\tEncrypt \n2.\tDecrypt \nOption: "; cin >> a;
  switch (a) {
    case 1:
      enc(); cout << a << "Debugger";
      break;
    case 2:
      dec();
    break;
  }
  return 0;
}

void enc(){
  void open();
  void inchar();
}

void dec(){

}

void inchar(){
  cout << "input: "; cin >> input; z = input.size();
  char dispos[input.size() + 1];
  copy(input.begin(),input.end(),dispos); dispos[input.size()] = '\0';
  for (int i = 0; i < z; i++) {
    temp[i] = dispos [i];
  }
}

void tableau() {
  cout << "Initialize Table Construct!!" << endl;
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < 26; j++) {
      x = (i + j) % 26; y = x + 65;
      tabs[i][j] = y;
      cout << tabs[i][j] << " ";
    }
    cout << endl;
  }
}

void open() {
  cout << "Well Hello There";
}

every time i choose option 1 the debugger cout kept on appearing. if the debugger is erased then the code just end.

P.S: I've done moving the function call to before the switch but still it does nothing. P.S.S: Sorry for bad English.

KotjengOren
  • 79
  • 1
  • 7
  • 5
    You do call `enc`, but `enc` doesn't do anything. `void open(); void inchar();` in `enc` are function declarations, not calls; drop the `void`. – HolyBlackCat Sep 25 '19 at 15:13
  • @HolyBlackCat I was posting my answer while you were posting your comment! – Adrian Mole Sep 25 '19 at 15:16
  • 1
    Unrelated: Don't use [VLA](https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers):s (like `char dispos[input.size() + 1];`) if you want to be portable. – Ted Lyngmo Sep 25 '19 at 15:23
  • There's really too much code here. You should reduce this for purposes of asking a good question. Also, you can take the debugging one step further by putting the `cout<<"Debugger"` *inside* of `enc()` -- through which you would see that your premise is slightly flawed. – Brent Bradburn Sep 25 '19 at 15:26
  • _"I've done moving the function call to before the switch but still it does nothing."_ - That was the sign for you that the `switch` statement probably had nothing to do with it and could be removed from the example code (bringing you closer to a [mcve]). Repeating that process would eventually lead you to find out that calling `enc` itself already does nothing. For next time! – Max Langhof Sep 25 '19 at 15:41

1 Answers1

4

The problem is that your enc() function is being called, but it doesn't do anything! The syntax is wrong:

void enc(){
  void open();   // These lines DECLARE two NEW function ...
  void inchar(); // ... without ever calling them!
}

To call the two functions, use this:

void enc(void) {
    open();
    inchar();
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • om*g i literally banging head for an hour for overlooked such a small mistake. thank you very much!!! – KotjengOren Sep 25 '19 at 15:18
  • 1
    @KotjengOren -- the `switch` has nothing to do with the problem. Remove the entire `switch` statement and replace it with `enc();` and you'll see the same thing. The problem is in the implementation of `enc()`, and this answer gives you the solution. – Pete Becker Sep 25 '19 at 15:19
  • @KotjengOren The `switch` *statement* (it's not a function) is fine! You don't need to change your code for `main`. Just change the definition of `enc()` as I have suggested, and try it out! – Adrian Mole Sep 25 '19 at 15:20