gerard4143 371 Nearly a Posting Maven

Instead of starting at zero and incrementing try starting at columns value and decrementing to zero..

gerard4143 371 Nearly a Posting Maven

Try

SocketSpace::TCPSocket* SocketSpace::ServerSocket::accept()
gerard4143 371 Nearly a Posting Maven

Because a returning function will invalidate all its variables, you can't return the address to any of them and expect consistent results...Here's one way to solve your problem using the heap.

#include <stdio.h>
#include <stdlib.h>

#define ARR_SIZE 4

int* foo(void);

int main()
{
	int i = 0;
	int *ans = foo();

	for (; i < ARR_SIZE; ++i)
		fprintf(stdout, "ans->%d\n", ans[i]);

	free(ans);
	return 0;
}

int* foo(void)
{
	int i = 0;
	int *ar = (int*)malloc(ARR_SIZE * sizeof(int));
	
	for (; i < ARR_SIZE; ++i)
		ar[i] = i + 10;

	return ar;
}
gerard4143 371 Nearly a Posting Maven

You have to create your array on the heap in this situation...Returning an address to an array on the stack won't work because the address is invalid as soon as the function returns..

gerard4143 371 Nearly a Posting Maven

I would investigate the string library's function strcat() and functions like sprintf();

gerard4143 371 Nearly a Posting Maven

You strcat version and "Stuff" into about...What's 'about'? Can we see its definition?

gerard4143 371 Nearly a Posting Maven

Line 68, your setting myfile to NULL. It should be myfile == NULL

I'm surprised your compiler didn't complain about that line. I corrected it and the program works now.

gerard4143 371 Nearly a Posting Maven

The most common way is to overload the operator>>

gerard4143 371 Nearly a Posting Maven

std::cerr is an I/O channel that can be used for errors...Its almost the same as std::cout except its not buffered.

Why two asterisks? Its a pointer to a pointer...so two asterisks.

What's this line?

farray = new float*[rows * sizeof(float*)];

That is how you allocate the memory for three float pointers.

This one?

farray = new float[cols * sizeof(float)];

Here I'm allocating the arrays of floats for each of the three pointers.

and I do use ifstream.

gerard4143 371 Nearly a Posting Maven

I tried your problem but used pointers to pointers for the array...

testfile

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

testit.cpp

#include <iostream>
#include <fstream>

int main()
{
	int rows = 0;
	int cols = 0;
	int count = 0;
	int row_count = 0;

	float **farray = (float**)NULL;

	std::ifstream		fin("testfile");

	if (fin.fail())
	{
		std::cerr << "Could not open file" << std::endl;
		return 1;
	}

	fin >> rows >> cols;

	farray = new float*[rows * sizeof(float*)];

	for (int i = 0; i < rows; ++i)
		farray[i] = new float[cols * sizeof(float)];

	float value = 0.0;

	while (fin >> value)	
	{
		farray[row_count][count++] = value;
		if (count >= cols)
		{
			count = 0;
			++row_count;	
		}		
	}

	fin.close();

	for (int i = 0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			std::cout << farray[i][j] << " ";
		}
		std::endl(std::cout);
	}
	return 0;
}

The code is pretty rough but you can clean it up and see if it works for you..

gerard4143 371 Nearly a Posting Maven

I think you'll find this a better way to read a file of floats..

testfile

3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

testit.cpp

#include <iostream>
#include <fstream>

int main()
{
	std::ifstream		fin("testfile");
	if (fin.fail())
	{
		std::cerr << "Could not open file" << std::endl;
		return 1;
	}

	float value = 0.0;

	while (fin >> value)	
	{
		std::cout << value << std::endl;
	}

	fin.close();
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Shouldn't this

3 5
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

be

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

gerard4143 371 Nearly a Posting Maven

You have major problems in your code...I tried compiling it and set off many errors and warnings...Try starting with this shell.

#include <iostream>
#include <cstring>

class string
{

public:

	string():s(NULL), itssize(0) {}
	string(const char* const cstring);

	void displayit() const { std::cout << s << std::endl; }
private:

	char *s;
	int itssize;

};

string::string(const char* const cstring)
:itssize(strlen(cstring))
{
	s = new char[itssize + 1];
	for (int i = 0; i < itssize; ++i)
		s[i] = cstring[i];
	s[itssize] = '\0';
}

int main()
{
	string me("my string");

	me.displayit();
	return 0;
}
jonsca commented: WTG for looking into it more deeply +5
gerard4143 371 Nearly a Posting Maven

Try this one

MyMatrix‬‬: MyMatrix.o MyStringMain
	gcc MyMatrix.o -o MyMatrix

MyMatrix.o: MyMatrix.c
	gcc -c MyMatrix.c

MyStringMain: MyString.o MyStringMain.o
	gcc  MyString.o MyStringMain.o -o MyString -o MyStringRun

MyStringMain.o: MyStringMain.c MyString.c MyString.h
	gcc -c MyStringMain.c 

MyString.o: MyString.c MyString.h
	gcc -c MyString.c


.PHONY:clean
clean: 
	rm -f *.o test.out
gerard4143 371 Nearly a Posting Maven

"I heard in class that dynamic arrays like in java aren't supported"

Hmmmm, yes they are. Try a vector.

gerard4143 371 Nearly a Posting Maven

Ooops I double posted by accident...Can a moderator remove this one..Thanks.

gerard4143 371 Nearly a Posting Maven

I'm guessing its the p[p] part. Well its simple if you break it down..

We'll look at the inner p which just returns the i-th element of the array. The value from the inner p is used as the 'index value' of the outer p[index value]. Its a little unorthodox to use an array this way.

gerard4143 371 Nearly a Posting Maven

What line(s) are you having problems with?

gerard4143 371 Nearly a Posting Maven

Please get out of the habit of using gets(), its a very dangerous function. A much better solution is fgets().

Also...

Your while statement assumes that the fetched c-string is 80 characters long?

while(i!=80)

shouldn't it be

while(i <= strlen(array))

Also this next line is incorrect

if(array[i]==(('a')||('A')||('e')))

It should be

if ( array[i] == (('a') || array[i] == ('A') || array[i] == ('e')))

Plus we have move vowels than 'a' and 'e'

gerard4143 371 Nearly a Posting Maven

Because that's the size of the pointer and not the size of the c-string. Try displaying the pointer with

std::cout << (void*)p << std::endl;
gerard4143 371 Nearly a Posting Maven

Could it be the new line character in the fgets() fetched c-string.

gerard4143 371 Nearly a Posting Maven

Which operating system?

gerard4143 371 Nearly a Posting Maven

Well once for erek and once for the copy of erek in the function call.

gerard4143 371 Nearly a Posting Maven

I'm a little confuse...Do you want a program that will take an inputted binary number and then produce its decimal equivalent?

e.g.

enter a binary number->111
ans->7

Is that what you want?

gerard4143 371 Nearly a Posting Maven

I tried rewriting you code...Please see this link on passing multi-dim arrays in C

http://www.eskimo.com/~scs/cclass/int/sx9a.html

#include <stdio.h>


void print_matrix(int n, int m[4][4]); 

void print_vector(int n, int v[4]); 

void matrix_x_vector(int n, int y[4], int x[4][4], int A[4][1]);


int main()

{  

	int n = 4;



	int m1[4] = {1, 2, 3, 4};


	int m2[4][4] = 	{

				{1,2,3,4},

				{5,6,7,8},

				{9,10,11,12},

				{13,14,15,16}

			};


	int multiplied_matrix[4][1] = 	{

						{0},

						{0},

						{0},

						{0}

					};





	print_vector(n, m1);

	print_matrix(n, m2);

	matrix_x_vector(n, m1, m2, multiplied_matrix);



	printf("\n");
	return 0;

}



void print_matrix(int n, int m[4][4])

{

	int i, j; 

	printf("\nMatrix Given\n");



	for (i=0; i<n; i++)

	{

		for (j=0; j<n; j++)

			printf("%3i", m[i][j]);

		printf("\n");

	}

}



void print_vector(int n, int v[4])

{

	int i;

	printf("\nVector Given\n");



	for (i=0; i<n; i++)

		printf("%3i", v[i]);



	printf("\n");

}



void matrix_x_vector(int n, int y[4], int x[4][4], int A[4][1])

{

	int i, j;



	printf("\nResulted Matrix of [M]*[V]\n");





	for (i=0; i<n; i++)

	{

		for (j=0; j<n; j++)

		{

			A[j][0] += x[j][i] * y[i];

			printf("%4i", A[j][0]);

		}

	
		printf("\n");

	}



	printf("\n");





	for (i=0; i<n; i++)

		printf("%4i\n", A[i][0]);

}

I never verified if the values were correct..

Caeon commented: Very helpful. Helped with solution and gave a link for more info on the topic. +1
gerard4143 371 Nearly a Posting Maven

Number one, your defining your functions inside of main..Place them before main like

void print_matrix(int n, int m[n][n]); // Prints an nxn matrix
void print_vector(int n, int v[n]); // Prints a vector of n elements
void matrix_x_vector(int n, int y[n], int x[n][n], int A[n][n]);

int main()
{}
gerard4143 371 Nearly a Posting Maven

If the server has created the shared memory then pass the shared memory id, passing pointer values between address spaces is wrong. With the shared memory id the clients can attach to the shared memory...

gerard4143 371 Nearly a Posting Maven

Why does it seg fault? Because you have to send the value that the pointer points to and not the pointer.

int *iptr = (int*)malloc(sizeof(int));
*iptr = 1234;

/*the wrong way*/ 
write(sock_var, iptr, sizeof(iptr));

/*the right way*/
write(sock_var, *iptr, sizeof(int));

Note - pointers are only valid in their own address space.

A few questions...This client server program, do they reside on the same computer and do they use Unix sockets? Because if they do you should be passing values used in functions like shmat() which requires shared memory id....once you attach, your pointing to the shared memory.

gerard4143 371 Nearly a Posting Maven

Because 'a' is a character with the value 97(in the ascii codes its 97)..."a" is a c-string that has the characters 'a' and '\0'. The last character('\0') is added by the compiler.

gerard4143 371 Nearly a Posting Maven

Number one, what is campus? Is it a c-string pointer or a character..Your comparing it it to a c-string pointer and them assigning a character to it.

So what is campus?

gerard4143 371 Nearly a Posting Maven

You don't need to loop through the nodes in your overload << operator....Just print the current one and then point to the next node to print.

gerard4143 371 Nearly a Posting Maven

Yes, we haven't learned how to use the string libary... Would this make my life easier? Because this whole function thing is not making sense to me.

Using the string.h library would indeed make life easier but would invalidate the exercise..The idea is to understand, at least superficially, how to create this type of functionality.

gerard4143 371 Nearly a Posting Maven

Well I always use fstream in my includes for file I/O.

gerard4143 371 Nearly a Posting Maven

Well the first thing I would get working is a function that finds a c-string in a c-string. Once that's working then you can work on removing the characters.

A side note - Are trying to accomplish this without the string library?

gerard4143 371 Nearly a Posting Maven

Your defining your static structure as global...So why can't you access it? Try this

#include <stdio.h>

typedef char * msg;

static struct queue
{
  msg message;
  struct queue * next;
}myQueue;


void b()
{
  printf("b()->%p, %s\n",(void*)myQueue.message, myQueue.message);
}

void a(msg m)
{
  myQueue.message = m;
  printf("a()->%p, %s\n",(void*)myQueue.message, myQueue.message);
}

int main()
{
	a("this is the string");
	b();
	return 0;
}
gerard4143 371 Nearly a Posting Maven

I think I understand(I'm probably wrong but you gave so little information). This will return the address of the static structure so that you can access the msg member..Note this breaks the reason for having local static variables.

#include <stdio.h>

typedef char * msg;

void** myfunc()
{
	static struct queue
	{
		msg message;
		struct queue * next;
	}myQueue;

	fprintf(stdout, "msg->%s\n", myQueue.message);

	return (void**)&myQueue;
}

int main()
{
	void **vptr = myfunc();

	*vptr = (void*)"this is some string";

	myfunc();
	return 0;
}
gerard4143 371 Nearly a Posting Maven

I'm really confused at how your using this static variable. Is it a local variable within a function? Maybe if you gave us an example of how you intend to use it..i.e. Post the code that's supposed to use the static variable.

gerard4143 371 Nearly a Posting Maven

Its probably because getchar() is leaving a newline character in the buffer.

After your for statement put a fgetc(stdin).

for(i = 0; i < n; ++i)
        sir[i] = getchar();

	fgetc(stdin);

Daniweb has a great sticky on numerous problems and solution when dealing with input buffers.

gerard4143 371 Nearly a Posting Maven

Tnx Gerard4143,

I'll try it
BTW - I've always thought of 16 as being in INT
as in (INT 4) * (INT 4) = (INT 16)
therefore
(INT x) * (INT x) "should" = (INT y)

_Later,

TNX
Shift-Stop

Well the literal value 16 is some form of integer but x1 is defined as a float type.

gerard4143 371 Nearly a Posting Maven

Try compiling like this

g++ filename.cpp -Wall -ansi -pedantic -o filename

It will point to some very suspicious areas in your code.

21: warning: null argument where non-null required (argument 2)
21: warning: null argument where non-null required (argument 2)
18: warning: ‘stri’ is used uninitialized in this function

The last one is probably the problem....Please use the code tags.

gerard4143 371 Nearly a Posting Maven

Can we see how your populating var[32]?

gerard4143 371 Nearly a Posting Maven

You code should work if...

the function expects a cstring and your passing a cstring..See below.

#include <stdio.h>

void foo(const char* s)
{
	fputs(s, stdout);
}

int main()
{
	int i = 0;

	char ch[32];

	for (i = 0; i < 26; ++i)
		ch[i] = i + 'a';

	ch[26]  = '\0';

	foo(ch);
	return 0;
}

This code works without incident.

gerard4143 371 Nearly a Posting Maven

Number of questions. The first one. Your variable

char val[32];

Is it delimited with a '\0'?

gerard4143 371 Nearly a Posting Maven

Your format specifiers

printf("\n SQRD return X1: %d",x1);

%d is the integer format specifier...You should be using %f.

gerard4143 371 Nearly a Posting Maven

Or you could try this way

#include <iostream>

using namespace std; 

int main ()
{
	char* ch = new char[10];
	
	for (int i = 0; i < 10; ++i)
		*(ch + i) = 'a' + i;

	for (int i = 0; i < 10; i++) 
		cout << static_cast<char>(*(ch + i)) << "  " << static_cast<void*>((ch + i)) << endl;

	delete[] ch;

        return 0;
}
gerard4143 371 Nearly a Posting Maven

Your incrementing your pointer (ch) as you populate your array and then you increment it it again to display the array and then you finally free your pointer...how does this not crash?

When you create you array on the heap save the initial pointer value like so:

char* ch = new char[10];
char *init_ch = ch;

Then use this initial value to set your pointer value back to its starting position.

i.e.

After you populate your array with values, please set

ch = init_ch;

and then run your for statement...when your done set

ch = init_ch;

then

delete [] ch;

gerard4143 371 Nearly a Posting Maven

If you want a function to square a double then try

double mysquare(double x)
{
	return x * x;
}
gerard4143 371 Nearly a Posting Maven

You can't send pointers through a socket connection. Pointer addresses are only valid within its own address space.

gerard4143 371 Nearly a Posting Maven

Ancient D. is correct. Try running this program, noticing the output when you type path and filename.

#include <iostream>
#include <string>

int main()
{
	std::string mystring;

	std::cout << "Enter path and filename->";
	std::cin >> mystring;

	for (int i = 0; i < mystring.size(); ++i)
		std::cout << mystring[i] << " ";

	std::cout << std::endl;
	return 0;
}
Jsplinter commented: thank you for the example! +1
gerard4143 371 Nearly a Posting Maven

Please note that the label 'numbers' is a pointer to the first element in the array numbers[]. So we can write your function call like below:

#include <stdio.h>

#define ARR_SIZE 7

int countEven (int* numbers)
{
	int i = 0;

	for (; i < ARR_SIZE; ++i)
		fprintf(stdout, "ans->%d\n", *(numbers + i));
	return 0;
}

int main (void)
{
	int numbers[] = {2, 5, 10, 16, 31, 17, 30};

	countEven(numbers);/*the array label numbers is a pointer to the first element of the array*/

	return 0;
}

You should be able to figure the rest out.