gerard4143 371 Nearly a Posting Maven

Well first I would get the working functionality out of the way....I would create a function that displays your array.

#include <stdio.h>
#define SIZE 10

void myfunc(int *x)
{
	int i;
	int j;

	printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );

	for ( i = 0; i < SIZE; i++) {
		printf( "%7d%13d       ", i, x[ i ]);

		for ( j = 1; j <= x[ i ]; j++) {
			printf("%c", '+' );
		}

		printf("\n");

	}
}

int main()
{
	int n[ SIZE ]= {19, 3, 15, 7, 11, 9, 13, 5, 17, 1};

	/*If you want...prompt user for values here in a for loop*/

	myfunc(n);

	return 0;
}
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

Generally speaking when you use threads to modify/read global data, you use some form of lock to assure that the data isolated to the modifying/reading thread.

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

Not sure what the problem is but you should never have this in a header file

using namespace std;

Also this line in your makefile

Node.o: Node.cpp Node.h

Why Node.h? Its already in the includes right?

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

gerard4143 is mistaken -- the semicolon is correct in this context.

Ooops, your right my mistake.

gerard4143 371 Nearly a Posting Maven

Look closely at your if statement

if (k - 1 < 0 || j - 1 < 0);

You have a semi-colon where it shouldn't be.

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

Olá

It might help if we knew what

Camiao **vecCamiao[];

Was...

gerard4143 371 Nearly a Posting Maven

I would investigate the functionality bundled up in the string library...especially functions like strstr().

gerard4143 371 Nearly a Posting Maven

You have

Breach();

But have no body for your default constructor.

Try

Breach() {}

gerard4143 371 Nearly a Posting Maven

I just tried your code on a AMD/64

#include <stdio.h>

unsigned char data[3] = {0x3f,0x20,0xf2};


int main()
{
	int j = 0;
	for(j=0;j<3;j++) 
		printf("0x%x\n",data[j]);

	return 0;
}

And it produced output

0x3f
0x20
0xf2

gerard4143 371 Nearly a Posting Maven

Can we see your structure/class?

gerard4143 371 Nearly a Posting Maven
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

While I appreciate your help, I do not need a lecture on the organization of my code. A simple "You need to add a line to clear your input buffer" would have sufficed.

I am no programmer, nor do I ever plan on being one. This is just a basic C/C++ class required for my major to which the instructor continually points out to us that we will probably never use this again once we are done.

Well we 'the people of this forum' don't appreciate posters who can't even bother to present their code in a readable fashion and still expect prompt and free help...

gerard4143 371 Nearly a Posting Maven

Try fgets, this is from my Linux manpages(help files)

char *fgets(char *s, int size, FILE *stream);

gets() reads a line from stdin into the buffer pointed to by s until
either a terminating newline or EOF, which it replaces with '\0'. No
check for buffer overrun is performed (see BUGS below).

gerard4143 371 Nearly a Posting Maven

I really have to ask, what run time error is expected?

All kidding aside take a look at the line I added. This forum had a real good sticky on problems and remedies when dealing with input buffers...I would look it up..

A few notes on sytle

comments in standard C are /*something*/ not //something

The C language ignores whitespace so use programmers use this feature to layout their programs in a very logical readable/manner. Please consider this the next time you post....Whitespace can be good if used in a consistent and logical manner.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
long int factorial(int n_end);
void clear_message(char* input);
int main()
{
        int menu, i, j, n, n_end;
        double y, x, exp, sum, sum_2;
        char message[111]={'\0'};
        printf("\nHomework 4:\n\n");
        printf("There are two parts: part 1 and part 2.\n");
        printf("Enter the part number to execute (1 or 2):");
        scanf("%d", &n);
        switch(n){
            case 1:
                printf("\nPart I:\n\n");
                printf("Enter your message:");
		while (fgetc(stdin) != '\n') {}/*this line clears the input buffer*/
                fgets(message, 111, stdin);
                printf("\nYour message is: %s\n\n", message);
                clear_message(message);

                int length=strlen(message);
                char* reverse = (char*) malloc(length + 1);
                for(i=0,j=length-1;j>=0;--j,++i)
                {
                reverse[i] = message[j];
                }
                reverse[i] = '\0';
                printf("Reversed String is: %s\n\n", reverse);
                break;
            case 2:
                printf("\nPart II:\n\n");
                printf("Series evaluation: x^n/n! from n=0 to n=n_end.\n");
                printf("\n");
                printf("Enter x, a real number, and n_end, a large positive interger.\n");
                scanf("%lf %d", &x, &n_end);
                printf("\nThe values entered are: x = %g and n_end = %4d.\n\n", x, n_end);
                for (n=0, sum=0.0;n<n_end;n++){
                sum = sum …
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

This brings up one question. Where is the server getting the responds message? Is it from some canned messages that it reads down into?

gerard4143 371 Nearly a Posting Maven

Well I would blah, blah blah.

Please post code that is remotely correct or at least post code where the error is obvious or post an explanation of the problem...

Posting something, something, blah, blah, blah makes us wonder - are you a spam bot?

gerard4143 371 Nearly a Posting Maven

Well the first function takes a value and squares it with pow() and then passing the answer 4 to the second function which cubes it 4^3 = 64.

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.

gerard4143 371 Nearly a Posting Maven

I looked at your header file and you need to add std:: to string so that it reads

std::string

Remember your not using namespace std here because its a header file.

gerard4143 371 Nearly a Posting Maven

Your code should be executed or called within main..Example

int main()
{
if (some_condition)
{

}
else if(some_other_condition)
{

}

return 0
}

You have a whole lot of code that isn't contained within main or any callable function.

#include <iostream>

bool myfunc(const char *s)//function - myfunc starts
{
	if (std::cout << s << std::endl)
	{
		return true;
	}
	else
	{
		return false;
	}
}//function - myfunc ends

unsigned long double_it(unsigned long val)//function - double_it starts
{
	return val * 2;
}//function - double_it ends

int main(void) 
{
	unsigned long ans = 0;

	myfunc("test string");//function call

	ans = double_it(123);//function call

	std::cout << ans << std::endl;

	return 0;
}
gerard4143 371 Nearly a Posting Maven

Hi guys,
quick question. Does the structure

try
{

}

catch

{

exit(0);
}

require the preprocessor directive

#include<cstdlib>

to be declared at the beginning of the program? The thing is if I don't declare it the visual c++ compiler works fine but if I try in Unix with g++ it doesn't compile...
thanks

You only need cstdlib for exit(0);

gerard4143 371 Nearly a Posting Maven

Maybe I'm looking at this all wrong but won't you just copy the multimap to a map container of the same type? Ooops didn't read the entire post...please ignore.

gerard4143 371 Nearly a Posting Maven

Well if I was responsible for this I would:

1. Create a data structure to hold my data - country, population, birth rate and death rate.
2. Create a vector with my data structure as its type.

gerard4143 371 Nearly a Posting Maven

Here's a good starting point.

#include <iostream>
#include <vector>
#include <algorithm>

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

Shouldn't you be prompting, for the numbers, inside the for loop?

gerard4143 371 Nearly a Posting Maven

Well char clr is a character not a string. If you want to assign to it, pass a character like

brd[x][y].clr = 'W';

Notice the single quotes..

gerard4143 371 Nearly a Posting Maven

So what is a

brd[x][y].clr="W";//Problem occured here
gerard4143 371 Nearly a Posting Maven

Why? Memory exhaustion is a very real possibility in a system with finite resources.

gerard4143 371 Nearly a Posting Maven

Did you actually look at the code I posted?

gerard4143 371 Nearly a Posting Maven

do you mean this array?

double * dSM = NULL

It depends on the size you pass to new.

gerard4143 371 Nearly a Posting Maven

Because make software comes in many flavors and the script it executes 'can' depend on the operating system...It really depends.

gerard4143 371 Nearly a Posting Maven

Not really sure what the objective is here? If you want to allocate memory via a function try the following code:

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

bool vector(char **theArray, int size);

int main()
{
	int i = 0;
	char *ch = (char*)NULL;

	if (!vector(&ch, 100))/*call vector and test for failure*/
	{
		fputs("Allocation failed...exiting program\n", stderr);
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < 26; ++i)/*load some values*/
		*(ch + i) = ('a' + i);

	*(ch + i + 1) = '\0';/*delimit our cstring with '\0'*/

	fprintf(stdout, "ch->%s\n", ch);/*display our cstring*/

	free(ch);

	ch = (char*)NULL;

	return 0;
}

bool vector(char **theArray, int size)
{
	*theArray = (char*)malloc(size * sizeof(char));
		
	if (!theArray)
		return false;

	return true;

}
gerard4143 371 Nearly a Posting Maven

Not sure what happened here...I posted twice.

gerard4143 371 Nearly a Posting Maven

What about server_address.sun_path ?

You should read this link

http://www.wsinnovations.com/softeng/articles/uds.html

gerard4143 371 Nearly a Posting Maven

I didn't say it ought to work -- I said that I didn't immediately see a reason that it shouldn't. As it happens, I made that post before I had had my morning coffee :-)

regardless, you were correct, a pair object contained in a map container has its first member defined as constant.

gerard4143 371 Nearly a Posting Maven

Those #define true 1 and #define false 0 are just preprocessor macros which will find and replace all true and false and replace them with 1 and 0 respectively.

gerard4143 371 Nearly a Posting Maven

Which operating system are use using...Threads are operating system specific.

i.e. Threads coded on Linux will not work on Windows.

gerard4143 371 Nearly a Posting Maven

Edit: this was for the first page ;p

Doesn't compile on VC++ 2010

So it wasn't GCC's g++ compiler or standard libraries. That's good to know, thank-you for posting/verifying.

gerard4143 371 Nearly a Posting Maven

Pass by const reference if you want to accept literals:

myc(myc const& a, myc const& b):itsvalue(a.itsvalue + b.itsvalue) {}

Yes of course, how do we expect to change a literal..Its funny how simple the answer is when someone else points it out.

gerard4143 371 Nearly a Posting Maven

Hi,

I have a question about constructors and the difference between passing by value and by reference.

If you look at the two examples I attached. Example one has a constructor that passes by value and example two pass by reference. Now I want to know why example one will use the constructor:

myc():itsvalue(0) {}

to convert this line

myc m(123, 456);

and work but example two will fail to compile saying no valid constructor found..Why does this happen?

Example-One

#include <iostream>

class myc
{
  
public:
  
  typedef unsigned long size_type;
  
  myc():itsvalue(0) {}
  myc(size_type val):itsvalue(val) {}
  myc(myc a, myc b):itsvalue(a.itsvalue + b.itsvalue) {}//This is the constructor by value
  
  friend std::ostream& operator<<(std::ostream & out, const myc & c);
  
private:
  
  size_type itsvalue;
  
};

std::ostream& operator<<(std::ostream & out, const myc & c)
{
  return out << c.itsvalue;
}

int main()
{
  myc m(123, 456);//converts literal integers with myc(size_type val):itsvalue(val) {}
  
  std::cout << m << std::endl;
  return 0;
}

Example-Two

#include <iostream>

class myc
{
  
public:
  
  typedef unsigned long size_type;
  
  myc():itsvalue(0) {}
  myc(size_type val):itsvalue(val) {}
  myc(myc & a, myc & b):itsvalue(a.itsvalue + b.itsvalue) {}//This is the constructor by reference
  
  friend std::ostream& operator<<(std::ostream & out, const myc & c);
  
private:
  
  size_type itsvalue;
  
};

std::ostream& operator<<(std::ostream & out, const myc & c)
{
  return out << c.itsvalue;
}

int main()
{
  myc m(123, 456);//Now myc(size_type val):itsvalue(val) {} fails to convert the literal inetegers
  
  std::cout << m << std::endl;
  return 0;
}
gerard4143 371 Nearly a Posting Maven

I'm not sure what trying to accomplish but you have String and string. If your using std::string in your class then you should include the header file and maybe call your version of string something more unique like maybe My_String.