Thread: i have a pointer that it aint working. plz help.

  1. #1
    adventurer
    Guest

    Angry i have a pointer that it aint working. plz help.

    wut is the problem with the pointer???. the compiler doesnt find an error but when i run it it gives me an illegal operation and i have to close it. any ideas??


    Code:
    void newperson ()
    {
    	int count, numppl;
    	char *strp;
    	*strp = input[20];
    	cout<<"How many people do you want to introduce?"<<endl;
    	cin>>numppl;
    	for (count=0 ; count <=numppl ; count=count +1)
    	{
    		cin.get (input, 20);
    	}
    }
    thx

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    39

    scope of input

    if input is global then the code should work but if it is not the case then u should pass it to the function or declare it in the function.it also depends upon the data types of the pointer and input.they should be same!
    once a kohatian,always a kohatian!

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    *strp = input[20];
    You don't know on what address this is getting assigned.
    Your program might overwrite anything radomly. Lucky for you your OS is stoping this from happening.

    int i;
    int *p;
    p = &i; //You must do this. p is pointing to the address of i.
    Last edited by Barjor; 05-01-2002 at 02:57 PM.

  4. #4
    adventurer
    Guest

    i have an unknown error

    i did wut u told me to barjor. i have now:
    Code:
    char *strp;
    .
    .
    .
    void newperson ()
    {
    	int count, numppl;
    	strp = agendaarray;
    	cout<<"How many people do you want to introduce?"<<endl;
    	cin>>numppl;
    	for (count=0 ; count <=numppl ; count=count +1)
    	{
    		cin.get (agendaarray, 20);
    
    	}
    }
    //I changed the input array for agendaarray.
    the error i get is ::

    --------------------Configuration: Cpp1 - Win32 Debug--------------------
    Linking...
    Cpp1.obj : error LNK2001: unresolved external symbol "char * agendaarray" (?agendaarray@@3PADA)
    Debug/Cpp1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Cpp1.exe - 2 error(s), 0 warning(s)

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Put the varaibles inside the function

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    your original code was fine except for one minor thing. you must first assign strp to input. like this:

    Code:
    void newperson()
    {
    	int count, numppl;
    	char *strp=input;
    	*strp = input[20];
    	cout<<"How many people do you want to introduce?"<<endl;
    	cin>>numppl;
    	for (count=0 ; count <=numppl ; count=count+1)
    	{
    		cin.get (input, 20);
    	}
    }
    just add the '=input' after 'char *strp'

  7. #7
    adventurer
    Guest

    one more thing and ill be good.

    now it runs fine. except for one major thing. the cin.get (input, 20); does not read anything. it skips that part and keeps going with my program.
    Code:
    void newperson()
    {
    	int count, numppl;
    	char *strp=input;
    	*strp = input[20];
    	cout<<"How many people do you want to introduce?"<<endl;
    	cin>>numppl;
    	for (count=0 ; count <=numppl ; count=count+1)
    	{
    		cin.get (input, 20);
    	}
    }
    I wonder if i havet to make the "void" into a char newperson (void) and place a return input[20];

    plz reply if u have an idea. thx

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Code:
    void newperson ()
    {
    	int count, numppl;
    	cout<<"How many people do you want to introduce?"<<endl;
    	cin>>numppl;
    	char *strp=new char [numppl+1];
                    for (count=0 ; count <=numppl ; count=count +1)
    	{
    		cin.get (input, 20);
    	}
    }

    And at the end of the program, write delete [] strp;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Pointer assignment not working
    By coderCPP1981 in forum C Programming
    Replies: 6
    Last Post: 06-20-2008, 06:59 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM