Char Arrays

This is a discussion on Char Arrays within the C++ Programming forums, part of the General Programming Boards category; I have been working with a program and am almost finished. I am at the last few parts which of ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    10

    Char Arrays

    I have been working with a program and am almost finished. I am at the last few parts which of course are going to give me the most trouble.

    In this part of the code, the user enters a name in which to edit. The name is located in a file. The program is supposed to search for the name, and then prompt the user to enter what the name should be changed to. But before it gets to that point, I get an illegal operation. That nice little message box that termates programs. I have been working on it for 2 days now, commenting and testing, but I am unable to find out specifically what line it is because it seems to move. Here is the source code for the functions:

    Code:
    void editfile()
     {
      system("cls");
      printf("Enter the name of the contact you wish to edit: ");
      cin.ignore(800, '\n');
      cin.getline(search, 256);
      strcat(fcontact, search);
      
      system("cls");
      printf(" *****************************************************************");
      printf("\n|          What do you want to do with this contact?              |");
      printf("\n|          1. Edit the contact's name                             |");
      printf("\n|          2. Edit the contact's phone number                     |");
      printf("\n|          3. Delete the contact                                  |");
      printf("\n *****************************************************************");
      printf("\nSelection: ");
      cin >> input;
      switch(input)
      {
       case 1: changename();
               break;
        
       case 2: changenumber();
               break;
       
       case 3: deletecontact();
               break;
       
       default: fileoptions();
                break;
      }         
     }     
     
     void changename()
     {
      fin.open(profilename); 
      while(!fin.eof())
      {
       i=0;
       fin.getline(szEdit[i], 256);
       i++;
       if (stricmp(fcontact, szEdit[i]) == 0)
       {
        fin.close();
        j = i;
        system("cls");
        printf("Contact found!");
        Sleep(1500);
        system("cls");
        printf("Enter the new name: ");
        
        // Does not seem to make it to this point.
        
        cin >> name;
        strcat(econtact, name);
        strcpy(name, econtact);
        strcpy(szEdit[j], name);
        fin.close();
        fin.open(profilename);
        while(!fin.eof()) 
        {
         i = 0;
         fin.getline(szEdit[i], 256);
         i++; 
        }
        k = i;
        strcpy(szEdit[j], name);
        fin.close();
        fout.open(profilename);
        while(i<=k)
        {
         i = 0;
         fout << szEdit[i];
         i++;
        }
        system("cls");
        printf("Editing complete!");
        fout.close();
        Sleep(1500);   
       }
      }  
     }
    I made a little comment for the part the program cannot seem to get to. Thanks for all your help!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What are the following declared as :

    >search
    >fcontact
    >input

    >profilename
    >szEdit
    >name
    >econtact

    and where are they declared? They're not all global are they?

    >fin.open(profilename);
    You must check that the open worked.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    Right, let me post all my variables, and yes, they are all global.

    Code:
    char search[256]
    char fcontact[256] = "Contact: ";
    int input;
    
    char profilename[100];
    char *szEdit[10000];
    char name[256];
    char econtact[256] = "Contact: ";
    I made sure that if it failed it returned to the main menu, but it still pulled out the illegal error instead of returning to the main menu so it must not have failed.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    char *szEdit[10000];


    Here are 10000 pointers that point to no memory...try something like:

    for(i = 0; i < 10000; i++)
    szEdit[i] = new char[256];


    (and don't forget to free the memory when you're done...)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    Ok I did that but the error still exists. I did some tests and found that the line that it stopped at was the if statement:

    Code:
    if (stricmp(fcontact, szEdit[i]) == 0)
    I am not sure if that is the actual problem, that is the line that the error appears at.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, this doesn't look right to me:
    Code:
       i=0;
       fin.getline(szEdit[i], 256);
       i++;
       if (stricmp(fcontact, szEdit[i]) == 0)
    Why are you incrementing i here? In doing so, you'll goes past the end of the array on the stricmp() line. Also, where's you safe guard to ensure you don't anyway? What if there's too many lines?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    I am incrementing "i" so that each time the loop repeats it will place the next line into the next slot of the array. What I am trying to do is make it so a person can edit certain information in a file, the way I am performing this is by bringing in all the lines, edit the one line that they want, and then put it back with the changed information. That is why I am comparing the strings. If you know a better way to do this I am all ears. Thanks!

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, this doesn't look right to me:

    code:--------------------------------------------------------------------------------
    i=0;
    fin.getline(szEdit[i], 256);
    i++;
    if (stricmp(fcontact, szEdit[i]) == 0)
    --------------------------------------------------------------------------------

    Look closely at these lines. Don't you see that the increment was made too soon?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Don't you see that the increment was made too soon?
    My point exactly...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    Oh ok, I see now, Ill try that and see if that works, Thanks!

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    I made the changes but still it gives the error, unless again I am missing something. Here is what it looks like with all the changes:

    Code:
    void changename()
     {
      fin.open(profilename); 
      if(fout.fail())
      {
       printf("Error, Returning to main menu");
       menu();
      }
      while(!fin.eof())
      {
       system("cls");
       i=0;
       for(i = 0; i < 500; i++)
       {
        szEdit[i] = new char[256];
       } 
       fin.getline(buffer, 256);
       strcpy(szEdit[i], buffer);
       if (stricmp(fcontact, szEdit[i]) == 0)
       {
        fin.close();
        j = i;
        system("cls");
        printf("Contact found!");
        Sleep(1500);
        system("cls");
        printf("Enter the new name: ");
        
        cin >> name;
        strcat(econtact, name);
        strcpy(name, econtact);
        strcpy(szEdit[j], name);
        fin.close();
        fin.open(profilename);
        if(fout.fail())
        {
         printf("Error, Returning to main menu");
         menu();
        }
        while(!fin.eof()) 
        {
         i = 0;
         fin.getline(szEdit[i], 256);
         i++; 
        }
        k = i;
        strcpy(szEdit[j], name);
        fin.close();
        fout.open(profilename);
        if(fout.fail())
        {
         printf("Error, Returning to main menu");
         menu();
        }
        while(i<=k)
        {
         i = 0;
         fout << szEdit[i];
         i++;
        }
        //system("cls");
        printf("Editing complete!");
        fout.close();
        Sleep(1500);   
       }
       else
       {
        i++;
       }
      }
     }

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
       i=0;
       for(i = 0; i < 500; i++)
       {
        szEdit[i] = new char[256];
       } 
       fin.getline(buffer, 256);
       strcpy(szEdit[i], buffer);
    What value is i when used in the strcpy() line? Do you think it might cause a problem?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    Heh, thanks for pointing that out, but that still does not fix the illegal error problem. Could it have something to do with my compiler? I am using the newest Bloodshed Dev C++. And I just tried the older one. And both failed.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> Could it have something to do with my compiler?
    No, it's your code!

    Why not break it down into smaller sections, just to try and see where you're going wrong.

    Try creating some code that will simply read the file into the variables you want, then loop through those variables, printing them. This will prove that you can read input correctly.

    Then, extend this to print only one record, based on the input string (contact name or whatever). Use the code you already created for doing the read and loop, just add a compare statement to print only the desired line.

    Then, work on the output side.

    This might seem the long way round, but it'd probably be worth it for you. I've had a closer look at your code, and can see a number of logic/flow problems that you'd most likely avoid if you worked in chunks.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    Ok, I did just that and I found out what is probably the problem. This is all the code I used and the illegal error still came up.

    Code:
    void changename()
     {
      
      i = 0;
      fin.open(profilename); 
      if(fout.fail())
      {
       printf("Error, Returning to main menu");
       menu();
      }
      while(!fin.eof())
      {
       system("cls");
       for(i = 0; i < 500; i++)
       {
        szEdit[i] = new char[256];
       } 
       fin.getline(buffer, 256);
       strcpy(szEdit[i], buffer);
       fin.close();
      }
     }
    All this code does is get information, so I assume it has something to do with szEdit[i] but am unsure what. So if you could help me find out, id be grateful. Thanks!

    Opps, forgot to post the variables.

    Code:
    char *szEdit[500];
    char buffer[256];
    int i;
    Last edited by Borommakot; 09-27-2002 at 05:56 PM.

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

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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