Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Algorithm works as a standalone but not if used in function. C language

0 votes
asked Oct 30, 2021 by Kajetan (160 points)

So I created algorithm that ciphers a string for my classes. Its supposed to use Caesars cipher, and there is 2nd algorithm that decyphers. The algoritym looks like that

#include <stdio.h>

#include <string.h>

#define N 1001

#define k 4

int szyfrowanie(); //cipher function

int deszyfrowanie(); //decipher function

int main()

{

    printf("Chcesz szyfrowac(1) czy deszyfrowac(2)?"); //user can choose to cipher or decip

    int wybor; //"choice"

    scanf("%d", &wybor);

    if(wybor == 1)

        szyfrowanie();

    else

        deszyfrowanie();

        

    return 0;

}

szyfrowanie()

{

    char *tab[N];

    printf("Podaj ciag do zaszyfrowania: "); //user inputs string to cipher

    fgets(tab, sizeof(tab), stdin); //user input

printf("zaszyfrowany ciag znakow to: "); // "ciphered string is"

int i; //zmienna sterujaca

int dl = strlen(tab); //pobieramy dlugosc stringa wpisanego przez uzytkownika zeby nie zrobic za duzo iteracji :)

for (i = 0; i < dl; i++) //petla "zaszyfruje" nam nasz string

{

tab[i] = tab[i] + k; //sam proces szyfrowania

    }

    puts(tab); //wypisanie zaszyfrowanego stringa

    return 0;

    

}

deszyfrowanie() //decipher function

{

    char *tab[N];

    printf("Podaj ciag do zdeszyfrowania: "); //uzytkownik podaje zdanie do zdeszyfrowania

    fgets(tab, sizeof(tab), stdin); //wejscie uzytkownika

printf("zdeszyfrowany ciag znakow to: ");

int i; //zmienna sterujaca

int dl = strlen(tab); //pobieramy dlugosc stringa wpisanego przez uzytkownika zeby nie zrobic za duzo iteracji :)

for (i = 0; i < dl; i++) //petla "zdeszyfruje" nam nasz string

{

tab[i] = tab[i] - k; //sam proces deszyfrowania

    }

    puts(tab); //wypisanie zdeszyfrowanego stringa

    return 0;

    }

(I've made some comments for you to understand since code is in polish ^^)

So the problem is that if I compile this code, user can choose to cipher and decipher but then he cant input the string, the program acts like the string is just empty and finishes work with no output

If I just put a cipher algorithm into the main function it works like a charm. Whats The problem? 

1 Answer

+1 vote
answered Oct 31, 2021 by Peter Minarik (84,720 points)
selected Nov 1, 2021 by Kajetan
 
Best answer

Problems

There are multiple problems with your code. Let's see them one by one.

1. Mixed scanf() And fgets() Functions.

You cannot mix these functions as they handle end of line differently and calling fgets() after scanf() reads the new line character left by scanf() effectively returning empty.

One of the easiest solutions is to always use fgets() and if you need a number, use sscanf() to scan for that number from the buffer written by fgets().

2. Wrong Data Type

You use char*[] for your tab, while the correct type would be char[];

3. Wrong Return Type

Your cypher and decypher functions have a return type int, but they do not really return anything (always return 0). You should have a return type void instead and never call.

4. Use English :)

Last, but not least, it's better to use English identifiers so others can read your code too. (I'm not a native English speaker either.)

The Fixed Code

#include <stdio.h>
#include <string.h>

#define N 1001
#define k 4

void szyfrowanie(); //cipher function
void deszyfrowanie(); //decipher function

int main()
{
    printf("Chcesz szyfrowac(1) czy deszyfrowac(2)?"); //user can choose to cipher or decip
    int wybor; //"choice"
    char buffer[255];
    fgets(buffer, sizeof(buffer), stdin);
    sscanf(buffer, "%d", &wybor);
    if (wybor == 1)
        szyfrowanie();
    else
        deszyfrowanie();
    return 0;
}

void szyfrowanie()
{
    char tab[N];
    printf("Podaj ciag do zaszyfrowania: "); //user inputs string to cipher
    fgets(tab, sizeof(tab), stdin); //user input
    printf("zaszyfrowany ciag znakow to: "); // "ciphered string is"
    int i; //zmienna sterujaca
    int dl = strlen(tab); //pobieramy dlugosc stringa wpisanego przez uzytkownika zeby nie zrobic za duzo iteracji :)
    for (i = 0; i < dl; i++) //petla "zaszyfruje" nam nasz string
    {
        tab[i] = tab[i] + k; //sam proces szyfrowania
    }
    puts(tab); //wypisanie zaszyfrowanego stringa
}

void deszyfrowanie() //decipher function
{
    char tab[N];
    printf("Podaj ciag do zdeszyfrowania: "); //uzytkownik podaje zdanie do zdeszyfrowania
    fgets(tab, sizeof(tab), stdin); //wejscie uzytkownika
    printf("zdeszyfrowany ciag znakow to: ");
    int i; //zmienna sterujaca
    int dl = strlen(tab); //pobieramy dlugosc stringa wpisanego przez uzytkownika zeby nie zrobic za duzo iteracji :)
    for (i = 0; i < dl; i++) //petla "zdeszyfruje" nam nasz string
    {
        tab[i] = tab[i] - k; //sam proces deszyfrowania
    }
    puts(tab); //wypisanie zdeszyfrowanego stringa
}
commented Nov 1, 2021 by Kajetan (160 points)
I would love to write all in english but this is homework hah. I dont have problem with english. My teacher does :(
Thank You for Your answer!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...