gerard4143 371 Nearly a Posting Maven

Now you have to check the characters of each line...checking for things like if, while, do and then check the last printable character to see if it is ';'.

gerard4143 371 Nearly a Posting Maven

A few format pointers, don't comment standard C functions. We know what they are and how they work or we can readily find information on them.

Here's how I would start this code

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

#define CHAR_SET 3

int main()
{
	int i = 0, j = 0;
	char string1[40], temp[CHAR_SET + 1];
	temp[CHAR_SET] = '\0';

	printf("Enter in a string: ");

	fgets(string1, 40, stdin);

	for(i = 0; i < strlen(string1) - CHAR_SET; i++)
	{
		for(j = 0; j <  CHAR_SET; j++)
		{
			temp[j] = string1[i + j];
		}
		

		fprintf(stdout, "char set->%s\n", temp);

		/*now compare the set with the remainder of the string*/

	}
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Never use gets, use fgets instead. From my help file

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

Could you explain what the second function does.

gerard4143 371 Nearly a Posting Maven

When you pass a character array to scanf, you don't have to use the address of operator because the array name is the address of the array..Also the main function should be defined returning an int and i is used uninitialized on line line 41.

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

struct Date {
            short Jour;
            short Mois;
            short Annee;
            };
struct Ouvriers {
            char Genre;
            long NumRegistre;
            char Nom[25];
            char Prenom[30];
            char Rue[50];
            int CodePostal;
            char Ville[20];
            char Specialite[20];
            struct Date DateNaiss;
            struct Date DateEngag;
                };
void AfficheOuvri(struct Ouvriers[], int);
int EncodeOuvri(struct Ouvriers[], int);
int main ()
{
    struct Ouvriers Personne[100];
    int nOuvrier = 0;
    int nFemme;
    int nHomme;
    int Out;
    int Ret;
    int i;
    long NumReg[100];
    int Index[100];
    char Data[100][56];

    do
    {
        nOuvrier = 0;
        Out = 0;
        Ret = EncodeOuvri(Personne, i);
        if(Ret == 0)
        {
            Out = 1;
        }
        nOuvrier++;
    }while(Out != 1);


    for(i = 0; i < nOuvrier; i++)
    {
        AfficheOuvri(Personne, i);
    }

return 0;

}
int EncodeOuvri(struct Ouvriers Personne[], int i)
{
    int Etat = 0;

    printf("\n\nEncoder le genre: ");
    fflush(stdin);
    Personne->Genre=getchar();
    if(Personne->Genre != '\n')
    {
        printf("\nEncoder le numero de registre: ");
        fflush(stdin);
        scanf("%ld", &Personne->NumRegistre);
        printf("\nEntrez votre nom: ");
        fflush(stdin);
        scanf("%s", Personne->Nom);
        printf("\nEntrez votre prenom: ");
        fflush(stdin);
        scanf("%s", Personne->Prenom);
        printf("\nNom de rue: ");
        fflush(stdin);
        scanf("%s", Personne->Rue);
        printf("\nCode postal: ");
        fflush(stdin);
        scanf("%d", &Personne->CodePostal);
        printf("\nVille: ");
        fflush(stdin);
        scanf("%s", Personne->Ville);
        printf("\nSpecialite: ");
        printf("\n\t1. Coffreur");
        printf("\n\t2. Ferrailleur");
        printf("\n\t3. Couvreur");
        printf("\n\t4. Menuisier");
        printf("\n\t5. Macon");
        printf("\n\t6. Manoeuvre");
        printf("\n\t7. Grutier\n");
        printf("Votre choix: ");
        fflush(stdin);
        scanf("%s", Personne->Specialite);
        printf("\n\n\n");
        Etat = 1;
    } …
gerard4143 371 Nearly a Posting Maven

I would use an if statement

if (some condition)
{

}
else if (some other condition)
{

}
else if (more conditions)
{

}
else
{

}
gerard4143 371 Nearly a Posting Maven

You can't us variables with case...It must be a integral constant.

switch ( expression )
case constant-expression : statement
[default : statement]

gerard4143 371 Nearly a Posting Maven

In the wise words of - Lippman/Koenig and Moo don't assume anything. Write your header files uncomplicated...don't assume an environment that may not exist.

gerard4143 371 Nearly a Posting Maven

What do you mean by that?

Header files shouldn't assume anything. They should be simple and plain...without any preconceptions...we can't assume that the programmer will state 'using namespace std'.

Please read Accelerated C++ or Ruminations on C++ or C++ Primer...All these books state, that header files should be unqualified.

jonsca commented: Agreed +6
gerard4143 371 Nearly a Posting Maven

Okay, to use

vector<int> v1;

without the need of the preceding std::, you need to include the

using namespace std;

at the beginning of the header file, else std:: will be required.

Well its O.K. if its not in a header file where we can assume things like the user will use things like 'using namespace std;'

gerard4143 371 Nearly a Posting Maven

Yeah, well that tells use nothing. Please post the current code and error and warning
messages.

gerard4143 371 Nearly a Posting Maven

Try rewriting like so

for(temp=hl;(temp!=NULL) || (bOver1==true); temp=temp->next)
gerard4143 371 Nearly a Posting Maven

Try

std::vector<int> v1;
gerard4143 371 Nearly a Posting Maven

Could we see the definitions of the classes in your dynamic arrays?

gerard4143 371 Nearly a Posting Maven

Use getline().

Here's an example of usage.

http://www.cplusplus.com/reference/string/getline/

gerard4143 371 Nearly a Posting Maven

create your case like this

case '1':
{
//put code here
}

Use braces so you can define variables in your case.

gerard4143 371 Nearly a Posting Maven

To print the table use a nested for statement...one for statement for each dimension.

gerard4143 371 Nearly a Posting Maven

But does that allow me to make it Bydimensional? I cannot see it... I mean:
I cannot print it as I want.

You can make the array as many dimensions as you like but remember that each element contains a structure that has both an int x and a int y. For printing try..Note for the demonstration I'll use a two dimensional example.

std::cout << "x->" << the_s[1][1]->x << " y->" << the_s[1][1]->y << std::endl;
gerard4143 371 Nearly a Posting Maven

Store the data in a structure

struct mys
{
unsigned int x;
unsigned int y;
};

struct mys the_s[10] = {{1, 2}, {3, 4}, {5, 6}, ...};
gerard4143 371 Nearly a Posting Maven

what about getting rid of the 0's is that possible that's what i have been asking?

I'm not sure what you mean by 'get rid of', your array is defined as a 10 x 12 array. If you require an array that is only known at run time them you'll have to explore the new and delete operators or possibly a vector.

gerard4143 371 Nearly a Posting Maven

Again, I ran the program you posted with the data you posted and it worked.

Here's my output...

20 24 32 35 40 50 50 45 40 35 30 25
30 38 40 45 50 60 60 55 45 40 35 32
60 60 55 50 45 40 40 40 45 50 55 60
45 45 40 30 25 20 15 20 25 30 40 45
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0

gerard4143 371 Nearly a Posting Maven

I just ran your program and it worked...What are you using for data in your data01.dat file?

gerard4143 371 Nearly a Posting Maven

Try changing the code like

for(int i=0; i<records && inFile; i++)
		{
			for(int j=0; j<month; j++)
			{
				int ans = 0;
				inFile >> ans;
				
				if (!inFile) break;
			
				monthArray[i][j] = ans;
			}
		}
gerard4143 371 Nearly a Posting Maven

Try something like

for(int i=0; i<records && inFile; i++)
		{
			for(int j=0; j<month && inFile; j++)
			{
				inFile >> monthArray[i][j];
			}
		}
gerard4143 371 Nearly a Posting Maven

in C, the keyword const doesn't exist, but it does in C++.

#define is used often in C to declare a name for a value that doesn't ever change throughout the execution of the program. This makes it easier to change values that are used over and over again.

Please read this link

http://tigcc.ticalc.org/doc/keywords.html

gerard4143 371 Nearly a Posting Maven

i understood my mistakes...thanks gerrard...although in your code...
when starting a new thread the value of t passed is always 2 and never 1...

where it should say Thread 1 started..it says Thread received 2 Thread received 2
instead of Thread received 1 Thread received 2

how is that ?

Your passing the address of t not its value, so when you change t to two its reflected in both threads.

gerard4143 371 Nearly a Posting Maven

I cleaned your code up a little...well a lot

#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<semaphore.h>
#include<unistd.h>

pthread_t t1,t2;
pthread_attr_t tattr;
int counter;
sem_t mutex;

void* runner(void *arg)
{
	int i = 0;

	printf("Thread received %i\n", *(int*)arg);

	for(i = 0; i < 5; i++)
	{
		sem_wait(&mutex);
		printf("Thread %i says %i\n",*((int *)arg),counter);
		counter++;
		sem_post(&mutex);
		sleep(1);
	}
	return (void*)NULL;
}

int main()
{
	int r;
	int t = 1;

	printf("hi");
	r = sem_init(&mutex,0,1);
	r = pthread_attr_init(&tattr);

	r = pthread_create(&t1,&tattr,runner,(void *)&t);

	t = 2;

	r = pthread_create(&t2,&tattr,runner,(void *)&t);

	printf("Threads initialised");
	r = pthread_join(t1,NULL);
	r = pthread_join(t2,NULL);
	r = sem_destroy(&mutex);

	return 0;
}

P.S.

This is a pet peeve of mine

r=sem_destroy(&mutex);

It makes the code impossible to read, please consider a more readable format like below.

r = sem_destroy(&mutex);

compile line:
gcc test.c -Wall -ansi -pedantic -o test -pthread

gerard4143 371 Nearly a Posting Maven

Well the member function 'get_gas' requires a double..

void Car::get_gas(double fuel_level)
gerard4143 371 Nearly a Posting Maven

I have a constant value that is inside of the brackets (just forgot to type it in the example) if that is what you are talking about, otherwise please enlighten me.

Maybe you can enlighten me as to how we(Daniweb) can diagnose a problem if the code is not complete.

gerard4143 371 Nearly a Posting Maven

You realize this is wrong

char* array [];

This is taken from your original posting.

gerard4143 371 Nearly a Posting Maven

Why are you doing it this way? Try sorting the values and then print them.

gerard4143 371 Nearly a Posting Maven

Try something like below

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

char data[] = "Hello, World!";
const char *delimiters = " ";  

void function (char* array [])
{
	array[0] = strtok (data, delimiters);

	fprintf(stdout, "first string->%s\n", array[0]);

	array[1] = strtok(NULL, delimiters);

	fprintf(stdout, "second string->%s\n", array[1]);
}

int main () 
{
	char* array[2];
	function(array);

	fprintf(stdout, "first string->%s\n", array[0]);

	fprintf(stdout, "second string->%s\n", array[1]);

	return 0;
}
gerard4143 371 Nearly a Posting Maven

Its called forward declaration and its used so the compiler has an idea what player1 is.

int player1(void);

This is all the information the compiler needs to generate a function call to player1.

gerard4143 371 Nearly a Posting Maven

Put this line before the function player2

int player1(void);
gerard4143 371 Nearly a Posting Maven

You really should use a library like XDR(external data representation) to pass data across a network. It'll ensure that the network byte order is maintained.

gerard4143 371 Nearly a Posting Maven

Are constants, defined by using the keyword const , external linkage or internal linkage?
Can constants be modified using pointers?
What about constants defined using by #define?

Can constants be modified? It really depends on what you mean by modified. Does that imply the use of memory management functions?

gerard4143 371 Nearly a Posting Maven

Try this for Main line 13.

testHand[0].addCard(testDeck.dealCard());
gerard4143 371 Nearly a Posting Maven

First, you'll have to create a < operator for your class and then you'll have to create a function that compares two objects of that class using the < operator and then call the sort algorithm

sort(your_vector.begin(), your_vector.end(), sort_function);

Here's a simple example

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

class myint
{

public:

    myint(int val):itsvalue(val) {}

    bool operator <(const myint & m) const { return itsvalue < m.itsvalue; }

    int getitsvalue() const { return itsvalue; }

private:

    int itsvalue;

};

bool myint_compare(const myint & a, const myint & b)
{
    return a < b;
}

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

int main()
{
    std::vector<myint> the_ints;

    for (int i = 8; i > 0; --i)
        the_ints.push_back(i);

   for (int i = 0; i < 8; ++i)
           std::cout << the_ints[i] << std::endl;

   sort(the_ints.begin(), the_ints.end(), myint_compare);

   std::endl(std::cout);

   for (int i = 0; i < 8; ++i)
           std::cout << the_ints[i] << std::endl;

    return 0;
}
gerard4143 371 Nearly a Posting Maven

Before you concern yourself with averages I would get your while loop working, as it is you read the last grade twice.

Try changing your loop condition to something like...

while( fscanf(infile, "%d", &numA) != EOF)
gerard4143 371 Nearly a Posting Maven

I see a problem right away

char list[9][31];
char sorted[9][31];
//--------Main Program Body----------
cout << "****Alphebetical Sorting Program***"<<endl<<endl;
cout<<"please enter 10 strings you wish to sort"<<endl;

Your arrays are 9 X 31 and should be 10 X 31.

beejay321 commented: wrong comment, i did that and it messed it up, i did what i had originally and it was fine +0
gerard4143 371 Nearly a Posting Maven

Firstly, on line 68

newloc=malloc(sizeof(struct bstree *));

your allocating memory for the size of a pointer not the size of struct bstree..It should be

newloc=malloc(sizeof(struct bstree));
gerard4143 371 Nearly a Posting Maven

Try adding this

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double root1,	root2, a, b, c,	root;

	cout << "Enter the coefficients a, b, c: ";
	cin  >> a >> b >> c;

	root = sqrt (b*b - 4.0*a*c);
	root1 =	.5*(root - b) / a;
	root2 =	-.5*(root - b) / a;
	cout << "The solutions are: " << root1  << " and " << root2 << "\n";

	cin >> b;

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

ravenous beat me to the answer.

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

int main(int argc, const char **argv)
{
	char ch[] = "12";

	int ans = atoi(ch);

	fprintf(stdout, "ans->%d\n", ans);
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Ok got it now. But why do i need to wrap another pointer around it?

If you think about what happens when you call a function, it makes sense.

When you call your function the first thing it does is create temporary variables for the function arguments then it copies the values into to it...

So if we have a function like so

void myfunc(int * i);

and have a int pointer like so

int *iptr = NULL;

When we call myfunc(iptr) we'll create a temporary variable for the parameter i and copy the contents of iptr into it. Notice we copied NULL into the temporary and iptr still equals NULL.

Now lets look at my scenario where I define my function like so

void myfunc(int **i);

and we'll use the same pointer

int *iptr = NULL;

Now when I call the function I pass the address of iptr which is copied into the temporary(myfunc(&iptr)) so we can access the original pointer value with simple dereferencing.

gerard4143 371 Nearly a Posting Maven

Right from my help files.

int atoi(const char *nptr);

The atoi() function converts the initial portion of the string pointed
to by nptr to int.

atoi accepts a c-string not a char.

gerard4143 371 Nearly a Posting Maven

What is 'some_char'?

gerard4143 371 Nearly a Posting Maven

This doesnt even compile-.-

That's odd, it compiles on my system without a warning or error. It also runs correctly.

I'm using

gcc (GCC) 4.4.3

gerard4143 371 Nearly a Posting Maven

Try doing it like below

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

typedef struct mystruct
{
    int a;
}mytype;

mytype* createMytype(int x)
{
    mytype *mt;

    mt = (mytype*)calloc(1, sizeof(mytype));

    mt->a = x;

    return mt;
}

int foo(mytype** mt)
{
    *mt = createMytype(6);
    return 0;
}

int bar(mytype** mt)
{
    printf("%d\n", (*mt)->a);
    return 0;
}

int main(int argc, const char **argv)
{
    mytype *mt = NULL;

    foo(&mt);
    bar(&mt);
    exit(0);
}