i get illegal operation.

This is a discussion on i get illegal operation. within the C++ Programming forums, part of the General Programming Boards category; im new in C++ this is only a sample code: Code: #include <iostream> #include <fstream> using namespace std; int main ...

  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,893
    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,319
    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, 07: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, 02: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, 03:33 PM

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