0

This is my code for Ceasar (pset2) of cs50.

Whenever I try to make the program, I get a

control may reach end of non-void function

Could you help me with this?

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

int main(int argc, string argv [])
{
  if (argc == 2 && isalnum(*argv[1]))
   {
      string t = argv [1];
      int k = atoi ( t );
      string s = get_string ("plaintext: ");
      printf ("ciphertext:" );
      for (int i = 0, n = strlen(s); i < n; i++)
      {
         char c = s[i];
         if (isalpha (c))
         {
            if (isupper(c))
            {
               int x = ((int) c - 65 + k) % 26 + 65;
               printf ("%c", (char) x);
            }
            else
            {
               int x = ((int) c - 97 + k) % 26 + 97;
               printf ("%c", (char) x);
            }
         }
         else
         {
            printf ("%c", c);
         }
      }

      printf ("\n");
      return 0;
   }
   else
   {
       printf("Usage: ./caesar key \n");
       return 1;
   };
}

bool is_numerical (string e)
{
   for (int i = 0, n = strlen(e); i < n; i++)
   {
      if (isdigit (e[i]))
      {
         return true;
      }
      else
      {
         return false;
      }
   }
}

Thank you so much

1

In your is_numerical function, the compiler sees that there is a theoretical path where no return statement would be encountered, so it generates that error.

Specifically, if the for loop were to execute (say n=0), then the code would never encounter a return statement while executing. There would need to be a return statement following the loop to fix this.

Having said that, the function is still broken. It will only execute one pass through the loop. When the first char is checked, it will either return true or will return false. Either way, it will never check the second char.

| improve this answer | |
  • Thank you very much for your feedback. I have managed to overcome that hurdle. However, now that I have rectified it, it compiles, but I now get a seg fault... Could you help me with that? (I have edited the question accordingly...) – Hamster1303 Oct 18 at 17:42
  • Ummm...no. Please restore the original question so that the answer matches it. If you have a new problem, please open a new question. Changing the existing question confuses people that may read it at a later date and don't know that the question was changed after being answered. – Cliff B Oct 19 at 10:08
  • Ok, I understand what you're saying. I will revert back to the previous question. Also, I have managed to solve the problem. I couldn't thank you enough for your help. Really appreciate it! – Hamster1303 Oct 19 at 12:27
  • I have edited the question so that it now shows the previous one. – Hamster1303 Oct 19 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .