• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

algorythm problems..

I have had problems with my algorythm, and now I can't get the debugger to work on this on. can you tell me what my problem is with the code, and how to go about solving it?
here is the code...

      static char ar[30];
      static char pr[30];
      static char br[30];
      static int vote;
      int x, y, z;
      int sw, b, a, c;
      c = strlen(br);

      printf("Enter choice 1 or 2:\n");            
    printf("(1) To encrypt a phrase\n");
    printf("(2) To decrypt a phrase\n");
    scanf("%d", &vote);
      fflush (stdin);

            printf("\nEnter the phrase you wish to have encoded...\n");
            a = strlen(ar);

            b = strlen(pr);

            for (x=1; x < a; x++)            /* increments x, while less than a*/
                  printf("%s", ar[x + 24]);            /* print the value of (x+24)*/
            for (y=1; y < b; y++)            /* same y increment*/
                  if ( y > a)            /* if y is larger than the value of the password, then start over*/
                  printf("%s", pr[y + 24]);      /* print y+24*/
            for (z=1; z < c; z++)      /* same increment for z*/
                  printf("%s", br);      /* print the whole br array for values of z*/


  • 2
1 Solution
iamdweAuthor Commented:
Edited text of question
I think you're having severe problems with C syntax.  It looks to me that you're not quite sure where curly brackets are supposed to go, so let's fix that first:

An if statement should look like this

  if (..some condition..)
    .. a statement; ...
    .. another statement, maybe; ...

A 'for' looks similar.  It's best to put in the curly brackets even if there is only one statement to remind you to use them when you add another one - there are several places in your code where it looks like you've forgotten them.

To be honest I'm not quite sure what this code is supposed to do (you didn't specify), but I'm pretty suspicious of the line

printf("%s", pr[y+24]);

This shouldn't even compile with a clever compiler, because what it means is "Take the character at (y+24) in the array 'pr' and print it as a string.".  It's meaningless, I'm afraid.

If you want to print characters, you should use '%c', not '%s'.  I'm guessing, but I suspect you want "pr[y]+24", not "pr[y+24]".  You want to get the character first, then add 24 to it.

Other things - the use of 'sw' looks dodgy.  You set it to zero if 'vote' is 1, but what is it if 'vote' isn't 1?  It must be initialised to something before use.  I can't follow the rest of that bit, I'm afraid.

Oh yes, think hard about how many times for(y=1; y<b; y++) loops round, and whether you shouldn't be starting with 0 as an array index.

I could write a clean version for you, but I'm not going to, because fixing these problems is the only way you can learn - I had to, years ago.  Cruel but kind, I'm afraid.

Hope this helps,

iamdweAuthor Commented:
I didn't expect you to give me code, the answer was good enough!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now