Jump to content

help a NOOB WITH C

crocski

Ive been at this since I started the code.

 

the code is a simple project management for an assignment and I wanted to act a bit extra. 

Though I am not able to figure how its done.

 

I want to have an primary key or entry number for my entries.

typedef struct{char entryname [50];}project;int numprojects // global variablevoid functionadd (void){printf(" ID NO %d\n", numprojects++); // this is the closest i've gotten; this worked for incrementing after the entry is saved.}void printentry (int x){if (x!= -1){printf("ID NO %d", numprojects); // when its prints- its prints a random number like 6 pair instead of 1 which is starting numberprintf (" ENTRY NAME : ", project[x].entryname); // for effect }}

every this I save the entries, though they all increment as they should. When I call to print to the entries, its starts at 4 or 8 instead of 1.

 

I hope this was clear enough. 

Dont might my variables, they're added for effect.

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

A class?

Is this in uni or extracurricular?

I want to learn basic code, so pray tell sir.

Someone told Luke and Linus at CES 2017 to "Unban the legend known as Jerakl" and that's about all I've got going for me. (It didn't work)

 

Link to comment
Share on other sites

Link to post
Share on other sites

A class?

Is this in uni or extracurricular?

I want to learn basic code, so pray tell sir.

extracurricular.

 

i dont know if you're being sarcastic about learning code. I do have a few sources to start.

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

extracurricular.

 

i dont know if you're being sarcastic about learning code. I do have a few sources to start.

Not at all. I'm sarcastic about a lot of things, but not when it comes to things I am actually interested in.

Someone told Luke and Linus at CES 2017 to "Unban the legend known as Jerakl" and that's about all I've got going for me. (It didn't work)

 

Link to comment
Share on other sites

Link to post
Share on other sites

printf (" ENTRY NAME : ", entryname); // for effect 

Don't know if it will fix the problem but you don't have any format specifiers in that string.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Ive been at this since I started the code.

 

the code is a simple project management for an assignment and I wanted to act a bit extra. 

Though I am not able to figure how its done.

 

I want to have an primary key or entry number for my entries.

int numprojects=1 // global variablevoid functionadd (void){printf(" ID NO %d\n", numprojects++); // this is the closest i've gotten; this worked for incrementing after the entry is saved.}void printentry (void){printf("ID NO %d", numprojects); // when its prints- its prints a random number like 6 pair instead of 1 which is starting numberprintf (" ENTRY NAME : ", entryname); // for effect }

every this I save the entries, though they all increment as they should. When I call to print to the entries, its starts at 4 or 8 instead of 1.

 

I hope this was clear enough. 

Dont might my variables, they're added for effect.

You could use a for loop to display the id instead and simply print the loop number as the id.

 

So for example:

for (int i = 0; i < //enter max entry number here ; i++){printf ("ID: ", i);printf (" ENTRY NAME : ", entryname);}

Although this might loop 'entryname' depending how you stored your entries. Sorry if the syntax is wrong in this.

Link to comment
Share on other sites

Link to post
Share on other sites

Not at all. I'm sarcastic about a lot of things, but not when it comes to things I am actually interested in.

I started coding along time ago, but I have never taken it serious until recently. I realised that I cant wait for anyone to teach me; especially where I live, its not a common field.

 

Youtube- newboston- just to get an understanding of syntax; dont expect to learn anything other then how to layout code.

http://linustechtips.com/main/topic/14904-ultimate-programming-resources-thread/- My name is between there somewhere. lol

Stackoverflow

cplusplus

coursera- something everyone should look into. its FREE.

 

 

google is your friend really. There are some books out there. If you are a reader, go for it.  youtube and that lynda site are good resources. 

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

Use a struct to store the entry name and number together in a list.

 

A basic example

#define MAX_ENTRIES 20#define MAX_ENTRY_LEN 30typedef struct  {	int     m_entryNum;	char    m_entryName[MAX_ENTRY_LEN];} Entry_t;size_t	totalEntries = 0;Entry_t entryList[MAX_ENTRIES];

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

 

Use a struct to store the entry name and number together in a list.

 

A basic example

#define MAX_ENTRIES 20#define MAX_ENTRY_LEN 30typedef struct  {	int     m_entryNum;	char    m_entryName[MAX_ENTRY_LEN];} Entry_t;size_t	totalEntries = 0;Entry_t entryList[MAX_ENTRIES];

storing the entries isn't the problem. I just need a primary key or entries number added to the entry

 

 

for eg.

 

ID NO: 1

 

Entry Name: Carvinal

 

---------------------------------

 

ID NO: 2

 

Entry Name: Soca

 

 

thats the format I am looking for.

 

 

 

Note that I tried struct already. Now I am trying a global variable for simplicity.

Edited by crocski

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

storing the entries isn't the problem

i think it is

you just need to store the entries in an array

the ID will be the array index

Link to comment
Share on other sites

Link to post
Share on other sites

i think it is

you just need to store the entries in an array

the ID will be the array index

i'll try it again then, that's where it was originally.  :mellow:

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

i'll try it again then, that's where it was originally. :mellow:

storing data in a data structure (e.g. array) tipocally implies that there will be some sort of identifier that will allow you to retrieve a single specific entry from the list

in arrays, this is the index

you need an array of strings, which is a matrix of chars

char[MAX_ENTRIES][ENTRY_LENGTH];
Link to comment
Share on other sites

Link to post
Share on other sites

storing data in a data structure (e.g. array) tipocally implies that there will be some sort of identifier that will allow you to retrieve a single specific entry from the list

in arrays, this is the index

you need an array of strings, which is a matrix of chars

char[MAX_ENTRIES][ENTRY_LENGTH];

Do we really need a 2d array for this? I have no experiences with them at all. 

 

I feel like we are complicating a simple matter. correct me if I'm wrong of course.

 

All I want to do is print a number and reprint it.

 

 

 

BTW I tried the array-again; now none of my inputs are coming back, -_-  at least they were printing correctly. Just not with the right ID NO.

Might scrap the whole No thing.

Though its killing me.  :(

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

Edited the code above to mirror a bit of what I actual have.

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

Edited the code above to mirror a bit of what I actual have.

Try this:

string entries[49]int p=50;printf("Please enter your number followed by the enter key");while (p != 0){printf("Please enter the next number");scanf ("%s", entries);p--;}for (int i = 0; i < 50 ; i++){printf ("ID: ", i);printf (" ENTRY NAME : ", entries[i]);}

Let me know if it throws any errors.

Link to comment
Share on other sites

Link to post
Share on other sites

Try this:

char entries[49];int p=50;printf("Please enter your number followed by the enter key");while (p != 0){printf("Please enter the next number");scanf ("%s", entries);p--;}int i;for (i = 0; i < 50 ; i++){printf ("ID: ", i);printf (" ENTRY NAME : ", entries[i]);}

Let me know if it throws any errors.

string need to but changed to char. ; after entries.

 

also 

this message

 

|error: 'for' loop initial declarations are only allowed in C99 mode

 

i have never seen this so I dont know what to do.lol

 

 

 

EDIT: FIXED

Edited by crocski

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

string need to but changed to char. ; after entries.

 

also 

this message

 

|error: 'for' loop initial declarations are only allowed in C99 mode

 

i have never seen this so I dont know what to do.lol

 

 

 

EDIT: FIXED

Try this line for the for loop:

int i;for (i = 0; i < 50; i++)

Also "%s" should have a c not an s i think. What compiler are you using by the way?

Link to comment
Share on other sites

Link to post
Share on other sites

Try this line for the for loop:

int i;for (i = 0; i < 50; i++)

Also "%s" should have a c not an s i think. What compiler are you using by the way?

codeblocks

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

Do we really need a 2d array for this? I have no experiences with them at all. 

 

I feel like we are complicating a simple matter. correct me if I'm wrong of course.

 

All I want to do is print a number and reprint it.

if you want to print and reprint a series of names, you need to store the names

to store a series of things you need an array

but a string is an array itself, so you need an array of arrays, aka matrix

 

and yes it is needed, which is why this didn't work

Try this:

string entries[49]

.

.

.

this was C++, not C, and just changing "string" to "char" made it a single string (an array of characters = a string)

plus, in that code he forgot to put the array index in the reading loop

 

this will work

#include <stdio.h>#define MAX_ENTRIES 3;#define ENTRY_LENGTH 50;int main(){	char entries[MAX_ENTRIES][ENTRY_LENGTH];	int i;	for(i = 0; i < MAX_ENTRIES; i++)		scanf("%s", entries[i]);	for(i = 0; i < MAX_ENTRIES; i++)		printf("\nentry %d: %s", i + 1, entries[i]);	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Here's an example I put together demonstrating how to use a struct to hold the record data.

 

It prompts for a record name and then record id # then prints them back out when the user is finished.

 

You might find it helpful.  :)

 

 
#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAX_RECORDS 30#define MAX_NAME_LEN 20#define ERR_BAD_PTR -1#define NOERR       0/*    our struct for holding record data*/typedef struct{    long    m_recordId ;    char    m_recordName[ MAX_NAME_LEN ] ;} Record_t ;//tidiness typedeftypedef Record_t* RecordPtr ;voidflush( void ){    int c;    while( (c = getchar()) != '\n' && c != EOF );}intaddRecord( RecordPtr record ){    //if we're NULL, eject and let the caller know    if( !record )        return ERR_BAD_PTR ;    //Buffer for our input    char buf [ MAX_NAME_LEN ] ;    //force initialization to all 0    memset( &buf[0], 0, MAX_NAME_LEN ) ;    //if we are of size 0 or just a newline (user just hit enter) keep asking    while( buf[0] == '\0' || buf[0] == '\n' )    {        printf( "\nEnter record name: " ) ;        //get input from user        fgets ( &buf[0], MAX_NAME_LEN, stdin ) ;    }    //copy our buf into the record    strncpy( &(record->m_recordName[0]), &buf[0], MAX_NAME_LEN ) ;    //reset the buf and reuse it    memset( &buf[0], 0, MAX_NAME_LEN ) ;    //this pointer will tell us if strtol completely failed    char*   p = &buf[0] ;    //loop while strtol is unable to parse any numbers    while( p == &buf[0] )    {        printf( "\nEnter the record number: " ) ;        fgets ( &buf[0], MAX_NAME_LEN, stdin ) ;        //strtol will attempt to convert the entered characters in buf to a number.        //it will stop if a non-numeric character is encountered        record->m_recordId = strtol( &buf[0], &p, 10 ) ;    }    //all is good    return NOERR ;}intprintRecord( RecordPtr record ){    if( !record )        return ERR_BAD_PTR ;    printf( "\nRecord ID: %ld \nRecord name: %s \n", record->m_recordId, record->m_recordName ) ;    return NOERR ;}intgetRecords( RecordPtr recordList, size_t maxRecords ){    if( !recordList )        return ERR_BAD_PTR ;    size_t i = 0 ;    char add = 'y' ;    //quit looping if we reach maximum records or user aborts    for( ; i < maxRecords && (add != 'n' && add != 'N') ; i ++ )    {        int ret = addRecord( &recordList[i] ) ;        //if adding the record went OK, ask for another        if( ret == NOERR )        {            printf( "\nAdd another record (y/n)? " ) ;            add = getchar() ;   //in truth we actually only care if they enter n/N            flush();        }        //bad pointer, not really possible in this example        else if( ret == ERR_BAD_PTR )        {            printf( "\nNULL pointer at record %d\n", i ) ;            //return error            return ret ;        }    }    //we succeeded, return number of valid entries    return i ;}intmain( int argc, char** argv ){    //our list of records    Record_t records[ MAX_RECORDS ] ;    int r ;    r = getRecords( &records[0], MAX_RECORDS ) ;    if( r != ERR_BAD_PTR )    {        int i = 0 ;        //r is the number of records we entered, loop through them        for( ; i < r ; i ++ )        {            printf( "Printing record %d of %d \n", i+1, r ) ;            printRecord( &records[ i ] ) ;        }    }    return 0 ;}

 

Sample output

 


Enter record name: Carnival

Enter the record number: 409

Add another record (y/n)? y

Enter record name: Soca

Enter the record number: 333

Add another record (y/n)? n
Printing record 1 of 2

Record ID: 409
Record name: Carnival

Printing record 2 of 2

Record ID: 333
Record name: Soca

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Off-topic slightly:

useCamelCaseForVariablesAndFunctions. easiertoreadthanaunrecognizablestringofletters :)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Here's an example I put together demonstrating how to use a struct to hold the record data.

 

It prompts for a record name and then record id # then prints them back out when the user is finished.

 

You might find it helpful.  :)

 

 
#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAX_RECORDS 30#define MAX_NAME_LEN 20#define ERR_BAD_PTR -1#define NOERR       0/*    our struct for holding record data*/typedef struct{    long    m_recordId ;    char    m_recordName[ MAX_NAME_LEN ] ;} Record_t ;//tidiness typedeftypedef Record_t* RecordPtr ;voidflush( void ){    int c;    while( (c = getchar()) != '\n' && c != EOF );}intaddRecord( RecordPtr record ){    //if we're NULL, eject and let the caller know    if( !record )        return ERR_BAD_PTR ;    //Buffer for our input    char buf [ MAX_NAME_LEN ] ;    //force initialization to all 0    memset( &buf[0], 0, MAX_NAME_LEN ) ;    //if we are of size 0 or just a newline (user just hit enter) keep asking    while( buf[0] == '\0' || buf[0] == '\n' )    {        printf( "\nEnter record name: " ) ;        //get input from user        fgets ( &buf[0], MAX_NAME_LEN, stdin ) ;    }    //copy our buf into the record    strncpy( &(record->m_recordName[0]), &buf[0], MAX_NAME_LEN ) ;    //reset the buf and reuse it    memset( &buf[0], 0, MAX_NAME_LEN ) ;    //this pointer will tell us if strtol completely failed    char*   p = &buf[0] ;    //loop while strtol is unable to parse any numbers    while( p == &buf[0] )    {        printf( "\nEnter the record number: " ) ;        fgets ( &buf[0], MAX_NAME_LEN, stdin ) ;        //strtol will attempt to convert the entered characters in buf to a number.        //it will stop if a non-numeric character is encountered        record->m_recordId = strtol( &buf[0], &p, 10 ) ;    }    //all is good    return NOERR ;}intprintRecord( RecordPtr record ){    if( !record )        return ERR_BAD_PTR ;    printf( "\nRecord ID: %ld \nRecord name: %s \n", record->m_recordId, record->m_recordName ) ;    return NOERR ;}intgetRecords( RecordPtr recordList, size_t maxRecords ){    if( !recordList )        return ERR_BAD_PTR ;    size_t i = 0 ;    char add = 'y' ;    //quit looping if we reach maximum records or user aborts    for( ; i < maxRecords && (add != 'n' && add != 'N') ; i ++ )    {        int ret = addRecord( &recordList[i] ) ;        //if adding the record went OK, ask for another        if( ret == NOERR )        {            printf( "\nAdd another record (y/n)? " ) ;            add = getchar() ;   //in truth we actually only care if they enter n/N            flush();        }        //bad pointer, not really possible in this example        else if( ret == ERR_BAD_PTR )        {            printf( "\nNULL pointer at record %d\n", i ) ;            //return error            return ret ;        }    }    //we succeeded, return number of valid entries    return i ;}intmain( int argc, char** argv ){    //our list of records    Record_t records[ MAX_RECORDS ] ;    int r ;    r = getRecords( &records[0], MAX_RECORDS ) ;    if( r != ERR_BAD_PTR )    {        int i = 0 ;        //r is the number of records we entered, loop through them        for( ; i < r ; i ++ )        {            printf( "Printing record %d of %d \n", i+1, r ) ;            printRecord( &records[ i ] ) ;        }    }    return 0 ;}

 

Sample output

 

Enter record name: Carnival

Enter the record number: 409

Add another record (y/n)? y

Enter record name: Soca

Enter the record number: 333

Add another record (y/n)? n

Printing record 1 of 2

Record ID: 409

Record name: Carnival

Printing record 2 of 2

Record ID: 333

Record name: Soca

DUDE, thanks allot for your efforts, though it not what I was looking for. I am looking at your code as it is well explained and I've start to tamper with to full understand it and examine. Thanks for the breakdown; as I have seen allot of this syntax used but never knew what it does. So I appreciate it.

 

Off-topic slightly:

useCamelCaseForVariablesAndFunctions. easiertoreadthanaunrecognizablestringofletters :)

Lol

 

 

 

I think the initial solution posed wasn't explained properly and that was my fault.

Instead of having the user input a primary key.

My intend was to create an automatic number; between for example from (1 to 100).

1 being the starting point; while it increments for a new 'set of data'.

and prints it accordingly.

 

Sad to say I have not made progress with this.

I have been picking at it.

Ive been busy and kinda put it on the backburner

 

Thoughts?

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

I think the initial solution posed wasn't explained properly and that was my fault.

Instead of having the user input a primary key.

My intend was to create an automatic number; between for example from (1 to 100).

1 being the starting point; while it increments for a new 'set of data'.

and prints it accordingly.

 

Sad to say I have not made progress with this.

I have been picking at it.

Ive been busy and kinda put it on the backburner

 

Thoughts?

 

#include <stdio.h>#define MAX_ENTRIES 3;#define ENTRY_LENGTH 50;int main(){	char entries[MAX_ENTRIES][ENTRY_LENGTH];	int i;	for(i = 0; i < MAX_ENTRIES; i++)		scanf("%s", entries[i]);	for(i = 0; i < MAX_ENTRIES; i++)		printf("\nentry %d: %s", i + 1, entries[i]);	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

 

I have define maxprojects already.

 

do you mind if I share the entire code.

 

 

Also (unrelated) I have realised that when I am inputting data; unless I use near all the array, it wouldn't print them in order.

 

For instance -I have been test the input field with my name which is 7 char long, the array is set to 30, when I am over 10 or higher on all the entries it prints them all back to me in the order I put them in.

 

Why is that?

 

 

ok I have published the code. it is over 400 lines long.

 

http://pastebin.com/XbdLK4yx

 

noted this code is the older version of what I am using; as I when back to see what I was doing wrong and near close to what I'll actually use.

 

hope for your feedback because I have no one close by that can assist.

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×