retreiving variable from within loop.

This is a discussion on retreiving variable from within loop. within the C Programming forums, part of the General Programming Boards category; There is probably a fairly simple explenation to this, but how do you a variable to maintain a value it ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    retreiving variable from within loop.

    There is probably a fairly simple explenation to this, but how do you a variable to maintain a value it gained in a for loop, specifically a character array? I alter the "value" of the array in the loop and I want that value to carry on thorugh out the rest of the program program. Any help is appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Can you provide a pseudocode example? I have no idea what you're asking.
    My best code is written with the delete key.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This might be a question of scope? Just declare the value outside the loop?

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    ok, yeah i should have inclluded an example:
    Code:
    char a[1000];
    int x = o;
    for(x = 0; X<100; x++){
         a = do some stuff/calculations;
    }
    printf("%s\n", a);
    //and then do other stuff with a
    //a contains no data outside of the loop, why?
    when I do something like this, a contains no data as if it were never inisialized.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    If you are only assigning the value once then there should be no problem. If you want to assign multiple values and reference them at a later time then you need to use a character array of pointers and say the values to that.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    I'm not sure what you're actually experiencing. If you give a value inside the loop it stays outside the loop, unless the value you're setting is declared inside the loop because of scope.
    Code:
    itsme@itsme:~/C$ cat alpha2.c
    #include <stdio.h>
    
    int main(void)
    {
      char a[100];
      int i;
    
      for(i = 0;i < 99;++i)
        a[i] = (i % 26) + 'a';
      a[i] = '\0';
    
      puts(a);
      printf("a[2] = %c\n", a[2]);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./alpha2
    abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu
    a[2] = c
    itsme@itsme:~/C$
    Last edited by itsme86; 08-04-2006 at 02:04 PM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Alright, how to explain this. I declaire the arrays value outside of the loop and then alter that value inside the loop. What I get inside the loop is the value I want to continue throughout the rest of the program. I think I found a work around but further knowledge would be helpful.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Is it anything like the code I posted? Why do I bother showing example programs?
    If you understand what you're doing, you're not learning anything.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    //a contains no data outside of the loop, why?
    Does a contain any data inside the loop?

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    I cant get your code to compile, it says there is an unnessisary @.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    >>Does a contain any data inside the loop?
    Yes. Prior to the loop I assign input to the array. I then alter that input in the array. If I print out the array in the loop it works and prints out the info. If I try to print outside the loop (after it is terminated, it outputs nothing.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by lilrayray
    I cant get your code to compile, it says there is an unnessisary @.
    Don't include "itsme@itsme:~/C$ cat alpha2.c" in the source code, silly. That's a *NIX command prompt. "cat alpha2.c" just spits the file out to the screen for me to copy and paste.
    If you understand what you're doing, you're not learning anything.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes. Prior to the loop I assign input to the array. I then alter that input in the array. If I print out the array in the loop it works and prints out the info. If I try to print outside the loop (after it is terminated, it outputs nothing.
    Then may-be you could post some code. The pseudocode you posted doesn't show that.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    here:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void encrypt();
    
    int main(int argc, char *argv[])
    {
      encrypt();
      getchar();
      return 0;
    }
    
    void encrypt()
    {
         //
         //declaring variables and file initilization
         //
         FILE *fp;
         char file[150000];
         char encrypt[150000];
         char decrypt[150000];
         int count = 0;
         int a = 0;
         int x = 0;
         char key[16] = "aeiluozcgraolifi";
         //
         //file is opened here and checked for errors
         //
         fp = fopen("c:\\test.txt", "r");
         if(fp == NULL)
         {
               puts("No such file or directory.");
         }
         else{
              fread(file, 1000, 150, fp);
         }
          //
          //checks for total number of characters in document
          //
          for(a = 0; file[a] != '\0'; a++)
          {
            ++count;
          }
          
          for(x = 0; x < count; x++){
                encrypt[x] = file[x] ^ key[x];
                }
                printf("%s", encrypt);  
         
    }

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    I'm not even sure where to begin on this, but I'll give it a shot:
    Code:
    fread(file, 1000, 150, fp);
    Where did you come up with 1000 and 150? Doing 1 and sizeof(file) would make a lot more sense. You're also using way more stack space than you need to. You could read the file in in chunks until you reach EOF. encrypt and decrypt could be dynamically allocated so it only takes up as much space as it really needs to.


    Code:
         if(fp == NULL)
         {
               puts("No such file or directory.");
         }
         else{
              fread(file, 1000, 150, fp);
         }
    So...if the file can't be opened you print an error message and don't try to read the file (which is good), but then you go on to to process file as though something had been read into it (which is bad). For this program, it would probably be best to just print the error message and exit the program if the file can't be opened.

    Code:
    //checks for total number of characters in document
    If you use my above suggestion for fread(), fread() will return the number of characters in the file! If you use the tools at hand correctly, things tend to be a lot easier.

    Code:
          for(x = 0; x < count; x++){
                encrypt[x] = file[x] ^ key[x];
                }
                printf("%s", encrypt);
    Are you sure encrypt[x] is being set to a printable character? If not, you're going to have an awfully hard time printing it that way.
    Last edited by itsme86; 08-04-2006 at 04:19 PM.
    If you understand what you're doing, you're not learning anything.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Loop variable
    By anirban in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 10:36 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. for loop not letting me input variable data
    By RealityFusion in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 04:29 PM

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