Solved

algorythm problems..

Posted on 1997-04-29
3
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ vs C compilers 13 169
Resolve Dependency Issues 4 98
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 102
Windows Server 2012 R2 - connect to computer 13 99
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

738 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