Thread: getchar from keyboard couldn't be end with the termination condition

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Question getchar from keyboard couldn't be end with the termination condition

    There is a program of simple encryption and decryption ,it pass the compile and build,but stop at the running process.No whether how I input character thought keyboard,it couldn't end with the termination condition,let alone could have any result.any friend can give me a look?thanks!
    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define MaxSize 100
    typedef struct
    {char ch[MaxSize];
    int len;
    }SqString;
    
    SqString A,B;
    void StrAssign(SqString &str,char cstr[])
    {int i;
    for(i=0;cstr[i]!='\0';i++)
    str.ch[i]=cstr[i];
    str.len=i;
    }
    
    
    void DispStr(SqString s)
    {int i;
    if(s.len>0)
    {for(i=0;i<s.len;i++)
     printf("%c",s.ch[i]);
    printf("\n");}}
    
    
    SqString EnCrypt(SqString p)     /*algorithm of  encrypt with simple character*/
                                                        /*exchange */ 
    {int i=0,j;
    SqString q;
    while(i<p.len)
    {for(j=0;p.ch[i]!=A.ch[j];j++);
    if(j>=A.len)
    q.ch[i]=p.ch[i];
    else
    q.ch[i]=B.ch[j];
    i++;
    }
    q.len=p.len;
    return q;
    }
    
    
    SqString UnEncrypt(SqString q)        /*reverse of encrypt */
    {int i=0,j;
    SqString p;
    while(i<q.len)
    {for(j=0;q.ch[i]!=B.ch[j];j++);
    if(j>=B.len)
    p.ch[i]=q.ch[i];
    else p.ch[i]=A.ch[j];
    i++;
    }
    p.len=q.len;
    return p;
    }
    
    
    
    int main()
    {SqString p,q;i
    nt i=0,j,k=0,t=0;
    char ch;
    char s1[MaxSize],s2[MaxSize],s[MaxSize];
    
    do{k1:ch=getchar();        /*initialize s1 without  the same character*/
    for(j=0;j<i;j++)
    if(ch==s1[j]) goto k1;
    s1[i++]=ch;
    }while(s1[i-1]!='\0');
    
    printf("\n");
    
    while(k<i-1)                    /*initialize s2 without  the same character,and the */
                                    /*length of array s2 should be as the same as  array s1*/ 
    {k2:ch=getchar();
    for(j=0;j<k;j++)
    if(s2[j]==ch) goto k2;
    s2[k++]=ch;}
    s2[k]='\0';
    
    printf("\n");
    
    do{k3:ch=getchar();                            /* initialize s*/
     for(j=0;s1[j]!='\0'&&s1[j]!=ch;j++);
     if(j>=i-1) 
     {for(j=0;s2[j]!='\0';j++)
     if(ch==s2[j]) goto k3;}
     s[k++]=ch;
     }while(s[k-1]!='\0');
    
    printf("\n");
    
    
    	 
    StrAssign(A,s1);
     StrAssign(B,s2);
    
     StrAssign(p,s);
     
     printf("加密解密如下:\n");
     printf("原文串:");
     DispStr(p);
    
    q=EnCrypt(p);
     printf("加密串:");
     DispStr(q);
    
     p=UnEncrypt(q);
     printf("解密串:");
     DispStr(p);
    
     printf("\n");
     return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Learn to indent. Drop the goto abuse. If you're expecting getchar to produce a '\0', that certainly seems like a very weird expectation.

    I don't plan on looking at the rest of this until it resembles real code, and even then I doubt I'll be looking at it.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, it's hard to want to take you seriously if you cannot indent like everyone else.

    I think you have gotten ahead of yourself with this and need to focus in on specific tasks so you can ask specific questions about a specific problem rather than expecting someone else to take time and extract the relevant information. There is a good chance you will solve the problem yourself that way, which is always nice.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    getchar() returns EOF, not '\0' when the end of file is reached. On Linux, use Ctrl-D to add EOF to the input stream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  3. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM