gerard4143 371 Nearly a Posting Maven

Try replacing the problems line with this.

(*ptrL)->ptrNext = ptrInput;
gerard4143 371 Nearly a Posting Maven

Here's a tighter version.

#include <iostream>
#include <string>
#include <algorithm>
#include <locale>

std::string lower_string (const std::string& a_string, const std::locale& a_locale )
{
    char *str = new char[a_string.size() + 1];
    std::string::const_iterator begin = a_string.begin();
    std::string::const_iterator end = a_string.end();

    copy(begin, end, str);

    std::use_facet< std::ctype<char> > ( a_locale ).tolower ( str + 1, str + a_string.size() );

    std::string ans(str);
    delete [] str;

    return ans;
}

int main(int argc, char *argv[])
{
    std::locale loc("");
    std::string name("A nAME tO PASS aLONG");

    std::string ans = lower_string(name, loc);

    std::cout << ans << std::endl;
    return 0;
}

Its been a while since I look at C++ code.

gerard4143 371 Nearly a Posting Maven

I didn't take the time and read your post completely..Ooops

Try something like below.

#include <iostream>
#include <string>
#include <algorithm>
#include <locale>


std::string lower_string (const std::string& a_string, const std::locale& a_locale )
{
    char *str = new char(a_string.size());
    std::string::const_iterator begin = a_string.begin();
    std::string::const_iterator end = a_string.end();

    copy(begin, end, str);

    std::string ans;

    std::use_facet< std::ctype<char> > ( a_locale ).tolower ( str + 1, str + a_string.size() );

    ans = str;
    
    return ans;
}

int main(int argc, char *argv[])
{
    std::locale loc("");
    std::string name("A nAME tO PASS aLONG");

    std::string ans = lower_string(name, loc);

    std::cout << ans << std::endl;
    return 0;
}
gerard4143 371 Nearly a Posting Maven

So what you want is a function that creates a new string which is a copy of the string being past... Why not change the 'const std::string& a_string' parameter to 'std::string& a_string' and change original string or is that outside the requirements?

Your question, get the iterators for the string's begin and end and then increment the begin value by one.

gerard4143 371 Nearly a Posting Maven

You have this on line 104 - 110

static char* posielam = NULL;
/*...*/
do {
printf("Napis cestu:");
} while ((readd = getline(&posielam, &len, stdin)) == -1);

Your reading len characters(it should be len and not &len) into a c-string that points to NULL.

gerard4143 371 Nearly a Posting Maven

oh sorry,i did post this in c thread....i have a cpp file ....i tried both..cpp and c compiler but not progress.

You have a C++ library in your includes '#include <list>' so you have to use the C++ compiler.

gerard4143 371 Nearly a Posting Maven

Are you using a C compiler or C++. Your error message indicates a .cpp file?

gerard4143 371 Nearly a Posting Maven

so you do not need to read in the html file ?

Line 4 reads the file contents into a buffer and then line 6 writes that buffer into a socket.

gerard4143 371 Nearly a Posting Maven

Line 6 is wrong, it should be

n = write(newsock_filedesc,buffer, 256) ;
gerard4143 371 Nearly a Posting Maven

@ Gerard:
urs is exactly the same as mine. the only difference is that you change all the my pointers to []. anyway it doesnt work

@nezachhem:
Wow, can u explain more in depth I dont get what ur saying, but I tried to flush the filePtr anyway. And it WORKSSSS

and how come I dont need to flush in my ubuntu system, but need to flush it in the university's server?

It probably has to do with the SSH connection...You should be doing it on your Ubuntu system as well.

gerard4143 371 Nearly a Posting Maven

Try this version which is mostly like yours..This works on my system.

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

int main()
{
  char buffer[256];
  char charTemp1[40];
  char charTemp2[40];
  int intTemp;
  fpos_t position;
  
  FILE *fd = NULL;
  
  if (!(fd = fopen("employee.dat", "r+")))
  {
    fputs("Could not open file!\n", stderr);
    exit(EXIT_FAILURE);
  }
  
  fgetpos(fd, &position);
  
  while (fgets(buffer, 256, fd) != NULL)
  {
    sscanf(buffer, "%d %s %s", &intTemp, charTemp1, charTemp2);
    
    charTemp1[0] = toupper(charTemp1[0]);
    charTemp2[0] = toupper(charTemp2[0]);
    
    fprintf(stdout, "%d %s %s\n", intTemp, charTemp1, charTemp2);
    fsetpos(fd, &position);
    fprintf(fd, "%d %s %s\n", intTemp, charTemp1, charTemp2);
    fgetpos(fd, &position);
  }
  
  fclose(fd);
  return 0;
}
gerard4143 371 Nearly a Posting Maven

The file would be in the same form

ID FIRSTNAME LASTNAME
10 efron berlian
20 jim smith
30 What Ever

the first line is just trash really and the rest is always number |space| string |space| string

Did you open the data files in nano or vim to verify the contents of the files are the same? I ask because the program seems to work in both cases and maybe its a case of the data files are not the same.

gerard4143 371 Nearly a Posting Maven

Are you sure you working on identical data files?

gerard4143 371 Nearly a Posting Maven

You have a name collision with the standard library.

#include<iostream>

template <typename T> inline const T& max (const T & w,const T & x) { return w > x ? w : x; }

int main()
{
	int a = 10, b = 343;
	std::cout << max(a, b) << std::endl;
	std::cout << std::max(a, b) << std::endl;
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Inclusion guards are a good example of usage.

http://en.wikipedia.org/wiki/Include_guard

gerard4143 371 Nearly a Posting Maven

Yes you need the libraries statically compiled or to be present on the machine.

gerard4143 371 Nearly a Posting Maven

When you need dynamic memory, trees, lists, passing large objects to functions.

gerard4143 371 Nearly a Posting Maven

I'm no math expert but

mid = (min + (max - min)) / 2;

doesn't that reduce to

mid = max / 2;
gerard4143 371 Nearly a Posting Maven

Could you post a small example of the datafile.

gerard4143 371 Nearly a Posting Maven

Oh thanks a lot Gerad !!!!
Can u show me the final code with the loop ?

How about you show us the final code.

gerard4143 371 Nearly a Posting Maven

For this to work, you'll have to save the number of characters matched in each retrieval... Something like below.

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

int main(int argc, char**argv)
{
	int res = 0;
	int res2 = 0;
	char buf [] = "25+2;44+1;8-2.";

	char op1[20];
	char op2[20];
	char operazione[20];
	char en[20];

	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	res2 += res + 1;
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);
	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	res2 += res + 1;
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);
	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);

	exit(EXIT_SUCCESS);
}

I would put this functionality in a while loop to clean it up.

gerard4143 371 Nearly a Posting Maven

This is how I would solve this problem

#include <iostream>

class BaseClass
{
public:

    BaseClass( ){ }

    ~BaseClass( ){ }

	double getitsvalue() const { return value; }
	void setitsvalue(double val) { value = val; }

protected:

    double value;

};

class DerivedClass : public BaseClass
{
public:

    DerivedClass( ) { }

    ~DerivedClass( ){ }

};

std::ostream& operator <<(std::ostream & out, const BaseClass & b)
{
	return out << b.getitsvalue();
} 

int main( )
{
    DerivedClass myDerivedClass;

    myDerivedClass.setitsvalue(10.0);

    std::cout << myDerivedClass << std::endl;

    return 0;
}

Since DerivedClass objects can be sliced down to BaseClass this'll work.

gerard4143 371 Nearly a Posting Maven

The code below will demonstrate why pointer addition behaves this way

#include <stdio.h>

#define ARR_SIZE 4

int mya[ARR_SIZE] = {1233, 543, 567, 789}; 

int main(int argc, char**argv)
{
	int i = 0;
	int *iptr = mya;

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "value->%d\n", iptr[i]);/*array notation*/

	fputs("\n\n", stdout);

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "value->%d\n", *(iptr + i));/*add integral to pointer*/
	return 0;
}
gerard4143 371 Nearly a Posting Maven

hi navedalam, ur explanation is convincing but j is not integer POINTER it is integer variable. So, by ur explanation...

ptr=10. so, 10*4 = 40. Now, this 40 shud be added to 19 b'coz j=ptr+19. i.e 40+19=59.
Therefore, j's value shud be 59. Please give me ur view..

Thank you.

When you add a integral value to a pointer, you use the integral * unit size. Unit size in this case is sizeof(int).

So

j = ptr + 10

is really

/*pseudocode*/
j = ptr + (10 * sizeof(int));
gerard4143 371 Nearly a Posting Maven

It is not neccessary to initialise j there...it could be iniatialise in next line...that would not pose any problem.

I think I need to go to the optometrist....

gerard4143 371 Nearly a Posting Maven

Ya, but it must be initialise by a address location or NULL (which is nothing but zero)

I'm not talking about the pointer, I'm talking about the j integer which is uninitialized.

gerard4143 371 Nearly a Posting Maven

You should mark this a solved.

gerard4143 371 Nearly a Posting Maven

No, that might not be the reason. :icon_confused:

Its still unwise to use uninitialized local variables.

gerard4143 371 Nearly a Posting Maven

And your example

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

char * toString(char character)
{ 
	char *x =	malloc(2 * sizeof(char));
	x[0] = character;
	x[1] = '\0';	
	return x;			
}

int main(int argc, char**argv)
{
	int ca;
	char *ans = NULL;

	fputs("Enter a character->", stdout);
	ca = fgetc(stdin);

	ans = toString(ca);
	
	fprintf(stdout, "%s\n", ans);

	free(ans);
	ans = NULL;
	return 0;
}
gerard4143 371 Nearly a Posting Maven

My point on the comments refers to commenting something like this

char character = getchar(); //get a character from stdin

getchar() is a function from the standard library. Its well documented, so I consider commenting its functionality redundant. That said, if your Prof wants comments then give him/her comments.

gerard4143 371 Nearly a Posting Maven

Any memory that you allocate should be also freed and since your returning a pointer to the allocated memory, you can use that to free it.

Also, why all the comments its really distracting.

gerard4143 371 Nearly a Posting Maven

@gerard4143: writing makefiles is such a voodoo black-magic kinda thing. I wouldn't wish that on my worst enemy. In this case, either qmake or cmake are directly and very easily usable with Qt, that is by far the easier option, if Qt Creator really cannot be used to build a release version (extremely unlikely!).

LOL...I thought user would like a brief introduction into making makefiles. You know something that would help demystify that voodoo.

gerard4143 371 Nearly a Posting Maven

You could use

buf = const_cast<char*>(ConstBuffer);

but using a cast to get your code to work is a good indicator of poorly written code.

gerard4143 371 Nearly a Posting Maven

Because j is a local variable it takes whatever value that happens to be on the stack...To fix this initialize j to 0

int j = 0;
gerard4143 371 Nearly a Posting Maven

Try this

double sum = accumulate(a.begin(),a.end(),0.0);
gerard4143 371 Nearly a Posting Maven

try compiling with these switches

gcc filename.c -ansi -Wall -pedantic -o filename

This line

write(clientFd,sumOfPrimes(lowerLimit,upperLimit),200);

sumOfPrimes returns an int which you are saying is 200 bytes big...It should be

int ans = sumOfPrimes(lowerLimit,upperLimit);
write(clientFd, (const void*)&ans,sizeof(int));
gerard4143 371 Nearly a Posting Maven

A make file is just a list of instructions for the compiler and linker. If your interested try googling make file tutorial or check here

http://mrbook.org/tutorials/make/

gerard4143 371 Nearly a Posting Maven

I'm not sure what C compiler your using but you should use one that's modern..

these lines are wrong

read(clientFd,lowerLimit);
read(clientFd,upperLimit);
write(clientFd,sumOfPrimes(lowerLimit,upperLimit));

ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);

Please note the number of arguments.

gerard4143 371 Nearly a Posting Maven

Can we see an example of the string?

gerard4143 371 Nearly a Posting Maven

Line 34 should be

writeRes = write(fd_out,genBuffer,bufsize);
gerard4143 371 Nearly a Posting Maven

Line 15

int input(worker_t*);
gerard4143 371 Nearly a Posting Maven

why do you use call input twice, using the increment?

He's showing you two equivalent ways of doing the same thing.

gerard4143 371 Nearly a Posting Maven

If the poster will search this section, he'll find a post that shows how to randomly fill an array with unique values...The post was recent(1 - 7 days ago).

gerard4143 371 Nearly a Posting Maven

A few notes, this is C so main return an integer and you should define your array like below.

#include<stdio.h>

int main()
{
  int arr[5][5]={ {2,4,8,11,15},{6,8,5,8,7},{3,5,1,21,72},{28,95,62,8,2},{4,1,7,6,8}};
  int i,j,val;
  
  val=arr[0][0];
  for(i=0;i<5;i++)
  {
     for(j=0;j<5;j++)
     if(val<arr[i][j+1])
      {
        val=arr[i][j+1];
      }
  }
printf("\n %d ",val); 
return 0;
}

This worked correctly on my machine.

gerard4143 371 Nearly a Posting Maven

Save the data to file.

gerard4143 371 Nearly a Posting Maven

The GCC compiler has this option built in, just compile with this option

-ftime-report

example:

g++ filename.cpp -ftime-report -o filename

gerard4143 371 Nearly a Posting Maven

You need to add an extra for loop like below.

#include <iostream>

using namespace std;

int main()
{
	for (int i = 0; i < 5; ++i)
	{
		for (int j = i + 1; j > 0; --j)
		{
			cout << i + 1;
		}
		for (int j = 5 - i - 1; j > 0; --j)
		{
			cout << 5 - i;
		}
		cout << 5 - i;
		cout << endl;
	}
	return 0;
}
gerard4143 371 Nearly a Posting Maven

If think you should upgrade your compiler, its passing back mangled names in its error message.

This, on line 81

bool search(int roll,Node **current,Node **previous)

Should be

bool list::search(int roll,Node **current,Node **previous)
gerard4143 371 Nearly a Posting Maven

Narue beat me to the punch...Here's my simple solution

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

#define ARR_SIZE 16
#define LOW 0
#define HIGH 15

int mya[ARR_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

int main(int argc, char**argv)
{
	int i = 0;
	int j = 0;
	int count = 0;
	int index = 0;
	time_t seconds;
	time(&seconds);
	srand((unsigned int) seconds);

	for (; i < ARR_SIZE; ++i, ++count)
	{
		index = rand() % (HIGH - LOW + 1 - count) + LOW;
		fprintf(stdout, "ans->%d\n", mya[index]);
			
		for (j = index; j < (ARR_SIZE - 1); ++j)
		{
			mya[j] = mya[j + 1];
		}
	}

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

As you can see in the function useLocal the value of variable x is set to 25;at the first call, its value is still 25, then is incresead of 1, and when it's called another time,its value is 26. Ok, so far so good!! Are you sure about that? In the second call, x should start with the value 25.

Now your function that contains a static variable, Static variables are well static and they have lifetimes that last as long as the program. How is this accomplished? In my implementation of C, its accomplished by declaring all static variables global but with controlled access and because they are global(on my implementation) I can write code like so...Note you shouldn't write code that breaks what the compiler, compiles.

#include<stdio.h>

int* useStaticLocal(void);


int main()
{
    int *iptr = useStaticLocal();

    fprintf(stdout, "ans->%d\n", *iptr);

    return 0;
}


int* useStaticLocal(void)
{

	static int x = 50;

	printf("\nlocal static x is %d on entering useStaticLocal\n",x);
	x++;
	printf("\nlocal static x is %d on exiting useStaticLocal\n",x);

	return &x;
}