1

I wrote program which use menu to execute function.

#include <iostream>
#include<conio.h>
#include <stdio.h>
#include <cstring>

using namespace std;

int menu();
void zad1();
void zad2();


int main(){
    switch(menu()){
        case 1: zad1();break;
        case 2: zad1();break;

        default: cout<<"blank";
    };
    getch();
    return 0;
}

int menu(){
    cout<<"Menu\n\n";
    cout<<"Zad.1\nZad.2\n";
    int wybor;
    cin>>wybor;
    return wybor;
}

int encryption(){
    char string[256];
    int i = 0;
    int key = 3;
    const int num_letters = 26;

    printf("Enter the string you want encrypted\n");
    fgets(string, sizeof(string), stdin);

    for (i = 0; i < strlen(string); i++) {
        if (string[i] >= 'a' && string[i] <= 'z') {
            string[i] = 'a' + (string[i] - 'a' + key) % num_letters;
        }
        else if (string[i] >= 'A' && string[i] <= 'Z') {
            string[i] = 'A' + (string[i] - 'A' + key) % num_letters;
        }
    }
    printf("Encrypted: %s\n", string);
    return 0;
}

void zad1(){
    encryption();
}

The only thing is I my output is not working properly - it shows the menu, I can type number (1-2) to choose function to execute and all I get from function "encryption" is:

Enter the string you want encrypted
Encrypted:

and that's all - I can't type character. What am I missing? I used the menu before and everything worked properly.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I think you just have some bug in your `int encryption()` function. You probably should obtain a debugger. Set a breakpiont on your fgets() and step through the code line by line after that to see what happens to string. – drescherjm Oct 21 '22 at 16:21
  • @drescherjm int encryption() function is working fine when I use only it, not through the menu. – tryingToDevelopMyself Oct 21 '22 at 16:23
  • Interesting fun fact. C++ has a type called `std::string`. When you employ `using`, for example with `using namespace std`, `std::string` can be referred to as `string`. This can lead you into trouble if you name something else `string`. – user4581301 Oct 21 '22 at 16:25
  • Don't mix C and C++ style input and output. Use either `cout` and `cin` or `printf` and `scanf` – NathanOliver Oct 21 '22 at 16:25
  • 1
    I am not familiar with fgets() as that is a `c` input function and I don't write `c` code or use `c` io in my `c++` code however maybe the problem is the same as this: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – drescherjm Oct 21 '22 at 16:26
  • Didn't get you this time, but never ignore the return code in an IO transaction. If the user screws up the input accidentally or deliberately, not checking for and handing error conditions turns those error conditions into bugs you need to find. – user4581301 Oct 21 '22 at 16:28
  • IMHO, You should have `menu` as a separate statement. Example: `const int selection = menu(); switch(selection)` – Thomas Matthews Oct 21 '22 at 16:48
  • You may want to change the variable name of `string` in the `encryption` function. In C++, there already is `std::string`, and there may be conflicts if you do `using namespace std;`. – Thomas Matthews Oct 21 '22 at 16:54

1 Answers1

2

After this input

cin>>wybor;

the input buffer contains the new line character '\n'.

So the next call of fgets

printf("Enter the string you want encrypted\n");
fgets(string, sizeof(string), stdin);

reads an empty string that contains only this new line character.

You need to clear the buffer before calling the function as for example

while ( getchar() != '\n' );
printf("Enter the string you want encrypted\n");
fgets(string, sizeof(string), stdin);

Or instead of the C function fgets you could use the C++ member function ignore for the object std::cin together with the function getline. . Also it seems here is a typo

 case 2: zad1();break;

You should write

 case 2: zad2();break;

Pay attention to that in fact your program is a C program. You are not using C++ features except the operator << for the output stream in the function menu. Using C stream functions and C++ stream functions together is a bad idea.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335