-1

I am a beginner of C, so I might not understand all the terms. The is a program meant to convert some plain text into ciphered text through vigenere. It's the thrid problem here :http://cdn.cs50.net/2016/x/psets/2/pset2/pset2.html

However, I keep having the warning "control may reach end of non-void function" from the CS50 IDE. Thanks for helping out!

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

string k = "";
int testalpha ();
int main(int argc, string argv[])
{
    k = argv[1];
    testalpha ();
   if (argc !=2 || testalpha(1))
    {
        printf("Please retry.");
        return 1;
    }
    string p= GetString();
    for (int i=0;i<strlen(p); i++)
            {   int c [50];
                int a=0;
                if (isupper(p[i]))
                {   
                    c [i] =  (((int)p[i] + k[a] -65) % 26)+65;
                    printf ("%c", c[i]);
                    a+=1;
                    if (a==strlen(k))
                    {a=0;}
                    continue;
                }
                if (islower(p[i]))
                {
                 c [i] =  (((int)p[i] + k[a]-97) % 26)+97;
                    printf ("%c", c[i]);
                    if (a==strlen(k))
                    {a=0;}
                    continue;
                }
                if (isalpha(p[i])==0)
                {
                    c [i] = p[i];
                    printf ("%c", c[i]);
                }


            }
            printf("\n");
}

int testalpha (void)
{
    for (int x=0; x<strlen(k); x++)
        {
            if(isalpha(k[x])==0)
            {   
            return 1;
            }   
            else 
            {
            return 0;
            }
        }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Zack Light
  • 167
  • 2
  • 11

1 Answers1

2

Look carefully at the function int testalpha (void).

It doesn't return a value on all control paths: for example if strlen(k) is zero.

That's undefined behaviour in C: aside from main, if a function has a return type other than void, then you must return a value.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483