1
  1. This project is giving me so much grief...the first issue that I'm having is that my menu forces me to input twice and then stops when I pick an option...
  2. I really am having a hard time getting program to take user input and store it in my array so that I can pass it to my functions to be either decrypted or encrypted.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 500
    
    int getUserChoice();
    int getShift();
    void getString(char buf[]);
    void encrypt(char buf[], int shift);
    void decrypt(char buf[], int shift);
    
    int main()
    {
        char inputBuf[SIZE] = {0};
    
        while ( getUserChoice() != 4)
        {
    
            switch (getUserChoice())
            {
                case 1:
                    getShift();
                    break;
    
                case 2:
                    getString(inputBuf[SIZE]);
                    encrypt(inputBuf[SIZE], getShift());
    
            }
    
        }
    
        return 0;
    }
    
    int getUserChoice()
    {
        int userChoice = 0;
    
        printf("-------------------------------\n"
                     "| 1) Change Shift (default 3) |\n"
                     "| 2) Encrypt a message        |\n"
                     "| 3) Decrypt a message        |\n"
                     "| 4) Quit                     |\n"
                     "-------------------------------\n");
    
        printf("Option: ");
        scanf("%d", &userChoice);
    
    
        return userChoice;
    }
    
    int getShift()
    {
        int shift = 3;
    
        do
        {
            printf("Enter new shift value: ");
            scanf("\n%d", &shift);
        } while(shift < 1 || shift > 10);
    
        return shift;
    }
    
    void getString(char inputBuf[])
    {
        printf("Input: ");
        fgets(inputBuf, SIZE, stdin);
    }
    
    void encrypt(char inputBuf[], int shift)
    {
        int i = 0;
    
        for (i = 0; i < inputBuf[i] && inputBuf[i] != '\0'; i++)
        {
            inputBuf[i] = inputBuf[i] + shift;
    
            if (inputBuf[i] = ' ')
            {
                printf("%c", ' ');
            }
            else
            {
                printf("%c", inputBuf[i]);
            }
        }
    }
    
    void decrypt(char buf[], int shift)
    {
    
    }
    
patoglu
  • 401
  • 4
  • 16
Greg2dl101
  • 19
  • 1
  • Please focus on a single problem in your question and make an [mre] to demonstrate it. Including sample input, expect output and actual output. – Yunnosch Jan 20 '20 at 05:56
  • "my menu forces me to input twice" -- the code calls `getUserChoice()` twice.... – ad absurdum Jan 20 '20 at 05:57
  • Have a look at these very helpful articles (even if they might seem cynical ) https://ericlippert.com/2014/03/21/find-a-simpler-problem/ https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Jan 20 '20 at 05:58

1 Answers1

1

Problem 1:

The first issue that I'm having is that my menu forces me to input twice

It is because you call getUserChoice twice:

First in the while header

Next in the switch header within the body of the while loop

Solution:

Define a variable like int choice = getUserChoice(); outside your while loop and use this variable for the while and switch statements instead of calling the function again and again.

Problem 2:

I really am having a hard time getting program to take user input and store it in my array so that I can pass it to my functions to be either decrypted or encrypted.

The problem lies here:

case 2:

getString(inputBuf[SIZE]);

encrypt(inputBuf[SIZE], getShift());

This is not how you pass arrays to functions.

Solution:

Replace inputBuf[SIZE] with inputBuf

Check this post to learn more about how to pass arrays to functions in C.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53