Solved

algorythm problems..

Posted on 1997-04-29
3
265 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 100 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 165
Using unmanaged DLL from managed application 14 294
How to get time_t value that is a week old 6 168
C++ vs C compilers 13 159
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

810 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