Thread: i get illegal operation.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    7

    i get illegal operation.

    im new in C++

    this is only a sample code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
        char *a;
        char *b;
        char *c;
        char *d;
        ifstream ab ("word.txt");
        ifstream bb ("wordlist.txt");
        ab>>c; //line 11    b=c;
        cout<<b;
        bb>>d; //line 14
        a=d;
        cout<<b<<a;
        cin.get();
    }
    note: the text with red font is the coz of "illegal operation" but if i change it to a same variable i dont recieve illegal operation but whenever line 14 is executed both variable b and a change to the value from the document.

    this is the problem if i use same variable to line 11 and 14:
    i change the variable c to d from line 11.

    ab>>d;
    b=d; meaning if string from the word.txt is aaaa, b = "aaaa"
    when line 14 is executed
    ab>>d;
    a=d; if string from wordlist.txt is bbbb supposed to be a= "bbbb"
    but b is also become b= "bbbb"

    any help!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your pointers a, b, c and d are pointing at "random" locations, you need to allocate some memory, or otherwise point at some valid memory location.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Just use std::string instead of raw char pointers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    i am new in C++ and dont know how to allocate memory can you give me a sample? thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just use std::string instead.

    Here's a quick introduction:

    http://www.cprogramming.com/tutorial/string.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. illegal operation error
    By xlordt in forum C Programming
    Replies: 2
    Last Post: 09-01-2003, 07:53 AM
  4. Illegal Operation
    By Bert in forum C++ Programming
    Replies: 15
    Last Post: 07-22-2002, 10:29 AM
  5. Illegal operation
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 04:33 PM