gerard4143 371 Nearly a Posting Maven

I couldn't figure out how to do it with multiple students. For example, for 6 students how can i organize my struct?

struct student 0
....
....
....

struct student 1
....
....
....
.
.
.
struct student 5
....
....
....

That's why I used struct variable inside for loop. I know it is so amateur but i could not figure out how to get what I desire.

If you want an array of structures then define your variable like this:

struct student i[6];

Here's a link on structures in C

http://www.exforsys.com/tutorials/c-language/c-structures-and-unions.html

gerard4143 371 Nearly a Posting Maven

hi...
'm findin it difficult to access system date in a c program.. pls help me... N also to write code fo airline reservation in c...

So help us out...Post what you have so far.

gerard4143 371 Nearly a Posting Maven

Try running this code...What do you notice about the output. Is match different than str?

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

char match[] = "123";

int main(int argc, char**argv)
{
	char str[25];

	fputs("enter a value->", stdout);
	fgets(str, 25, stdin);
	

	fputs(str, stdout);
	fputs(match, stdout);


	exit(EXIT_SUCCESS);
}
Aia commented: A good starting point +8
gerard4143 371 Nearly a Posting Maven

Hello, there.
I'm trying to write a program which reads student information from keyboard and does some calculations. I've just started it and I dont know much about this "Struct" topic. As far as I know, I've tried to do something and here is the result.

#include <stdio.h>
#define N 2
#define ISIMUZUN 15

  struct student{
  char name[ISIMUZUN];
  char surname[ISIMUZUN];
  char number[10];
  int final;
  int midterm;
  int hw1;
  int hw2;
                };
int main(int argc, char *argv[])
{
  struct student i;

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

   { printf("Please enter student number %d information with blanks between them\n", i+1);
     scanf( "%s %s %s %d %d %d %d", i.number, i.name, i.surname, i.midterm, i.final, i.hw1, i.hw2   );

     printf("%s %s %s %d %d %d %d\n", i.number, i.name[ISIMUZUN], i.surname[ISIMUZUN], i.midterm, i.final, i.hw1, i.hw2 );
      }
     system("pause");
  return 0;
}

I'm getting several errors while compiling this. Does anyone have an idea about whats wrong with it?
Regards..

Why are you using your struct variable as a counter in your for loop?

gerard4143 371 Nearly a Posting Maven

Here's what my manpages say about gets

BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

gerard4143 371 Nearly a Posting Maven

Hey

Im doing a loop with somthing similar to this:

char stn[25];
do
{
  printf ("Insert a number:\n");
  gets(stn);
}while (stn!="123");
if (str=="123")
{
 printf ("Good value!");
}

Now if I input 123, it enters the if that prints "Good value!" and the program ends naturally.

Now if I input 32432, 23432, 23234, 1233,9898, 87343, 238432 (all of this it would continue inside the do while loop) and finally I enter 123, it prints "Good value!" but the program itself crashes.

Very strange. I think it is a error with the buffer overfilling or something similar. I tried with fflush(stdin) before/after/both in the gets() loop but nothing.

How can I solve this?

You can't use this

stn!="123"
str=="123"

Try looking up strcmp or the string library of functions

gerard4143 371 Nearly a Posting Maven

Sounds like your having problems understanding functions...Check out this link

http://www.mycplus.com/tutorials/c-programming-tutorials/functions/

gerard4143 371 Nearly a Posting Maven

This client/server program...Windows or Linux?

gerard4143 371 Nearly a Posting Maven

In your client, try shutting down the write portion of the client socket after you sent the filename to the server.

Also try downloading wireshark, Its great for debugging client/server programs.

gerard4143 371 Nearly a Posting Maven

This is not my code...I picked it up from the internet some time ago, I wish I could give credit but I forget where I got it from..Also its in C++ but can easily be converted to C.

dllcpp.cpp

#include <windows.h>

#pragma comment(linker,"/SECTION:.shared,RWS")

#pragma data_seg(".shared")
    int x = 0;
#pragma data_seg()

extern "C" __declspec(dllexport) int getx();
extern "C" __declspec(dllexport) void setx(int val);
BOOL APIENTRY DllMain(HANDLE hmodule, DWORD dwreason, LPVOID lpreserved)
{
	switch (dwreason)
	{
		case DLL_PROCESS_ATTACH: break;
		case DLL_PROCESS_DETACH: break;
		case DLL_THREAD_ATTACH: break;
		case DLL_THREAD_DETACH: break;
	}
	return TRUE;
}

int getx()
{
	return x;
}

void setx(int val)
{
	x = val;
}

dllapp.cpp

#include <iostream>
#include <windows.h>

typedef int (*importgetx)();
typedef void (*importsetx)(int);

int main(int argc, char**argv)
{
	importgetx GetX;
	importsetx SetX;
	int choice = 0, val = 0;

	HINSTANCE hinstlib = LoadLibrary("C:\\ggcpp\\publicdll\\Debug\\dllcpp.dll");

	if (hinstlib == 0)
	{
		std::cout<<"could not load DLL!\n";
		return 1;
	}
	else
	{
		std::cout<<hinstlib<<"\n";
	}

	GetX = (importgetx)GetProcAddress(hinstlib, "getx");

	if (GetX == 0)
	{
		std::cout<<"could not load DLL function!\n";
		return 1;
	}
	else
	{
		std::cout<<GetX<<"\n";
	}

	SetX = (importsetx)GetProcAddress(hinstlib, "setx");

	if (SetX == 0)
	{
		std::cout<<"could not load DLL function!\n";
		return 1;
	}
	else
	{
		std::cout<<SetX<<"\n";
	}

	for (;;)
	{
		std::cout<<"(0)exit (1)set DLL data (2)view DLL data->";
		std::cin>>choice;
		std::cout<<"\n";

		if (choice == 0) break;

		if (choice == 1)
		{
			std::cout<<"enter new DLL value->";
			std::cin>>val;
			std::cout<<"\n";
			SetX(val);
			std::cout<<GetX()<<"\n";
		}

		if (choice == 2)
		{
			std::cout<<GetX()<<"\n";
		}
	}

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

Gerard -- that is assuming the varible being received and passed are the same, right? In my case, that isn't the case. I'm passing one element of a structured array ([x].input), but I'm receiving a completely different array ([x].input_binary[]).

This was an example of passing the array. If you want to pass both, then create a function with two parameters.

gerard4143 371 Nearly a Posting Maven

To pass the array try something like below..

#include <stdio.h>

#define ARR_SIZE 8

void myfunc(int *myint)
{
	unsigned int i = 0;

	for (i = 0; i < ARR_SIZE; ++i)
		myint[i] = i + 5; 
}

int main(int argc, char**argv)
{
	unsigned int i = 0;
	int array[ARR_SIZE];

	myfunc(&array[0]);

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "array[%u]->%d\n", i, array[i]);
	return 0;
}
gerard4143 371 Nearly a Posting Maven

You could try some shared memory...I know you can share memory through a Dll in Windows.

gerard4143 371 Nearly a Posting Maven

Thank you for your reply. I didn't know what the problem was, thank you for explaining. I DO know that it doesn't work.
Do you have any suggestions on what to change?

You could pass the array to the function as a parameter.

gerard4143 371 Nearly a Posting Maven

I state this because, here's the code using a for statement

#include <stdio.h>

#define TABLE_SIZE 10

int main(int argc, char**argv)
{
	unsigned int i = 0;
	fputs("num#\tsqr#\tcude#\n", stdout);

	for (i = 0; i <= TABLE_SIZE; ++i)
		fprintf(stdout, "%u\t%u\t%u\n", i, i * i, i * i * i);
	return 0;
}

I really can't envision a neat solution with an if statement...

gerard4143 371 Nearly a Posting Maven

OK. Thank you. I'll go ahead and write a main() function to produce some sort of output for debugging purposes. In the first two functions I wrote, I did write, save, compile, and debug each one before moving to the next one. The third function is where I ran into the error I could not see (though it was a simple error of the extra ',' and the ';' at the end of the function. Currently, I am sorting out some redundant variables and the variable I named that shadows the parameter in the call (more aggressive attention to detail needed on my part). I appreciate the advice so far with this. Do you have a recommendation or advice on how you would approach the comparison in line 89? I don't mean this insultingly or harshly, but your answer of "No, I would not make that comparison" was a little ambiguous, but then again, it WAS a direct answer to my question as well.

I am trying to not post the code redundantly as I appreciate the back and forth conversation that lets me figure the issues out on my own. However, an example of how you would perform the comparison in line 89, or a pseudo-code variation would be most appreciated.

When a programmer checks for EOF, its usually done as part of the read/get...

#include <stdio.h>

int main(int argc, char**argv)
{
	unsigned int ch;

	while ((ch = getc(stdin)) != EOF)/*part of the read/get*/
	{
		fputc(ch, stdout);
	} …
gerard4143 371 Nearly a Posting Maven

Hi. I'm trying to call a function to convert a decimal integer into a binary number (stored as an array of integers). Before I knew that I couldn't return an array, this is how I thought to implement it. Can anyone help me understand the way I'm actually supposed to do this?
Thank you very much.
Here is MAIN:

#include <stdio.h>
#include <math.h>
int dec2bin (int decimal);
    
int main(void) {
    int binary[8]; // creates array to store 8-bit binary number
    int decimal=5; // with a test value. final version will have user input.

    binary = dec2bin(decimal);

And my function:

int dec2bin (int decimal) {
    int binary[8];
    int x;
       
    for (x=8; x>0; x--) { 
        binary[x] = decimal % 2;
        decimal /= 2;
        printf("%d", binary[x]);
    }
    return binary;
}

Your returning an array that's created on the stack...This is never a good idea because when the function returns it releases its stack memory....therefore invalidating it(the array binary[8]).

gerard4143 371 Nearly a Posting Maven

When your new to C programming you should think of programing as set of steps and smaller the steps the better.

Program a step, compile and fix all errors and warnings. If possible try running the program. Does it produce correct results? If not - correct and recompile.

As a novice you should never get into a situation where you have page(s) of code which has not been compiled and run...Actually this advice applies to all programmers not just novices

As for your if statement...No I won't make that comparison.

gerard4143 371 Nearly a Posting Maven

This doesn't sounds right - if statement - are you sure its not the for statement?

gerard4143 371 Nearly a Posting Maven
int max_temp(measured_data_t id, measured_data_t temperature, , int *ID, int *max_temp);

You have two , , with no parameter. Probably should be

int max_temp(measured_data_t id, measured_data_t temperature, int *ID, int *max_temp);
gerard4143 371 Nearly a Posting Maven

You really shouldn't mix cout and printf especially on the same line...When you have this

<<printf("whatever")

You'll print out the result of calling printf which is four characters in your code.

gerard4143 371 Nearly a Posting Maven

Because I haven't read your materials I can only guess at what you require but the basic setup of your program is wrong...or at least inconsistent...I think this is what your after.

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

#define ARR_SIZE 4

typedef struct  
{
	int site_id_num;   
	int wind_speed; 
	int day_of_month;
	int temperature; 
} measured_data_t;

int main(void)
{             
	int i = 0;
	measured_data_t thestr[ARR_SIZE];

	
	for (i = 0; i < ARR_SIZE; ++i)
	{
	printf("%-10s%-6s%-17s%-20s", "ID", "Day", "Wind Speed (knots)", "Temperature (deg C)");
	fscanf(stdin, "%d%d%d%d", 	&thestr[i].site_id_num,\
					&thestr[i].day_of_month,\
					&thestr[i].wind_speed,\
					&thestr[i].temperature);
	}

	for (i = 0; i < ARR_SIZE; ++i)
	{
	fprintf(stdout, "our data - id->%d, day->%d, wnd->%d, temp->%d\n", 	thestr[i].site_id_num,\
										thestr[i].day_of_month,\
										thestr[i].wind_speed,\
										thestr[i].temperature);
	}
	return 0;
}
gerard4143 371 Nearly a Posting Maven

I say this is the most helpful way...Get an intro book on C programming and read. You have so many errors in your code that I'm not sure where I should begin...

Your structure. Why do you have integer arrays for things like site_id_num, wind_speed, day_of_month and temperature?

gerard4143 371 Nearly a Posting Maven

If your using fscanf then it should read like

fscanf(stdin, "format string", ....);
gerard4143 371 Nearly a Posting Maven

How to create an assembly program that will allow the user to enter somer characters and group the characters according to the following: alphabets, numbers, and special characters.


ASAP.

thanks.

You want your free advice/pointers - ASAP.

gerard4143 371 Nearly a Posting Maven

Aia posted the answer for you..use

sed -n '6, 11p' filename
gerard4143 371 Nearly a Posting Maven

Like I said I'm no expert but you should able to find some here - experts that is

http://kernelnewbies.org/KernelHacking

gerard4143 371 Nearly a Posting Maven

Required is a program that calculate the factorial of a non negative whole number which it gets from a user. It then displays the number and its factorial..

can any one try to test for me this algorithim? plz

How did this end up in the kernel section?

gerard4143 371 Nearly a Posting Maven

I'm no expert but I seem to remember reading somewhere that you have to install all the kernel headers and source for the version of kernel your compiling...Did you?

gerard4143 371 Nearly a Posting Maven

Again this is a stripped down verion of what your trying to accomplish

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

void get_days_in_month(int **days);

int main(int argc, char **argv) 
{
	
	int *days_In_Month = NULL ;	
	
	
	get_days_in_month(&days_In_Month) ;
	
	printf("days in month %d\n", *(days_In_Month + 1));
	printf("days in month %d\n", *(days_In_Month + 2));
	printf("days in month %d\n", *(days_In_Month + 3));
	printf("days in month %d\n", *(days_In_Month + 4));

	free(days_In_Month);

	return EXIT_SUCCESS;
}

void get_days_in_month(int **days) 
{
	
	*days = malloc(sizeof(int) * 5) ;

	/* number of days in each month excluding February */
	(*days)[0] = 31 ;
	(*days)[1] = 28 ;
	(*days)[2] = 31 ;
	(*days)[3] = 30 ;
	(*days)[4] = 31 ;
	
}
gerard4143 371 Nearly a Posting Maven

I have a few questions...Is this a boot loader you wrote or one you downloaded.

Why would you XOR something by itself in order to zero it out rather than just moving 0000h into it? It is just more efficient?

Its more efficient.

gerard4143 371 Nearly a Posting Maven

Well, it works without double pointers... Why can I use a pointer or even a triple pointer to the given string (strings are the one's exluded with " " and chars with ' ', right?) while passing only "blah blah", or even a double pointer to that function... Ok, I'm passing for instance "r" to the function. There I use it as a pointer to char. Then I'm passing argv[x] which should be used as a pointer, not double one. I think that's correct?

What are you talking about, FILE **file or char *filename.

gerard4143 371 Nearly a Posting Maven

I tried to simplify your code and here's what I arrived at...I got your function working now. Please note, you don't have to allocate the memory for an array that already exists.
You should be able to take this and complete the rest.

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

void get_days_in_month(int days[], int year);

int main(int argc, char **argv) {
	
	int nFields, i;
	int year = 0 ;
	int days[12] ;
	char *months[12] = {"January", "February", "March", "April", "May", 
		"June", "July", "August", "September", "October", "November", 
		"December"} ;
	
	
	/* Get user year */
	printf("Enter a year (ex. 1997): " ) ;
	nFields = scanf("%d", &year) ;
	
	/* Verify letters were not entered */
	if (nFields == 0) {
		printf("Incorrect Input\n") ;
		return 0 ;
	}
	
	/* Call function */
	get_days_in_month(days, year) ;
	
	/* Print months with # of days */
	printf("Number of days in each month: \n") ;
	for (i = 0 ; i < 12 ; i++) 
	{
		
		printf("%10s: %2d\n", months[i], days[i]) ;
		
	}
	
    return EXIT_SUCCESS;
}

void get_days_in_month(int days[], int year) 
{
	days[0] = 31 ;
	days[2] = 31 ;
	days[3] = 30 ;
	days[4] = 31 ;
	days[5] = 30 ;
	days[6] = 31 ;
	days[7] = 31 ;
	days[8] = 30 ;
	days[9] = 31 ;
	days[10] = 30 ;
	days[11] = 31 ;
}
gerard4143 371 Nearly a Posting Maven

It works. I think now I know that double pointers aren't needed.

Are you sure about that..Try run the code without double pointers.

gerard4143 371 Nearly a Posting Maven

If you want to find out if this works...write and read some data to you files.

gerard4143 371 Nearly a Posting Maven

Not really sure what the question is...but you functions takes a character not a string.

gerard4143 371 Nearly a Posting Maven

Yep this is the C section. Thought I was lost for a second with all those cout's and iostreams.

gerard4143 371 Nearly a Posting Maven

Are you really sure you want your while to read like below? Maybe you mean while (true) or while(1)

while ('\n')
gerard4143 371 Nearly a Posting Maven

Hi I,ve recently joined daniweb and was wondering if you guys might help me in writing a shellscript that automatically detects whether a flashdrive has been inserted into a usb port and recursively searches all folders deleting any .exe files it encounters.
Thanx.

A question...What operating system?

gerard4143 371 Nearly a Posting Maven

sorry to post this stupid question here. I'm just learning Assembler and a problem from my book is: input: a string from a user. output:character in that string. for example: input: DANI so output is:
D
A
N
I
I just can received a string from user.
thanks. :)

Can we what you have so far?

gerard4143 371 Nearly a Posting Maven

Well I tried and....the number of errors and warning...oh my.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

void *A(void *arg);
void *B(void *arg);
void *C(void *arg);

int main()
{
	int res, total=0;
	pthread_t b_thread, e_thread, s_thread;
	void *thread_result;

	res = sem_init(&A,B,C 0, 0);

	if(res != 0)
	{
		perror("Semaphore initialization failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&b_thread, A,NULL);

	if(res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&s_thread, NULL, B, NULL);
	
	if(res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&s_thread, NULL, C, NULL);

	if(res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	and(getpid());

	while(total <12)
	{
		sleep(rand()%3 + 1);
		sem_post(&A);
		printf("A ready\n");
		total++;
	}

	printf("\nWaiting for thread to finish...\n");

	res = pthread_join(b_thread, &thread_result);

	if(res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}

	printf("Thread joined\n");

	res = pthread_join(s_thread, &thread_result);

	if(res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}

	printf("Thread joined\n");
	sem_destroy(&B);
	exit(EXIT_SUCCESS);
}

void *A(void *arg)
{
	int i;
	srand(time(NULL));

	for(i=0; i<7; i++)
	{
		sem_wait(&B);
		sleep(rand()%4+1);
		printf("A prepared.\n");
	}

	pthread_exit(NULL);
}

void *B(void *arg)
{
	int i;
	srand(time(NULL));

	for(i=0; i<5; i++)
	{
		sem_wait(&A);
		sleep(rand()%3+1);
		printf("B prepared.\n");
	}

	pthread_exit(NULL);
}

void *C(void *arg)
{
	int i;
	srand(time(NULL));

	for(i=0; i<5; i++)
	{
		sem_wait(&A);
		sleep(rand()%3+1);
		printf("C prepared.\n");
	}
	pthread_exit(NULL);
}

gcc test.c -Wall -ansi -pedantic -o test -lpthread

test.c: In function ‘main’:
test.c:22: error: expected %<)%> before numeric constant
test.c:22: warning: passing argument 1 of ‘sem_init’ from incompatible pointer type
/usr/include/semaphore.h:37: note: expected ‘union sem_t *’ but argument is of type ‘void …

gerard4143 371 Nearly a Posting Maven

in linux installation, which of these options(Advanced,Expert,Selective,Custom) allows you to select only the packages needed to suit your computing needs?

Which distro offers this selection?

gerard4143 371 Nearly a Posting Maven

Like most Linux users, I have tried my share of distros and always have come back to Mandriva/Gnome. It's just stable and quick..

gerard4143 371 Nearly a Posting Maven

Hence, what would be your recommendations and why? Keep in mind that I need Linux for work, thus, seeking for stability and speed over fun and gloom.

What does that mean 'I need Linux for work'? Do we interpret this as - my Linux must be Debian based or a derivative?

gerard4143 371 Nearly a Posting Maven

If the compiler will generate the proper machine codes and the linker will produce the proper addresses then yes it will produce executable code....The only remaining obstacle is wrapping the executable code in a format recognized by the operating system...

gerard4143 371 Nearly a Posting Maven

It doesn't matter where you initialize the mutex...Just do it before you read or write to the shared resource and then set/unset(lock/unlock) the mutex with each read and write.

gerard4143 371 Nearly a Posting Maven

If you check the manpages for strftime, you'll find a complete and working program at the bottom of the listing..

gerard4143 371 Nearly a Posting Maven

I know compiling a C program with mysql in Linux is not standard. To give you an example here's a makefile

CFG=/usr/bin/mysql_config

test: test.c
	gcc test.c -o test `$(CFG) --cflags` `$(CFG) --libs`

Notice the CFG=/usr/bin/mysql_config

You should check on compiling instructions for your operating system.

gerard4143 371 Nearly a Posting Maven

Hello, I have been trying to setup mysql API with C. I followed tutorials but I don't think I am installing the API correctly.

Could someone tell me the package to download, and what to do with it. Thank you very much.

On this page you should find a link called Connector/C (libmysql)
A client library for C development.

http://dev.mysql.com/downloads/

Note if your trying to install this on a Linux box then use the appropriate package manager..

gerard4143 371 Nearly a Posting Maven

I didn't try it because by the looks of it, the code continually prints out a fixed stream of 80 bytes until the end of the file, so what's to prevent it from printing past the end of a file? Maybe you should stop ignoring what other people say, ie substituting in "blah blah" for my posts.

You should read up on basic I/O. Here's a brief description of fread, fwrite return values from my manpages

RETURN VALUE
fread() and fwrite() return the number of items successfully read or
written (i.e., not the number of characters). If an error occurs, or
the end-of-file is reached, the return value is a short item count (or
zero).

fread() does not distinguish between end-of-file and error, and callers
must use feof(3) and ferror(3) to determine which occurred.

If you don't understand the above quote then get yourself a good intro book on C.