sprintf(me,"%s","HELP") // HELP ME

This is a discussion on sprintf(me,"%s","HELP") // HELP ME within the C++ Programming forums, part of the General Programming Boards category; My array (sOutput) is going out of scope after the for loop. Could someone tell me how i could use ...

  1. #1
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281

    sprintf(me,"%s","HELP") // HELP ME

    My array (sOutput) is going out of scope after the for loop. Could someone tell me how i could use sprintf() or similar to solve this? I was using "sprintf(sOutput[whatever],"%c",sInput[i]) but for some reason it didn't work.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      char sInput[1024];
      char sOutput[1024];
      //memset(reinterpret_cast<void *>(&sOutput), '\0', sizeof(sOutput));
      for (int p=0;p<1024;p++)sOutput[p]='\0';
      int iLength = 0;
      int i = 0, x = 0, y = 0, z = 0;
    
      cout << "What message do you want to scramble?\n";
      cin.get(sInput, 1024);
      //cin.ignore(1024, '\n');
    
      iLength = strlen(sInput);
      int half = iLength/2;
      //strrev(sInput);
    
      sOutput[iLength]='\0';
      for (i=0, x=0; (i<iLength) && (sInput[x] != '\0'); i++, x=i/2){
        if (i%2==0){
          sOutput[half+x]=sInput[i];
          cout << "i = " << i << "; x = " << x << "; len = " << iLength << "   EVEN: " << half << "+" << x << endl;  // DEBUG
          cout << "sOutput[" << half+x << "] = " << sOutput[half+x] << "; " << sOutput << endl; //  DEBUG
        } else {
          sOutput[x]=sInput[i];
          cout << "i = " << i << "; x = " << x << "; len = " << iLength << "   ODD: " <<  x << endl; // DEBUG
          cout << "sOutput[" << x << "] = " << sOutput[x] << "; " << sOutput << endl;  //  DEBUG
        }
        sprintf(sOutput,"%s",sOutput);
      }
      cout << sOutput[x] << endl;
      for (i=0, x=0; (i<iLength) && (sInput[x] != '\0'); i++, x++){
        if (i%2){
          //sUnscrambled[i] = sScrambled[iLength/2+x];
        } else {
          //sUnscrambled[i] = sScrambled[x];
        }
      }
      if (getche());
      return 0;
    }
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    just ONE comment... that was originally my code but you can use it since I still need help. I never thought of doing it that way.

    thx

  3. #3
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    I am not "using it", as you say, i am trying to figure out how i can get it to work right!

    //EDIT: As both an excercise for me and possibly helpful for you.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  4. #4
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    I just solved it!

    I replaced
    Code:
    cout << sOutput[x] << endl;
    with
    Code:
    cout << sOutput << endl;
    I feel so silly...

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

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