gerard4143 371 Nearly a Posting Maven

Try something like below

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

void mycall(char cmd[])
{
	char *pch = NULL;

	printf ("%s\n", cmd); /* correctly prints 'ls -l' */
	printf ("Before call to strtok\n");
	pch = strtok (cmd, " ");
	printf ("After call to strtok\n"); 
}

int main()
{

	char cmd[] = "ls -l";
	mycall(cmd);
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

So you want a function the behaves exactly like clrsrc but isn't clrsrc?

gerard4143 371 Nearly a Posting Maven

You should start my defining x as a string and then compare it to things likes

"brown"

Please note the double quotes not signal quotes.

Or better yet you should create an enumeration.

enum Colors {brown, red, yellow,...etc};

gerard4143 371 Nearly a Posting Maven

Your allocating memory but you haven't requested a size.

Like:

char *str = new char[sizeof(char) * 100];

To allocate a memory chunk equal to 100 bytes.

gerard4143 371 Nearly a Posting Maven

First you didn't allocate any memory for the str character pointer....

gerard4143 371 Nearly a Posting Maven

You have this

atoi(argv);

Which doesn't save the value generated by the function atoi()

Here's the definition of atoi

int atoi(const char *nptr);

Note it returns an integer and does not change the value of nptr in place.

gerard4143 371 Nearly a Posting Maven

What i mean is that if i do a right shift, where does the LSB goes?
can i save it while doing so, or is it lost for ever and ever...?

Where does it go? Its truncated. Can you save it? Yes you can by masking out the value before you shift.

gerard4143 371 Nearly a Posting Maven

LNAME is a float....You probably want a string for LNAME.

gerard4143 371 Nearly a Posting Maven

I'm not really sure what you mean...Is it something like below?

#include <iostream>

int main(int argc, char**argv)
{
	unsigned int i = 0;
	unsigned int x = 1;
	int size = sizeof(unsigned int);
	size *= 8;

	std::cout<<"ans->"<<x<<std::endl;

	for (i = 0; i < size; ++i)
		std::cout<<"ans->"<<(x <<= 1)<<std::endl;

	return 0;
}
gerard4143 371 Nearly a Posting Maven

I get a warning message while executing a C program containg 'gets'.Is there any way to avoid this.Currently I am using ubuntu OS>

Yes, use a crappy compiler that doesn't warn you of 'potentially' bad...bad programming practises.

And its compiling the program not executing it that generates the warning...

gerard4143 371 Nearly a Posting Maven

External to what?

gerard4143 371 Nearly a Posting Maven

Quick question, does it have to be a vector? Could it be a map?

gerard4143 371 Nearly a Posting Maven

When you assign them memory or you may point then to an existing object which should already be initialized.

Your example 'class foo' returns len which does not exist....

gerard4143 371 Nearly a Posting Maven

You have a memory leak in your = operator. You should first delete your member pointer before you create the new one.

Plus you have numerous errors in your example like:

SomeClass(int size) {myMember = new OtherClass(thingSize);};

you have size but use thingSize?

gerard4143 371 Nearly a Posting Maven

is the error on line 7 or 8?

If its on 7 where is ConstantBuffer defined?

gerard4143 371 Nearly a Posting Maven

Try using iterators.

gerard4143 371 Nearly a Posting Maven

Not absolutely sure what your trying to accomplish here. Is this what your looking for?

#include <iostream>

class BaseClass
{
protected:
    double parameter;
};

class DerivedClass : public BaseClass
{
};

class SecondDerivedClass : public BaseClass
{
};

class AnotherClass
{
public:
	void setParameterOfDerivedClass( BaseClass * baseClass, double parameterValue ) 
	{
		std::cout<<"called ptr"<<std::endl;
	}
	void setParameterOfDerivedClass( BaseClass & baseClass, double parameterValue ) 
	{
		std::cout<<"called refer"<<std::endl;
	}
};

int main()
{
	DerivedClass myDerivedClass;
	BaseClass* pointerToMyDerivedClass;

	pointerToMyDerivedClass = &myDerivedClass;

	AnotherClass myAnotherClass;

	myAnotherClass.setParameterOfDerivedClass(myDerivedClass, 10.0 );
	myAnotherClass.setParameterOfDerivedClass(pointerToMyDerivedClass, 10.0 );

	return 0;
}
gerard4143 371 Nearly a Posting Maven

On lines 11 and 12...Do you even have a valid constructor for these?

gerard4143 371 Nearly a Posting Maven

Your incrementing copy then deleting it....How does this not crash?

gerard4143 371 Nearly a Posting Maven

i see how i missed that. i fixed that, but this statement:

else if (command != "function1" || "function2")  
     {         
    cout << "Invalid Entry\n";       
      cout << "Please your correct terms\n";  
     }

keeps appearing. it always prints out, regeardless of putting y or Y.

Its the same problem as stated above by Akill 10

gerard4143 371 Nearly a Posting Maven

Yes use strtol()..

The value is the same, hex or integer, its just a matter of how you choose to display it.

gerard4143 371 Nearly a Posting Maven

Here's the newest 64 bit Flash Player from Adobe Labs....Way to go Adobe...

http://labs.adobe.com/downloads/flashplayer10.html

gerard4143 371 Nearly a Posting Maven

Try solving this problem with a linked list. It turns out to be pretty simple...Here's a very rough and buggy example to show you what I mean.

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

struct node
{
	unsigned char value;
	struct node *nptr;
};

void showit(struct node *nptr)
{
	if (nptr->nptr) showit(nptr->nptr);
	fprintf(stdout, "%u", nptr->value);
}

void addone(struct node *ptr)
{
	if (ptr->value < 3)
	{
		++(ptr->value);
	}
	else if (!ptr->nptr)
	{
		ptr->value = 0;
		ptr->nptr = (struct node*)malloc(sizeof(struct node));
		ptr->nptr->value = 1;
		ptr->nptr->nptr = NULL;
	}
	else
	{
		ptr->value = 0;		
		addone(ptr->nptr);
	}
}

int main(int argc, char**argv)
{
	unsigned int i = 0;
	struct node start = {0, NULL};

	for (i = 0; i < 100; ++i)
	{
		addone(&start);
		showit(&start);
		fputs("\n", stdout);
	}
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

This would be easier if you used strtol() to handle all the conversions.

gerard4143 371 Nearly a Posting Maven

Ok, I now have the following:

char *endp;
	long value1, value2;

        unsigned char *hexResult;

	char hex1[3] = "0x";
        char hex2[3] = "0x";

	strcat(hex1, inputData);
	value1 = strtol(hex1, &endp, 16);

	strcat(hex2, mask);
	value2 = strtol(hex2, &endp, 16);

Now value1 and value2 hold the decimal values for the two hex inputs. What I need to do is perform an AND operation on the two hex values when I get them into hex. E.g.,

0x1006 & 0xF0 = 0x1000

I know that hex values can be represented as integers. i.e. I can do something like:
int memAddr = 0x1fe;

And that I can perform an AND operation on two hex (int) values. How can this be achieved from the above?

Right away your character arrays are too small. You have

char hex1[3] = "0x";

and then you

strcat(hex1, inputData);

hex1 is only three characters long...i.e It already is filled with '0', 'x', '\0' so you can't append anything onto it without overflowing it.

gerard4143 371 Nearly a Posting Maven

Yes, I will read in a string something like "1006", and I want to change that to "0x1006", which I can then cast to an int to get the correct value (and perform an AND operation on that and another hex string; e.g., (0x1006 & 0xFF)). Does that make sense?

Yeah it does now...You'll have to convert(not cast) your string into an integer with strtol.

gerard4143 371 Nearly a Posting Maven

I'm not sure what you mean by a 'hex string'...Do you meant something like

char hex_string[] = "0xffddee99";

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

#define ARR_SIZE 6

int main(int argc, char**argv)
{
	char hex1[10] = "0x";
	char hex2[10] = "0x";

	char ch[ARR_SIZE];

	fputs("enter some hex->", stdout);
	fgets(ch, ARR_SIZE, stdin);

	strcat(hex1, ch);

	fprintf(stdout, "You entered->%s\n", hex1);

	/*
		now your working with a string of characters so you'll
		have to convert your string into a integer with strtol()
	*/

	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

The results are the same -- a character is read -- but comparing the two is a little hard since your comparing two different programing paradigms...I guess if you strip away the syntax and look at the low level results of the code...then yes the code is equivalent since both accomplish the same thing.

gerard4143 371 Nearly a Posting Maven

Sure...What exactly is wrong?

gerard4143 371 Nearly a Posting Maven

So what's the problem?

gerard4143 371 Nearly a Posting Maven

You might want to investigate arrays..

const size_t ARRAY_SIZE = 10;
int MyIntArray[ARRAY_SIZE];

gerard4143 371 Nearly a Posting Maven

what my code does is it waits for the user to type in "led" and performs the blink function. what i am trying to do is introduce the letter 'p' as a command to stop the blinking. what happens is that blink is only executed once.

if (!(strcmp(userinput, led))){		//condition 1
	for(howlong = 0; howlong++ < 0xFF; howlong++){		//condition 2	
		blink();
		if (getch() == 'p')
		break;
	}
}

getch() is probably picking up the '\n' in the input buffer...Try replacing if (getch() == 'p') with

fprintf(stdout, "char->%x\n", getch());

What's the value displayed?

gerard4143 371 Nearly a Posting Maven

other program's edit

Not sure what you mean by this...

gerard4143 371 Nearly a Posting Maven

The only thing you have to worry about is displaying the values...Try something like below
std::cout<<std::hex<<1234<<std::endl;

When you input the values make sure you use this format 0xXXX again see below

0x4d2

gerard4143 371 Nearly a Posting Maven

I would try sum += lower...

int range(int lower, int higher, int sum=0)
{

 while (lower <= higher)
{
sum += lower;
++lower;
}

return sum;
}
gerard4143 371 Nearly a Posting Maven

No problems...Again thanks for the pointers...

gerard4143 371 Nearly a Posting Maven

StuXYZ...Thanks for the reply and pointers, everything you stated makes sense except for the error on line 78. Are you sure about that one because I don't see it.

gerard4143 371 Nearly a Posting Maven

Could someone please verify. Is it proper to have pointers to containers(vectors in this example) where you allocate memory for them and then free them. I googled and found a posting that stated its considered poor coding style to do this...I doesn't seem like it should be poor programming style to me.

struct student contains the pointer

std::vector<double> *assignments;

Which I allocate in the program and then free towards the end with..

for (i = 0; i < the_students_size; ++i)
{
the_students.assignments->clear();
delete the_students.assignments;
}


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


//struct with pointer to vector....
struct student
{
  std::string name;
  double midterm, final;
  std::vector<double> *assignments;
};

bool student_compare(const struct student & lhs, const struct student & rhs)
{
  return lhs.name < rhs.name;
}

int main(int argc, char**argv)
{
  struct student a_student;
  std::vector<struct student> the_students;
  int choice;
  double x;
  
  while (true)
  {
    std::cout<<"(0)exit (1)set (2)sort and view->";
    std::cin>>choice;
    
    if (choice < 1) break;
    
    if (choice == 1)
    {
      a_student.assignments = new std::vector<double>;
      
      if (!a_student.assignments) return 1;
      
      std::cout<<"Please enter your name->";
      std::cin>>a_student.name;
      
      std::cout<<"enter midterm and final marks->";
      std::cin>>a_student.midterm>>a_student.final;
      
      std::cout<<"Please enter assignment marks "
		"Eof file for completion"<<std::endl;
      
      while (std::cin>>x)
	a_student.assignments->push_back(x);
      
      std::cin.clear();
      
      the_students.push_back(a_student);
    }
    
    if (choice == 2)
    {
        std::vector<struct student>::const_iterator begin_iter = the_students.begin();
	std::vector<struct student>::const_iterator end_iter = the_students.end();
	
	std::sort(the_students.begin(), the_students.end(), student_compare);
	
      while (begin_iter != end_iter)
      {
	std::cout<<"Name->"<<begin_iter->name<<std::endl;
	std::cout<<"Midterm->"<<begin_iter->midterm<<" Final->"<<begin_iter->final<<std::endl;
	
	std::vector<double>::const_iterator ibegin_iter = begin_iter->assignments->begin();
	std::vector<double>::const_iterator iend_iter = begin_iter->assignments->end();
	
	while (ibegin_iter != iend_iter)
	{
	  std::cout<<*ibegin_iter<<" ";
	  ++ibegin_iter;
	}
	std::cout<<std::endl; …
gerard4143 371 Nearly a Posting Maven

You don't really have to compute the number of numbers...

while (lower <= higher)
{
//do something
++lower;
}
gerard4143 371 Nearly a Posting Maven

Could you post your structure.

gerard4143 371 Nearly a Posting Maven

thanks, I changed

node* head=NULL;         
node* second=NULL;       
node* third=NULL;

to

node* head=new node;  
node* second=new node;
node* third=new node;

now it works, but where should I deallocate the dynamic allocated memory? at the end of the main program?

You really should implement your nodes as classes and let the destructor's worry about freeing up allocated memory..

gerard4143 371 Nearly a Posting Maven

You haven't allocated any memory for your pointers

node* buildonetwothree()
{
node* head=NULL;
node* second=NULL;
node* third=NULL;

head->data=1;

head->next=second;
second->data=2;
second->next=third;
third->data=3;
third->next=NULL;

return head;
}
gerard4143 371 Nearly a Posting Maven

Dumb reply...Why don't you run the code and see for yourself?

gerard4143 371 Nearly a Posting Maven

A bit off topic , but still one suggestion is to avoid using 'fread' as the fp. fread is already a clibrary function and will create confusion. I think its not a good practice.

So you think we shouldn't use C library functions in a C program?

gerard4143 371 Nearly a Posting Maven

Number one...are you checking to see if the file opened correctly?

If so then try:

#define MAXLINE 4096

char recvline[MAXLINE];
int connfd;
FILE *fd;

while ((write(connfd, recvline, fread(recvline, 1, MAXLINE, fd))) > 0)
{}
gerard4143 371 Nearly a Posting Maven

I had that thought as well, but then my overwhelming hatred of the C++ syntax for it kicked in.

Then why are you coding in C++?

gerard4143 371 Nearly a Posting Maven

Well if the string class is C++ then I would adopt its convention..

gerard4143 371 Nearly a Posting Maven

The problem is that printing binary data messes up the console.

Well duh...Try printing the data as hex values using the format specifier %x.

gerard4143 371 Nearly a Posting Maven

What is this

out(client_fd, string, readcount);

Ooops, found your posted code..

You could help us out with a few remarks in your code...Also I noticed many malloc's

char* string = malloc(BUFLEN);

But no free's.

gerard4143 371 Nearly a Posting Maven

Try including stdlib.h

Also, you may want to put this at the end of the main function - return 0;