Sokurenko 42 Junior Poster

There are many ways to do this. What about the idea to use linked list and insert new names in correct place every time it is entered? Or even better would be binary tree. Or use some array of pointers to swap pointer values instead of struct values you can do it in this program too no?

Sokurenko 42 Junior Poster

@pooh1234qwerty
dont use questions like that, use printf to make shure :) make some test array to test and make it smaller maybe and if it will work in smaller it will work in bigger.
You wrote something , you should understand what you wrote, assumptions is not very good thing in programming and it's not about compiler.
To make code more readable for you and other people name variables to something meaningfull like Schol sayed i dont want to read code where something is l then something does s1 + s2 what is that and no comment
to normal human s1 doesent mean anything
hope this will help you to debug your code :) if no understand comment every line explay what it is and print results out till you have something that you cant explayn and it doesent work

Sokurenko 42 Junior Poster

alternative is malloc
you will locate it on heap instead of stack or calloc effect is same bot you haveto free it after and it can be dinamically changed during execution :)

Ancient Dragon commented: right answer :) +14
Sokurenko 42 Junior Poster

i like visual studio at work cause it's easy to navigate big project, bot at home i usually use vim, gedit maybe kate. I use notepad++ too sometimes at work, it's great.

Sokurenko 42 Junior Poster

@rubberman
arent finite state machines just regular expressions ??

Sokurenko 42 Junior Poster

Most simple solution if you know max line length and max text length would be using fgetline then compare what you got to desired if it is it read next line data. with the same thing fgetline
or fread bot then you haveto parse chunks which would be more harder or read whole file to buffer and prase if it's size is limited

Sokurenko 42 Junior Poster

See, if it confuses you so much then maybe separate calculation and printing, and print result variable only.
Also can write function to calculate

Sokurenko 42 Junior Poster

@hanananah also will work for original poster
you guys better use this one strtok

or if you know pointers good, you can locate it '|' with strchr if you have only one of these, if you have more you can ofcourse make it work too.

Or do it manually, going through array and searching position of '|' then taking whats after before | or 0

Sokurenko 42 Junior Poster

or maybe something like that to clear input buffer, if you have better solution please tell

/* getchar example : typewriter */
#include <stdio.h>

int main ()
{
  char c;
  int iNeededChars = 2;
  puts ("Enter text. Include a dot ('.') in a sentence to exit:");
  do
  {   
     c = getchar();//get needed char
     putchar (c);
     putchar('\n');
     while(c != '\n')
     {
         c = getchar();
         printf("Clearing [%c]", c);
     }
   iNeededChars--;
  }
  while(iNeededChars); 
  return 0;
}
Sokurenko 42 Junior Poster

yes qsort is good example as deceptikon told.
Bot it is not possible to pass only array or struct, you need to tell to a function howto compare those values.
For example in qsort for struct it could be a.data > b.data

Sokurenko 42 Junior Poster

enum or define of possible values and switch no ?

#include <stdio.h>

enum eGprsEnum
{
   GPRS_T = 3,
   GPRS_X = 1000,
};

int iDoStuff(int iId)
{
   int iRet = 0;

   switch(iId)
   {
   case GPRS_T:
      printf("%d",iId);
      break;
   case GPRS_X:
      break;
   default:
      iRet = -1;
      break;
   }

   return iRet;
}

int main()
{
   iDoStuff(3);
   return 0;
}
Sokurenko 42 Junior Poster

use getchar instead of fgets Click Here
your face is 2 bytes, you read 3 in it

Sokurenko 42 Junior Poster

Would be interesting to see example of what you are doing :)
I dont understand how to do it, just use bitwise logic operators and if else ?
I remember from school we had special graphical program where you can move and connect all that stuff to simulate this.

Sokurenko 42 Junior Poster

there is answer on the stack Click Here

Sokurenko 42 Junior Poster

if it is binary value stored then you can read first 8 bytes in float variable and second 8 bytes in float variable i just dont see where you open different files or i dont understand

Sokurenko 42 Junior Poster

@abrarsyed
your program is really sick, bot you did great job if it works, you might learn allot from it :)

Sokurenko 42 Junior Poster

The moust simple way is that you can have max like 300 accounts(300 structs) and 100 users(structs)
2 banks structs then you link them somehow together this does not need to be effective way, cause data is very small. See for example to be easy you can store 3 account struct in the user and users can be stored in some bank struct it all depends on the specification, how will you add data or delete, and search, as i see here i dont even understand what user is, it actually can be hard and effective which is meaningless here or easy. Or you can use 2 banks files where every line have user ,account count, then accounts and numbers and read them as getline.

It is all about imagination, try this it will be fun

Sokurenko 42 Junior Poster

@Trentacle
the value stays the same 1010 1011 if i use char or unsigned char. even if i use 171 or -85 does not matter, how is it ? the only value that is displayed is changed if i cast to char or unsigned char.

Yes this is about assigning values. You can look at them as byte that represent char or unsigned char no ?

There is bit that represent sign so char is not 8bit it is 9 ??

#include <stdio.h>

int main()
{
   char ucChar = -85;
   int iBit = 7;
   printf("[%d]",ucChar);
   printf("sign %c", (ucChar &(1 << 8)) ? '+' : '-' );
   while(iBit >= 0)
   {
      printf("%d", (ucChar &(1 << iBit)) ? 1 : 0 );
      if(iBit % 4 == 0)
      {
         putchar(' ');
      }
      iBit--;
   }
   puts("b");

   return 0;
}
Sokurenko 42 Junior Poster

look at Ascii table

all i whant to say that char is not int, it is one byte - 8 bites combination

#include <stdio.h>

int main()
{
   /*unsigned*/ char ucChar = 171;
   int iBit = 7;
   while(iBit >= 0)
   {
      printf("%d", (ucChar &(1 << iBit)) ? 1 : 0 );
      if(iBit % 4 == 0)
      {
         putchar(' ');
      }
      iBit--;
   }
   puts("b");

   return 0;
}
Sokurenko 42 Junior Poster

dont know, maybe you can play with this code i wrote using printf and figure it out, it will be fun

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
   int * piInteger = NULL;
   int iInteger = 0;
   void *vPointer = NULL;
   char *pcBuffer = {"xyz"};//3 bytes and null terminate
   printf("Value of pointer [%d]\n", piInteger);
   //printf("Seg faul cause points to null , go where points",*piInteger);
   printf("iInteger value is %d, bot address %x \n",iInteger,&iInteger);
   piInteger = &iInteger;//piInteger adress now gets adress of iInteger
   printf("was seg fault before, now %d\n",*piInteger);
   //bot also we can say differently
   memcpy(&iInteger, pcBuffer, sizeof(pcBuffer)) ;//we copy 4 byte buffer string to 4 byte char
   printf("new value of int %d bot piInteger points to it too look %d\n",iInteger, *piInteger);
   printf("treat it as address to null terminated char array %s and this one too %s",piInteger, &iInteger);
      vPointer = pcBuffer;
   printf("%s",vPointer);

   return 0;
}
Sokurenko 42 Junior Poster

@DeanMSands3

but your post is... not what the OP is asking for.

I passed by value, i did make copy, look link what it is, passing by value Click Here Please

yes i know the details are that i passed constant pointer to constant data, bot the result is exactly same from outside the function, except it stores data on heap and you can dinamically pass more or less data

Sokurenko 42 Junior Poster

`

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

void vIdoPrintArray()
{


}

void vPassByValue(const void * const vData, int iSizeOfElement, int iDataLength)
{
   void *vCopyData = NULL;
   vCopyData = malloc(iSizeOfElement * iDataLength);

   if(vCopyData != NULL)
   {
      int i = 0;
      memcpy(vCopyData, vData, iSizeOfElement * iDataLength);
      //printf("passed by value %s\n",vCopyData);
      while(i < iDataLength)
      {
         //printf("%d",*((int*)vCopyData + i ));
         printf("%d",((int*)vCopyData)[i]);
         i++;
      }
      memset(vCopyData, 0, iSizeOfElement * iDataLength);
   }

   free(vCopyData);
   return;
}



int main()
{
   int  pcBuffer[] = {1,2,3,4,5,6};

   //sprintf(pcBuffer,"%.12d",1234);

   vPassByValue(pcBuffer, sizeof(int), 6);
   //puts(pcBuffer);
   return 0;
}

Bot This could be best solution or best VAR_ARGSClick Here

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

void iPassByVal(char *pcBufferToWorkWith)
{
   puts(pcBufferToWorkWith);

   return;
}

int main()
{
   char pcBuffer[20] = {'W','o','r','k','\0'};
   char pcBufferCopyToPass[20] = {0};

   memcpy(pcBufferCopyToPass, pcBuffer, sizeof(pcBuffer));

   iPassByVal(pcBufferCopyToPass);

   return 0;
}

`

Sokurenko 42 Junior Poster

Another possible solution

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

void vPassByValue(const void * const vData, int iSizeOfData)
{
   void *vCopyData = NULL;

   vCopyData = malloc(iSizeOfData);
   if(vCopyData != NULL)
   {
      memcpy(vCopyData, vData, iSizeOfData);
      printf("passed by value %s\n",vCopyData);
      memset(vCopyData, 0, iSizeOfData);
   }
   free(vCopyData);

   return;
}

int main()
{
   char pcBuffer[20] = {0};

   sprintf(pcBuffer,"%.12d",1234);
   puts(pcBuffer);
   vPassByValue(pcBuffer, sizeof(pcBuffer));
   puts(pcBuffer);

   return 0;
}
Sokurenko 42 Junior Poster

dont use sizeof. do track memmory manually or use static buffers for now, and if everything works, try to change them

Sokurenko 42 Junior Poster

We don't do homework for people here. This is not a cheat site.

maybe it doesent matter if this guy who cheats cant explayn what he did or cheated

Sokurenko 42 Junior Poster
Sokurenko 42 Junior Poster

sorry didnt see that it was old post :( it showed to me at top :/

Sokurenko 42 Junior Poster

line 13 char* output1[100];

this would be pointer to pointer to string i suppose maybe you better make a copy of this string before saving pointer to it ?

works like that bot it still have mistakes(

    #include <stdio.h>
    #include <string.h>
    main (int argc, char *argv[])
    {
    FILE *f;
    int i=0;
    int j=0;
    char output[100];
    char* output1[100];
    char string[100];
    char delims1[] = " ";
    char delims2[] = "*";
    char* result = NULL;
    char* result3 = NULL;
    int num;
    //for (j=0; j<2; j++)
    //{
    //printf("%s",delims9[6]);
    //}
    f = fopen("text.txt","r");
    //
    while( fgets(string,sizeof(string),f) )
    {
    i=0;
    memset(output1,0,sizeof(output1));
    result = strtok( string, delims1 );
    while( result != NULL )
    {
    output1[i]=result;
    printf("%s\n",output1[i]);
    result = strtok( NULL, delims1 );
    i++;
    }
    for (num = 0; num < 100; num++ ) // correct would be num < i
    { // Error On thi
    if(output1 != NULL && output1[num] != NULL)
    printf("%s\n", output1[num]); //
    } //
    }
    printf("\n%d",i/3+1);
    return 0 ;
    }
Sokurenko 42 Junior Poster
#include <stdlib.h>
#include <stdio.h>
int main()
{
   int iToHex = 2346;

   printf("%x",iToHex);
   return 0;
}

outputs 92a or if you wish you can sprintf it somewhere

or use unsigned char for value < 255

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
   int iToHex = 255;
   unsigned char pcBuffer[sizeof(iToHex)] = {0};
   memcpy(pcBuffer,&iToHex,sizeof(iToHex));
   printf("[%x][%x][%x][%x]",pcBuffer[0],pcBuffer[1],pcBuffer[2],pcBuffer[3]);
   return 0;
}
Sokurenko 42 Junior Poster

by the way i reccoment you storing it in binary format 4 bytes, this means you memcpy integer to buffer and write this buffer to file, then memcpy those bytes back to integer

Sokurenko 42 Junior Poster

what do you know ? if you know that it is delimited with space before then read it from end of file till space or read some size and parse and then atoi look Maximum value for a variable of type int. 2147483647

for example i could fseek to end then fseek till space from end and read till end from this space this way you can even know how many bytes you will read

Sokurenko 42 Junior Poster

@Schol-R-LEA
I didnt mention when to do it in response to WaltP

Sokurenko 42 Junior Poster

comment out lines with errors, then slowly uncomment and look for errors
next time you write something, compile it every time you add something new while you are learning

Sokurenko 42 Junior Poster

@WaltP

Yeah, it's quite impossible after collecting all the letter counts to figure out how to combine the counts for 'a' and 'A'...
(i wish we still had the roll-eyes -- i like to mark sarcasm in case Shedon Cooper is reading this...)

didn't i just say that he needs to do it, where did i say it is not possible ? ?

except that he needs only small letters or only big letters in case he gets 3 'a' and 5'A'

Sokurenko 42 Junior Poster

@WaltP
wow, nice idea :) except that he needs only small letters or only big letters in case he gets 3 'a' and 5'A'

Sokurenko 42 Junior Poster

something like that could be design m?

#include "stdio.h"
#include "stdlib.h"
#define APLPABET_LEN 2//should be 25

struct stAlpha
{
   int iCount;
   char cChar;
};
enum
{
   CHAR_A = 0,
   CHAR_B = 1,//add needed indexes
};

void vPrintResultAlpha(stAlpha stAlphabet[])
{
   int i = 0;
   for (i; i < APLPABET_LEN; i++)
   {
      printf("char %c count %d\n",stAlphabet[i].cChar, stAlphabet[i].iCount);
   }
   printf("##############\n");
   return;
}
int main()
{
   stAlpha stAlphabet[APLPABET_LEN] = {{0, 'A'}, {0, 'B'}};//initialise 25 chars since we know alphabet
   int i = 0;

   vPrintResultAlpha(stAlphabet);

   stAlphabet[CHAR_B].iCount++;//found char 'b' or 'B' ?
   //check with if ()
   //{
   //}
   //else if()
   //{
   //}
   //else if()
   //{
   //}
   vPrintResultAlpha(stAlphabet);
   return 0;
}

or if you understand good you can read char x and use stAlphabet[x - 65].iCount++;
ofcourse check for needed range and toUpper then
you can refere to ascii table http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/images/ASCII1.GIF

you can read char by char by 1, or 50 then check this buffer or more
first try to printf what you need to check to see that you have needed data

Sokurenko 42 Junior Poster

check conn for NULL, maybe failed to connect ? or result is NULL cause nothing found

Sokurenko 42 Junior Poster

have you tryed this ? http://www.cplusplus.com/reference/clibrary/cstring/strtok/

while (tokenizer != NULL)
{
tokenizer = strtok(buffer,"\n");
//check if it is not NULL then strcpy else break;
strcpy(PackageSlips.PickUpAdd,tokenizer);
tokenizer = strtok(NULL,"\n");
strcpy(PackageSlips.DropOffAdd,tokenizer);
tokenizer = strtok(NULL,"\n");
strcpy(PackageSlips.ItemType,tokenizer);
tokenizer = strtok(NULL, "\n");
strcpy(PackageSlips.PriorAssign,tokenizer);
}
Sokurenko 42 Junior Poster

try this ? just check everywhere for NULL where possible :)

row = mysql_fetch_row(result)

while (row != NULL)
{
   for(i = 0; i < num_fields; i++)
   {
      //printf("%s ", row != NULL ? row[i] : "NULL");
      printf("%s", row[i]);
   }
   printf("\n");

   row = mysql_fetch_row(result)
}
Sokurenko 42 Junior Poster

same as for static, you use array index to access elements Click Here For Malloc this is dynamic array

Sokurenko 42 Junior Poster

you need to read about it, what it is and then construct it

Sokurenko 42 Junior Poster
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HOW_MANY_CHARS 2
int main()
{
   char *s = NULL;//does not point to allocated memmory

   s = (char*) malloc (sizeof(char) * HOW_MANY_CHARS);//create nice old string
   memset(s, 0, HOW_MANY_CHARS);//initialize it to zeroes

   gets(s);//though better to use getchar and check for overflow

   printf("[%s]\n",s);// print whole c string
   printf("[%c]\n",s[0]);//acces first char and print it

   free(s);

   return 0;
}
Sokurenko 42 Junior Poster

this is the link on howto do it

try to compile hello world program first maybe error is in your code ? cause char* ItemType; is pointer to memmory did you allocate some memmory for it ?

and you read to char lineRead; which is actually one byte long, you read there 120 bytes
it should be then char lineRead[120]

'Segmentation fault(core dumped)'. means memmory error

Sokurenko 42 Junior Poster

please exlayn how you calculated 4rd line ? from n/21 till n/5 why not other i mean if n is 21 then 21/21 is from 1 till 4.2 like O(n/5) dont know
i am interested in a result too

Sokurenko 42 Junior Poster
Sokurenko 42 Junior Poster
if(answerInput[count][i]=='.')
{
    printf("ERROR");
    break;
} 

or something like that is possible bot i didnt tested just a sketch

//read 
char c = getchar();
while(c!='.' && i<SIZE)
{
    answerInput[count][i++] = c;
c = getchar();// get next char then
}
answerInput[count][--i]='\0';
Sokurenko 42 Junior Poster

How does this create a huge security hole and reallocating the array by 1 for each input doesn't? Keep in mind the info we have been given by the OP, not making unfounded assumptions about what we haven't been told..

maybe if you check for array overflow and realloc error then no holes

WaltP commented: Yes, if you program properly, there will be no holes. +14
Sokurenko 42 Junior Poster

wouldnt the first one be O(n) or more precise O(n + 66) ?
second dont know it's just math something line n * 16 can exyst

Sokurenko 42 Junior Poster

dont haveto deal with zeroes in the front, you must sort only length with needed data, no need to sort whole array
just hold variable iLen look at bubble sort
if it crashes then malloc array dynamically at the start

Sokurenko 42 Junior Poster

have you considered java ?

WaltP commented: How does that help in passing from C to PHP??? +0