Thread: a string program question

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    a string program question

    hi guys , this is a program to read string S1 ,go to index k, and delete the
    char located in index k. but its giving me an error that declaration terminated incorrectly could anyone help me plz ? here is the code.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
     void delete(char x[],int size,int k)
     {
     int i;
     for(i=k;i<=strlen(x);i++)
     x[i]=x[i+1];
     strcpy(&x[k],&x[k+1]);
     }
    
     main()
     {
     char S1[10];
     int k;
    
     scanf("%s%d",S1,&k);
     delete(S1,10,k);
    
     getch();
     }
    Last edited by everyone0; 11-17-2010 at 02:44 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Missing a ;.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    but thats not a prototype thats the function itself

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should combine anduril462's observation with information provided by the error message, including the line number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    what does (6,8) declaration terminated incorrectly mean ?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Does your compiler (conio.h suggests Borland Turbo C, I think) not give a line number anywhere? Maybe it's time to get a new compiler. My compiler (gcc, FTW!) tells me:
    main.c: In function ‘delete’:
    main.c:11: error: expected ‘;’ before ‘}’ token

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    yep its borland mm , i did add a ; before the } but still having same problem

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm...

    Why do you need strcpy() in there when you're moving the string character by character?

    Or, conversely, why do you need the loop if you're doing it block style with strcpy.

    Also... it should be int main(void) and main should return an int (usually 0).
    Last edited by CommonTater; 11-17-2010 at 03:11 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The following 2 links suggest that your compiler is confused as to whether you're using C or C++. Not having used a Borland product in a decade or more, I can't tell you where to check or change that, but maybe you can figure it out.

    CintTalk: RE:[CINT] Borland C++ Builder or OCX versio
    Declaration Terminated Incorrectly Error - C And C++ | Dream.In.Code

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    tried to figure it out , this is killing me can't find a single problem with the function and still gives me same error

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by everyone0
    this is killing me can't find a single problem with the function and still gives me same error
    I suggest that you try:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void delete(char x[], int size, int k)
    {
        int i;
        for (i = k; i <= strlen(x); i++)
            x[i] = x[i + 1];
        strcpy(&x[k], &x[k + 1]);
    }
    
    int main(void)
    {
        char S1[10];
        int k;
    
        scanf("%s%d", S1, &k);
        delete(S1, 10, k);
        return 0;
    }
    If you still have a compiler error, then try:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void remove(char x[], int size, int k)
    {
        int i;
        for (i = k; i <= strlen(x); i++)
            x[i] = x[i + 1];
        strcpy(&x[k], &x[k + 1]);
    }
    
    int main(void)
    {
        char S1[10];
        int k;
    
        scanf("%s%d", S1, &k);
        remove(S1, 10, k);
        return 0;
    }
    If the error disappears, then I would guess that anduril462's suspicion is confirmed: you are compiling as C++ instead of C. If the error remains, then you either need to figure out how to fix your compiler, or you ditch your compiler.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Given the time stamps on the posts, I don't think you tried all that hard. You spent a maximum of 3 minutes, but if you take out time for reading my post, the two links I sent you and replying to the post, I'd give you 2 minutes. Not much time to look at every setting in Borland, check all the Borland help stuff and look for Borland specific forums that might cover how to play around with your settings, then actually try changing different settings and recompile.

    Now that the upbraiding is done, there is not much wrong with your C code. I compiled it in gcc with -Wall -pedantic and got the follwing errors:
    main.c:13: warning: return type defaults to ‘int’
    main.c: In function ‘main’:
    main.c:21: warning: control reaches end of non-void function

    Declare main as
    Code:
    int main(void)
    and make sure main returns a value (probably 0).
    Last edited by anduril462; 11-17-2010 at 03:31 PM.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    actually yea im working on several programs at the same time so i check once and a while , the second suggestion worked laserlight , thanks , i guess i won't have to ditch my compiler lol.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by everyone0
    the second suggestion worked laserlight , thanks , i guess i won't have to ditch my compiler lol.
    Yes, but you need to configure it to compile your C code as C, not C++. Otherwise, you either switch to C++, or you end up using a C compiler that rejects valid C programs, thus frustrating you if you don't know enough C++ to write C programs that are also valid C++ programs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Note that 'remove' is also defined in stdio.h but I guess that's part of the joke, isn't it?
    gcc -pedantic -Os -c "eek.c" -o "eek".o -std=c99
    eek.c:4:6: error: conflicting types for ‘remove’

    C Links (no affiliation)
    C Draft Standards
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
    http://rosettacode.org/wiki/Category:Programming_Tasks
    http://www.iso-9899.info/wiki/Main_Page
    http://clc-wiki.net/

    My C Stuff (GPL):
    PaperDragon
    TML Version 2
    Fix missing brackets with Anchor
    The Nerd Show | Platypus
    Last edited by hellork; 11-17-2010 at 03:59 PM. Reason: not affiliated with cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM