-1

I am creating a program that enciphers text using the vigenere cipher. However, when ever I run the code, it gives me errors saying

vigenere.c:27:29: error: multi-character character constant [-Werror,-Wmultichar]
            int letternum = 'currentletter';
                            ^
vigenere.c:27:29: error: character constant too long for its type [-Werror]
vigenere.c:26:18: error: unused variable 'currentletter' [-Werror,-Wunused-variable]
            char currentletter = input[i];
                 ^
3 errors generated.

I am trying to get the ascii value of the i(th) letter of the user's input by converting the char to an int. My program code is below. Pls help.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

string input;
int digitnum = 0;

int main(int argc, string argv[])
{
    if (argc>2 || argc<2)
    {
        printf("Please enter a valid argument.\n");
        return 1;
    }  

    input = GetString();

    for (int i = 0; i < strlen(input); i++)
    {
        char c = input[i];

        if (isalpha(input[i]))
        {
            char currentletter = input[i];
            int letternum = 'currentletter';

            if(isupper(c))
            {
                int upper = 'A';
                int alphanum = letternum - upper;

                int newint = (alphanum + alphanum) % 26;

                newint = newint + upper;

                char newchar = newint;

                printf("%c", newchar);
            }

            if(islower(c))
            {
                int lower = 'a';
                int alphanum = letternum - lower;

                int newint = (alphanum + alphanum) % 26;

                newint = newint + lower;

                char newchar = newint;

                printf("%c", newchar);
            }

            digitnum = digitnum + 1;

            if (digitnum >= strlen(argv[1]))
            {
                digitnum = 0;
            }
        }
        else
        {
            printf("%c", input[i]);
        }
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Do you really need to post all that code? That is a rhetorical question. – juanchopanza Mar 20 '16 at 19:16
  • I am really not sure because I am new to programming and am not 100 percent sure where/what exactly is causing the problem. – Faiz Momin Mar 20 '16 at 19:18
  • It is basic problem solving. Remove everything until you're only left with problematic code. – juanchopanza Mar 20 '16 at 19:21
  • "However, when ever I run the code, it gives me errors saying ..." - There is no chance your code will output this when run. You mean the compiler give this errors when compiling! – too honest for this site Mar 20 '16 at 19:21
  • 1
    I think you mean `int letternum = currentletter;` (without the single quotes). – Paul Ogilvie Mar 20 '16 at 19:22
  • Sorry my bad, i meant that whenever i compiled the code it would give the error. BTW, i have found the solution to the problem. I had to remove the single quotes for it to work. Instead of int letternum = 'currentletter'; i was supposed to have int letternum = currentletter; – Faiz Momin Mar 20 '16 at 19:23
  • `if (argc>2 || argc<2)` is usually written as `if (argc != 2)` – pmg Mar 20 '16 at 21:29

2 Answers2

1
char currentletter = input[i];
int letternum = 'currentletter';

'currentletter', as the warning says, is a multi-character constant. It is of type int and its value is implementation-defined. It has nothing to do with the variable currentletter you defined on the line above.

Multi-character constants are a very nearly useless language feature. They cannot be used in portable code. They're legal, so a conforming compiler won't reject them (even if they would have a value that's out of range), but apparently you're invoking the compiler with an option that causes it to reject them (which is actually a pretty good idea).

My best guess is you should have:

char currentletter = input[i];
int letternum = currentletter;

That would be legal, but I don't know why you'd need to copy the value of currentletter into an int object. char is already an integer type.

I'd have to study the rest of your code to determine how you should fix the problem, but this answers your question about the error message.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

The problem is you are trying to convert a string to a character here.

int letternum = 'currentletter';

But you have to convert the character to ASCII value. To do so, just remove the quotes. Then it will convert the variable - currentletter. If you include the '', it tries to read it as a character and hence give you an error !

ThisaruG
  • 3,222
  • 7
  • 38
  • 60