Thread: New to C programming...Need help :)

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Talking New to C programming...Need help :)

    Hi all, i'm 15yrs old, and VERY new to C programming...I have about 1 and a half days to learn enough C to make a list of keywords, which when typed in will give you a definition of the word...I am not asking anyone to write this for me, only asking if someone has any idea as to where i should start? Should i look at someone elses code? Or should i just grab a book and hope for the best? (I've been learning from a book but the code tutorials in there aren't very practical, and being so new to the language, i dono how to bend those commands to the task at hand) If anyone could help, i'd be very grateful...Thank you!!

    Talon

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Well, if you only have to do certain words you can read them in and use strcmp() to compare them to a list of words. Then when you find which ones match print out the correct definitions.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    I did some reading on those commands...And i'm not entirely sure if they will do what i need 'em to either :-\. I basically just want a list of words...And wanna assign each word a value...For instance, say my list consists of commonly used acronyms (online) so, LOL should equal 'LOL means, Laugh Out Loud, or Lots Of Luck' or something...Does that make sense? I'm rather scatterbrained..I have today and an hour or three tomorrow to finish this, so i'm kinda stressed.

    Talon

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    are you familiar with structures and linked listes? you can think about it.. having maybe a linked list containing a structure, every structure has an acronyms and its meanins, so when the user inserts a word, you have to look inside the list, then if one structure matches your word and print the meaning.

    The following is an easy example of a phoone book, it looks for number given a string (name)...you should do something like this but u have to return a string, given a string

    Code:
    //      MANUAL INSERIONT OF NAMES AND PHONE NUMBERS IN A LINKED LIST
    //      INCREMENTAL AND DECREMENTAL SORTING USING PHONE NUMERS
    //      PRINTING AND REINSERTING LINKED LIST'S VALUES
    //      SAVE\LOAD TO FILE
    
    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    
    
    // function declaration:
    void PrintNumbers();
    void InsertNumbers();
    void SortItems(int minore);
    void SortItemsbyName(int minore);
    void ClearList();
    void ClearScreen();
    int  Save(char* path);
    int  Load(char* path);
    void CutLast(char* str);
    void SearchUser(char* name);
    int  GetMinor(char* name,char* name2);
    
    typedef struct Listelement
    {
    	char name[50];
    	int phone;
    	struct Listelement* next;
    
    }Item;
    
    
    //global variables to
    Item *curr=NULL;  //current element in the list
    Item *first=NULL; //head of the list
    
    
    int main(int argc, char* argv[])
    {
    
    
    	int choose=0; //choose in the iteractive menu
    	char path[1024] ;  // path for saving\loading list
    
    	printf("*****************************************\n");
    	printf("**             Phone   List            **\n");
    	printf("*****************************************\n\n");
    
    
    	while(choose!=12) //loop cycle, the lopp is repeated until the user decide to exit
    	{   //app's menu
    		choose='0';
    		printf("  Insert phone number                : 1 \n");
    		printf("  Clear phone list                   : 2 \n");
    		printf("  Print list                         : 3 \n");
    		printf("  Search by name                     : 4 \n");
    		printf("  Sort incrementally phone numbers   : 5 \n");
    		printf("  Sort decrementally phone numbers   : 6 \n");
    		printf("  Sort incrementally by name         : 7 \n");
    		printf("  Sort decrementally by name         : 8 \n");
    		printf("  Save to file                       : 9 \n");
    		printf("  Load from file                     : 10 \n");
    		printf("  Clean screen                       : 11 \n");
    		printf("  Exit                               : 12 \n\n");
    		printf("*****************************************\n\n");
    		printf("       # Select command: ");
    		scanf("%d",&choose);
    
    
    		switch(choose)
    		{
    		case 1:
    			InsertNumbers();
    			//PrintNumbers();
    			break;
    		case 2:
    			{
    			ClearList(); //clear the phone list
    			break;
    			}
    		case 3:
    			{
    			PrintNumbers(); //print phone list
    			break;
    			}
    		case 4:
    			{
    			 //search by name
    			printf("\n Input user name:");
    			scanf("%s",path);  //reusing var path as name variable
    			ClearScreen();
    			SearchUser(path);
    			break;
    			}
    		case 5:  //incremental order
    			{
    			SortItems(0);
    			break;
    			}
    		case 6://decremental order
    			{
    			SortItems(1);
    			break;
    			}
    		case 7:  //incremental order by name
    			{
    			SortItemsbyName(0);
    			break;
    			}
    		case 8://decremental order by name
    			{
    			SortItemsbyName(1);
    			break;
    			}
    
    
    		case 9://save to file
    			{
    			printf("\n Input file name :");
    			scanf("%s",path);
    			Save(path);
    			ClearScreen();
    			break;
    			}
    		case 10://load from file
    			{
    			printf("\n Input file name :");
    			scanf("%s",path);
    			Load(path);
    			ClearScreen();
    			break;
    			}
    		case 11:
    			{
    			//clear the screen
    			ClearScreen();
    			break;
    			}
    		default:
    			ClearScreen();
    			break;
    
    		};
    
    	}
    
    
    	ClearList();//memory deallocation
    	return 0;
    }
    
    //Print current elements in the list   name :  phone number
    void PrintNumbers()
    {
    
    	char str[50];
    	if(!first)  //if no number has been inserted we exit with error
    	{
    		printf("\n Error: phone list is empty\n");
    		return;
    	}
    
    	curr=first;
    	printf("\n\n\n");
    	while(curr!=NULL)
    	{
    		sprintf(str,"%s  : %d \n",curr->name,curr->phone);
    		curr=curr->next;
    		printf(str);
    	}
    	printf("\n\n");
    }
    
    
    //names and phonelist insertions
    void InsertNumbers()
    {
    	int cc=0;
    	int phone=0;
    	int esci=0;
    	char name[50];
    	printf("\n\n Insert new phone numbers:\n");
    	printf("  insert -1 to complete operation\n:");
    
    
    	while(esci!=1)
    	{
    		printf("  Phone number: \n:");
    		scanf ("%d", &phone);
    		if(phone==-1) // if user input is -1 we exit from the input cycle
    			esci=1;
    		else
    		{
    			printf("  User name: \n:");
    			scanf ("%s", &name);
    			//dynamic allocation of the currente element in the list
    			curr=(Item*)malloc(sizeof(Item));
    			curr->phone=phone;
    			strcpy(curr->name,name);
    			curr->next=first;
    			first=curr;
    		}
    	}
    }
    
    // ordering by phone numbers' values using bubblesort
    void SortItems(int minore/*set incremental\decremental sorting*/)
    {
    
    	Item* j,*q=NULL;
    	int comodo;
    	char str[50];
    
    
    	if(!first)  //if no number has been inserted we exit with error
    	{
    		printf("\n Error: phone list is empty\n");
    		return;
    	}
    
    
    	if(minore==0) //incremental order
    	{
    		curr=first;
    		while(curr!=NULL)
    		{
    			j=curr->next;
    			while(j!=NULL)
    			{
    				if(curr->phone> j->phone)
    				{
    					strcpy(str,curr->name);
    					comodo=curr->phone;
    					curr->phone=j->phone;
    					strcpy(curr->name,j->name);
    					j->phone=comodo;
    					strcpy(j->name,str);
    				}
    				j=j->next;
    			}
    			curr=curr->next;
    		}
    	}
    	else   //decremental order
    	{
    		curr=first;
    		while(curr!=NULL)
    		{
    			j=curr->next;
    			while(j!=NULL)
    			{
    				if(curr->phone < j->phone)
    				{
    					strcpy(str,curr->name);
    					comodo=curr->phone;
    					strcpy(curr->name,j->name);
    					curr->phone=j->phone;
    					j->phone=comodo;
    					strcpy(j->name,str);
    				}
    				j=j->next;
    			}
    			curr=curr->next;
    		}
    
    	}
    
    	PrintNumbers();
    
    }
    
    
    
    void ClearList()
    {
    	Item* comodo=NULL;
    
    	if(first!=NULL)
    		curr=first;
    
    	while(curr!=NULL)
    	{
    		comodo=curr->next;
    		free(curr);
    		curr=comodo;
    	}
    	curr=NULL;
    	first=NULL;
    
    }
    
    
    
    void ClearScreen()
    {
    //	system("cls"); // windows system
    	system("clear");  //unix stsyem
    }
    
    
    //save phone list to file
    int Save(char* path)
    {
    	FILE* ff;
    	char str[80];
    	ff=fopen(path,"w");
    	if(!ff) // cannot open file
    	{
    		sprintf(str,"Err: cannot save to specified file : %s\n",path);
    		printf(str);
    		return 1;
    	}
    
    	curr=first;
    	while(curr!=NULL)
    	{
    		if(curr->next==NULL)
    			sprintf(str,"%s\n%d",curr->name,curr->phone);
    		else
    			sprintf(str,"%s\n%d\n",curr->name,curr->phone);
    
    		fputs(str,ff);
    		curr=curr->next;
    	}
    
    	fclose(ff);
    	return 0; // return succesfully
    }
    
    int Load(char* path)
    {
    	FILE* ff;
    	char str[80];
    	int len=50;
    	ff=fopen(path,"r");
    	if(!ff) // cannot open file
    	{
    		sprintf(str,"Err: cannot load specified file : %s\n\n",path);
    		printf(str);
    		return 1;
    	}
    	//clear the list in memory
    	ClearList();
    	//fill the list with readed data rom file
    	while(!feof(ff))
    	{
    		//dynamic allocation of the currente element in the list
    		curr=(Item*)malloc(sizeof(Item));
    
    		fgets(str,len,ff);  //read the name
    		CutLast(str);   //cut the string after the \n
    		strcpy(curr->name,str);
    		fgets(str,len,ff);	//read the phone
    		CutLast(str);  //cut the string after the \n
    		curr->phone=atoi(str);
    		curr->next=first;
    		first=curr;
    
    	}
    	fclose(ff);
    	return 0; // return succesfully
    }
    
    
    void CutLast(char* str)
    {
    	int len=strlen(str);
    	str[len-1]='\0';
    }
    
    
    //search user by name using strstr method
    void SearchUser(char* name)
    {
    
    	int found=0;
    	char*seq=NULL;
    	char str[255];
    
    	sprintf(str,"Matching result for user name query : %s\n",name);
    	printf(str);
    	curr=first;
    	while(curr!=NULL)
    	{
    		seq=strstr(curr->name,name);
    		if(seq)
    		{
    			found++;
    			sprintf(str,"%d)  %s   %d \n",found,curr->name,curr->phone);
    			printf(str);
    		}
    		curr=curr->next;
    	}
    
    	if(found==0)
    		printf("No results for the specified query\n\n");
    
    	printf("\n\n");
    	
    
    }
    
    
    
    void SortItemsbyName(int minore)
    {
    	Item* j,*q=NULL;
    	int comodo;
    	char str[50];
    	int i=0;
    	int len=0;
    
    
    	if(!first)  //if no number has been inserted we exit with error
    	{
    		printf("\n Error: phone list is empty\n");
    		return;
    	}
    
    
    	if(minore==0) //incremental order
    	{
    		curr=first;
    		while(curr!=NULL)
    		{
    			j=curr->next;
    			while(j!=NULL)
    			{
    				if(GetMinor(curr->name,j->name)==2)
    				{
    					strcpy(str,curr->name);
    					comodo=curr->phone;
    					curr->phone=j->phone;
    					strcpy(curr->name,j->name);
    					j->phone=comodo;
    					strcpy(j->name,str);
    				}
    				j=j->next;
    			}
    			curr=curr->next;
    		}
    	}
    	else   //decremental order
    	{
    		curr=first;
    		while(curr!=NULL)
    		{
    			j=curr->next;
    			while(j!=NULL)
    			{
    				if(GetMinor(curr->name,j->name)==1)
    				{
    					strcpy(str,curr->name);
    					comodo=curr->phone;
    					strcpy(curr->name,j->name);
    					curr->phone=j->phone;
    					j->phone=comodo;
    					strcpy(j->name,str);
    				}
    				j=j->next;
    			}
    			curr=curr->next;
    		}
    
    	}
    
    	PrintNumbers();
    
    }
    
    
    int GetMinor(char* name,char* name2)
    {
    	int minlen;
    	int minor=1;
    	int i=0;
    	int esci=0;
    	char p,q;
    
    	if(strlen(name)>strlen(name2))
    		minlen=strlen(name);
    	else
    		minlen=strlen(name2);
    
    	
    	while(esci==0)
    	{  
    		p=name[i];
    		q=name2[i];
    		if(p>q)
    		{
    			minor=2;
    			esci=1;
    		}
    		else if(p<q)
    		{
    			minor=1;
    			esci=1;
    		}else if(i==minlen) //le stringhe sono identiche
    		{
    			minor=1;
    			esci=1;
    		}
    		i++;
    
    
    	}
    /*
    	if(strncmp(name[0],name2[0],1)<0)
    		minor=1;
    	else if(strncmp(name[0],name2[0],1)>0)
    		minor=2;
    	else 
    	{
    		while(esci!=0)
    		{  
    			if(strncmp(name[i],name2[i],1)<0)
    			{
    				minor=1;
    				esci=0;
    			}
    			else if(strncmp(name[i],name2[i],1)>0)
    			{
    				minor=2;
    				esci=0;
    			}
    			i++;
    			if(i==minlen)
    				esci=0;
    		}
    	}
    */
    	return minor;
    
    }
    Last edited by BianConiglio; 04-28-2005 at 05:48 AM.
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    I'll look into it...Thank you very much for taking the time to help me

    Talon

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    as you can see I had a structure

    Code:
    typedef struct Listelement
    {
    	char name[50];
    	int phone;
    	struct Listelement* next;
    
    }Item;
    you should have here the string "LOL" and some strings as "meaning" instead having the phone
    struct Listelement* next; means that you are pointing to the next item in the list..

    InsertNumbers() is an example how you should insert data

    SearchUser(char* name) is what you should use when you want to search something an print the content of the structure

    now start to code and post it if you have problems
    This forum is the best one I've ever seen. Great ppl, great coders

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Hehe, thank you! I'm still Googling syntax and stuff...Cause when i said i was VERY new to this..I meant as in...This'll be my second day being exposed to C..(I've done HTML/CSS/touched into VB/C/C++ A LONG time ago...)So i have some of the practical knowledge..But some of it still looks like Japanese :-D Thanks again for the help

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Ugh! I tried the syntax that everyone tells me is correct...And my compiler gives me a parse error.. "C:\Documents and Settings\David\Desktop\C Stuff\Davids_Prog_1.c: line 9: Parse Error, expecting `'}''
    'typedef struct country'
    aborting compile" to be exact...Any idea why?

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Do u remember what I said before?

    post here your entire code, remember to use code tags too .
    This forum is the best one I've ever seen. Great ppl, great coders

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > aborting compile" to be exact...Any idea why?
    Show your code,
    Say which OS and compiler you're using.

    Without that, it's just guesswork.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Code so far *embarrassed*

    Code:
    #include <stdio.h>
    
    
    typedef struct country
    {
    char     name[20];
    int    population;
    char laugnage[10];
    } Country;   
    
    {  
      /*insert lame printf commands here*/
       return 0;
    }
    That is a code snippet that i stuck in there directly from a website explaining Structures (Right after i tried my own and it didn't work..) I'm using Miracle C v3.2 and running Windows XP..I apologize for my n00bishness..But i didn't have very much notice, and was thrust into the C realm quite suddenly..

    Talon

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    *thumps his own forehead* Ok, the error was in the printf commands (Which i took out when i posted it up there... Sorry again for my ignorance.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    keep coding
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed