my program

This is a discussion on my program within the C++ Programming forums, part of the General Programming Boards category; what is wrong with this code. the problem is flagged as /*here*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #define stringsize1 ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    my program

    what is wrong with this code. the problem is flagged as /*here*/

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define stringsize1 256 /*length of original*/
    #define stringsize2 80 /*length of second string*/
    #define numsize 10


    void insert(char original[], char sub[],int pos); /*function*/

    main()
    {

    char str1[stringsize1], str2[stringsize2],num[numsize];
    int position;
    int i;


    for (i=0;i<5;i++)
    {
    printf("\nPlease enter a character string:\n");
    gets(str1);

    printf("\nPlease enter the inserting character string:\n");
    gets(str2);

    printf("\nPlease enter the inserting position:\n");
    gets(num);

    /*HERE*/position=pos(num);
    insert(str1,str2,position);

    printf("\nThe new string is:\n");
    printf("%s\n",str1);
    }

    return EXIT_SUCCESS;
    }
    /*The inserting function body*/

    void insert(char origin[], char sub[],int pos)

    {
    int i, j; /*i=original string,j=substring*/

    int firstlength;

    int seclength;

    char temp[stringsize1]; /*buffer*/

    firstlength=strlen( origin);

    seclength=strlen( sub);


    /*copy the original str to position in buffer*/

    for (i=0;i<pos;++i)
    temp[i]=origin[i];

    /*insert substr to buffer at desired location*/

    for (j=0;j<seclength;j++,i++)
    temp[i]=sub[j];

    /*remainder of original string copied to buffer*/

    /*here*/ for (pos<firstlength;pos++;i++)
    temp[i]=origin[pos];


    /*end the string*/
    temp[i]='\0';


    /*complete string copied back to the original location*/

    strcpy(origin,temp);
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    pos is undefined. that is why it errors.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Hmzz

    Well I think the for loop is wrong

    for (pos<firstlength;pos++;i++)
    temp[i]=origin[pos];

    its suppoed to be, for(variable initailisation, condition, incretement/decretement )

    you put a conditional for first

    try

    for(i = 0; i < firstlenght; i++)
    {
    pos++
    temp[i]=origin[pos];
    }

    ok bye!

  4. #4
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    From the Visual C++ 6.0 Help file

    The for statement can be divided into three separate parts, as shown in Table 5.3.

    Table 5.3 for Loop Elements

    Syntax Name When Executed Contents
    for-init-statement Before any other element of the for statement or the substatement. Often used to initialize loop indices. It can contain expressions or declarations.
    expression1 Before execution of a given iteration of the loop, including the first iteration. An expression that evaluates to an integral type or a class type that has an unambiguous conversion to an integral type.
    expression2 At the end of each iteration of the loop; expression1 is tested after expression2 is evaluated. Normally used to increment loop indices.


    The for-init-statement is commonly used to declare and initialize loop-index variables. The expression1 is often used to test for loop-termination criteria. The expression2 is commonly used to increment loop indices.

    The for statement executes the statement repeatedly until expression1 evaluates to zero. The for-init-statement, expression1, and expression2 fields are all optional.

    The following for loop:

    for( for-init-statement; expression1; expression2 )
    {
    // Statements
    }

    is equivalent to the following while loop:

    for-init-statement;
    while( expression1 )
    {
    // Statements
    expression2;
    }
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  5. #5
    Unregistered
    Guest
    drop num[numsize] in this line:
    char str1[stringsize1], str2[stringsize2],num[numsize];

    change this:
    gets(num);

    to this:
    scanf(&position);

    or better yet, in my opinion, use streams rather than C style functions for input.

    drop this line:
    position=pos(num);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21