Need ASAP help with a function (program)!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alenik1989
    New Member
    • Oct 2007
    • 64

    Need ASAP help with a function (program)!!!

    I wrote this code, but I'm not able to test it because im getting 3 errors. So I'm not sure that this code will work properly or not!!!

    the code in
    [CODE=c]
    #include <stdio.h>
    #include <stdlib.h>
    int rnd(char s1[],char s2[],char c[]);
    int GetSt(char s1[],char s2[],char c[]);
    void SearchAndPrint (char s1[], char s2[], char c[], int length);

    int main ()
    {
    char s1[41];
    char s2[21];
    char c[2];
    int answer,m;
    while(answer !='n')
    {
    m=rnd(s1,s2,c);
    if (m==-1)
    {
    printf("ERROOOR RR\n");
    printf("wanna cont press enter or n to exit\n");
    answer=getchar( );
    continue;
    }
    else
    {
    printf("wanna cont press enter or n to exit\n");
    answer=getchar( );
    }
    return 0;
    }





    int rnd(char s1[],char s2[],char c[])
    {
    int i,n;
    for(i=0;i<41;i+ +)
    s1[i]=rand() %26 + 'A';
    s1[i]='\0';

    n=GetSt(s1,s2,c );
    if(n==-1)
    return -1;
    else
    return 1;
    }


    int GetSt(char s1[],char s2[],char c[])
    {
    int n,i,length,m;
    printf("enter a char\n");
    c[0]=getchar();
    if((n=getchar() )!='\n')
    {
    printf("ERROOOR RR\n");
    while(n!='\n')
    {
    n=getchar();
    }
    return -1;
    }
    c[1]='\0';

    printf("enter char\n");
    i=0;
    while(i<21)
    {
    if( (m=getchar()) !='\n')
    {
    if('A'<=m && m<='Z')
    {
    s2[i]=m;
    i++;
    }
    else if ('a'<=m && m<='z')
    {
    m=m-32;
    s2[i]=m;
    i++;
    }
    else
    {
    while(m!='\n')
    m=getchar();
    break;
    }

    }
    else
    {
    s2[i]='\0';
    break;
    }
    }
    length=i;
    if(i==21)
    {
    printf("error 2\n");
    while(m!='\n')
    m=getchar();
    return -1;
    }
    else if(i<2)
    {
    printf("error 3\n");
    return -1;
    }
    else
    {
    SearchAndPrint( s1,s2,c,length) ;
    return 1;
    }
    }


    void SearchAndPrint (char s1[], char s2[], char c[],int length)
    {
    int i,j;
    char cha;

    for (i=0;i<length;i ++)
    {
    cha=s2[i];
    for(j=0;j<41;j+ +)
    {
    if(s2[j]==cha)
    {
    s2[j]=c[0];
    continue;
    }
    else
    {
    continue;
    }
    }
    }
    puts(s1);
    puts(s2);
    puts(c);
    }
    [/CODE]
    And im getting 3 errors like:
    [CODE=txt]
    :\Temp\lab213.c (36) : error C2143: syntax error : missing ';' before 'type'
    I:\Temp\lab213. c(39) : error C2065: 'i' : undeclared identifier
    I:\Temp\lab213. c(43) : error C2065: 'n' : undeclared identifier
    [/CODE]
    Note: All the above errors are about the function
    int rnd(char s1[],char s2[],char c[])
    Please help!!!!!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You're missing a } to close main().

    rand() returns a double. Use (char) to cast to char so it goes into your array.

    Comment

    • Alenik1989
      New Member
      • Oct 2007
      • 64

      #3
      Tanx. That helped alot.

      Comment

      Working...