Thread: help plz crashing program

  1. #1
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    43

    Unhappy help plz crashing program

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    
      char* passarray[10];
      cout<<"This program can store 5 passwords of yours.\n";
      for(int i=0;i<5;i++)
      {
      cout<<"Please enter the passwords nickname so you can get it later:";
      cin>>passarray[i];
      cout<<"Please enter the password for the nickname "<<passarray[i]<<":";
      }
      system("pause");
      return 0;
    }

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    You did not allocate memory for every pointer you use.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    
      char* passarray[10];
      cout<<"This program can store 5 passwords of yours.\n";
      for(int i=0;i<5;i++)
      {
      cout<<"Please enter the passwords nickname so you can get it later:";
      passarray[i] = new char[101]; // assume max length is 100 chars 
      cin>>passarray[i];
      cout<<"Please enter the password for the nickname "<<passarray[i]<<":";
      }
      system("pause");
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by quagsire
    You did not allocate memory for every pointer you use.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    
      char* passarray[10];
      cout<<"This program can store 5 passwords of yours.\n";
      for(int i=0;i<5;i++)
      {
      cout<<"Please enter the passwords nickname so you can get it later:";
      passarray[i] = new char[101]; // assume max length is 100 chars 
      cin>>passarray[i];
      cout<<"Please enter the password for the nickname "<<passarray[i]<<":";
      }
      system("pause");
      return 0;
    }
    Make sure you delete all the memory you allocate also.
    Code:
    for( int i = 0; i < 5; i++ )
    {
      delete [] passarray[i];
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    43
    i dont want that to be a pointer i just want it to be an array of words and NOT A STRING like a regular array of int but of words and i onnly did the * siblye becuase if i didnt if would say the frist letter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing, Need help!
    By Obsidian_179 in forum C Programming
    Replies: 3
    Last Post: 06-19-2008, 05:26 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM