Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

algorythm problems..

Posted on 1997-04-29
3
Medium Priority
?
294 Views
Last Modified: 2010-05-18
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...
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>



main()
{
      static char ar[30];
      static char pr[30];
      static char br[30];
      static int vote;
      int x, y, z;
      int sw, b, a, c;
      strcpy(br,"elow!13%*$#!1Hllwo!");
      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);
      {
      
    if(vote==1)
            sw=0;
      fflush (stdin);

      if(sw==0)
            {
            printf("\nEnter the phrase you wish to have encoded...\n");
            gets(ar);
            a = strlen(ar);
            sw=1;

            printf("\npassword?\n");
            gets(pr);
            b = strlen(pr);
            y=0;
            }
      {

            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*/
                        y=0;}
                  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*/
      }

                  
            }
      

}
0
Comment
Question by:iamdwe
  • 2
3 Comments
 

Author Comment

by:iamdwe
ID: 1250171
Edited text of question
0
 
LVL 1

Accepted Solution

by:
prc earned 200 total points
ID: 1250172
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,

Paul
0
 

Author Comment

by:iamdwe
ID: 1250173
I didn't expect you to give me code, the answer was good enough!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question