I'm trying to pass a character as parameter into a function. I will then get the user's input( characters and numbers etc), the function then will return all the input characters excluding everything else. I've gotten the sorting the characters from everything else figured out, but returning the array back to the main function is my main concern.

first attempt: pointers!
problem: having trouble storing the values from the address of the pointer into another array.

second attempt: ...... not sure what to do next.

can you post the function code here?

can you post the function code here?

sure, here it is

#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;

const int SIZE = 61;

void getinput(char *[]);
void compare(char [], char []);

int main()
{
	char *sentence1[SIZE], *sentence2[SIZE];

	getinput(sentence1);
	getinput(sentence2);
	return 0;
}

void getinput(char *sentence[])
{
	char ch;
	int i = 0;
	cout << "enter something => ";
	cin.get(ch);
	int count = 0;
	char sentence_temp[SIZE];
	while(ch >= ' ' || count <= SIZE)
	{
                
		if (isalpha(ch))
		{
			sentence_temp[i++] = ch;
		}
                cin.get(ch);
		count++;
	}
	int k = 0;
	while (k <= count)
	{
		*sentence[k] = sentence_temp[k];
		k++;
	}
	
}

void compare(char sentence1[], char sentence2[])
{
	

}

sorry if it is a mess.

while(ch >= ' ' || count <= SIZE)
	{
                
		if (isalpha(ch))
		{
			sentence_temp[i++] = ch;
		}
                cin.get(ch);
		count++;
	}

does this ever exit?

while(ch >= ' ' || count <= SIZE)
	{
                
		if (isalpha(ch))
		{
			sentence_temp[i++] = ch;
		}
                cin.get(ch);
		count++;
	}

does this ever exit?

yes, when the user hit the enter key, the loop will exit. or when the counter hits SIZE(61).

When posting c++ code, please use c++ code tags
[code=c++] // Your code here

[/code]

And actually in my testing, that doesn't exit like you want it to.

It keeps looping if the character is >= ' ' OR if the count is <= SIZE . It will not stop until both conditions are false. (PS- you want < SIZE)

If you change the "||" to "&&" you get the effect you were looking for, it will only loop as long as both conditions are satisfied.

In the testing I changed the sentence declarations to: char sentence1[SIZE]; And the function declaration to: void getinput(char *sentence) The calls to getinput(sentence1); and the update inside the function to sentence[k] = sentence_temp[k]; You're not terminating your output strings with a '\0', and you don't necessarily have count characters in sentence_temp.

To do it simply you can declare sentence1 as a simple char array rather than a 2d char array.

i modified your code to illustrate this you can put your logic back again whatever it is. i havn't compiled it so cant guarantee against that. Any reason why you were using a 2d char array?

#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;

const int SIZE = 61;

void getinput(char []);
void compare(char [], char []);

int main()
{
	char sentence1[SIZE];

	getinput(sentence1);
	cout << sentence1 << endl;
	//getinput(sentence2);
	return 0;
}

void getinput(char sentence[])
{
	char sentence_temp[4] = {'a','b','c','d'};
	int k = 0;
	while (k <= 3)
	{
		sentence[k] = sentence_temp[k];
		k++;
	}
	sentence[k] = '\0';

}

void compare(char sentence1[], char sentence2[])
{


}

:) its the same point as Murtan's. I didn't refresh i guess.

ok, i have another set of codes written without the pointer also. but my main concern is returning the array back to the main function so it can be passed again through another function(compare). I'm trying to write a program that detects an anagram. It is the reason why I used pointer so that the values stored isn't lost.

declaration like

char *p[10];

declares a 2-d char array which holds values like

char p[0] = "yoyo";
char p[1] = "pogo";

you can use a 1-D char array for your assignment. change the contents of the array by passing its base address(like i have done in the sample code) and then pass the same to another function and it should work.

so I was declaring a 2-d array? Maybe I'm not understanding something. I thought 2d array was declared as

array[something][something]

yes like that too but you can declare a 2d array as char *p[] also.

To do it simply you can declare sentence1 as a simple char array rather than a 2d char array.

i modified your code to illustrate this you can put your logic back again whatever it is. i havn't compiled it so cant guarantee against that. Any reason why you were using a 2d char array?

#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;

const int SIZE = 61;

void getinput(char []);
void compare(char [], char []);

int main()
{
	char sentence1[SIZE];

	getinput(sentence1);
	cout << sentence1 << endl;
	//getinput(sentence2);
	return 0;
}

void getinput(char sentence[])
{
	char sentence_temp[4] = {'a','b','c','d'};
	int k = 0;
	while (k <= 3)
	{
		sentence[k] = sentence_temp[k];
		k++;
	}
	sentence[k] = '\0';

}

void compare(char sentence1[], char sentence2[])
{


}

:) its the same point as Murtan's. I didn't refresh i guess.

wow, that was strange. so without a pointer, you can pass an array into a function argument and the values won't be lost once it is returned back to the main function?

yes like that too but you can declare a 2d array as char *p[] also.

isn't *p[] declaring a pointer pointing to an array?

it is, so when you increment it you don't go to the next element but to the next 'array' !!

char *p[4] = {"yo","po","to","go"};

	for(int i=0;i<4;i++)
	{
		cout << *(p+i) << endl;

	}

output:
yo
po
to
go

p[0] = "yo"
p[1] = "po"
etc...

you guys are awesome!!! I got my program working, but with some logic errors, thank you guys soo much! I will be back with more questions if any should come up.


-Homer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.