C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 05:58 PM   #16
Registered User
 
Join Date: Jun 2009
Posts: 20
Quote:
Originally Posted by ಠ_ಠ View Post
... what are you doing
Ah sorry, it was originally:
Code:
scanf("%d", &messages[position].priority);
I did take your advice the first time, i was just messing around with different things since I couldnt even get the simple one to work
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 05:59 PM   #17
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
Quote:
Originally Posted by CMakesMeSad :( View Post
Ah sorry, it was originally:
Code:
scanf("%d", &messages[position].priority);
I did take your advice the first time, i was just messing around with different things since I couldnt even get the simple one to work
ask yourself, what is the value of position
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-08-2009, 06:04 PM   #18
Registered User
 
Join Date: Jun 2009
Posts: 20
Quote:
Originally Posted by ಠ_ಠ View Post
ask yourself, what is the value of position
the value of postion is -1 which makes absolutely no sense. ill have to do something about this, thanks
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 06:56 PM   #19
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
I would recommend starting with a fixed-size structure and no dynamic allocation first, until you have got everything else worked out.
Sebastiani is offline   Reply With Quote
Old 07-08-2009, 10:23 PM   #20
Registered User
 
Join Date: Jun 2009
Posts: 20
Hey everyone, Ive been working on this all day and have made some good progress, however Im stuck again. Im working on the option of listing the messages by priority. However, no matter what priority I enter, it will only list the first message i enter.


Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100

struct MSG {
	int priority;
	int destination;
	char *message;
	int length;
	struct MSG *nextPtr;
}*node, *t;

typedef struct MSG message;


void add(struct MSG **n, int priority1, int destination1, char *message1);

int main ()
{
	struct MSG *temp;
	int choice;
	int num1, num2;
	char text[MAX];
 		
do
{
	printf("1.) Quit\n");
	printf("2.) Transmit next message\n");
	printf("3.) List messages by priority\n");
	printf("4.) Summarize message list\n");
	printf("5.) Add new message\n");
	printf(":");
	scanf("%d", &choice);
	switch(choice){

		case 1:
			return 0;
		case 2:
			//transmit next message
			if (node==NULL)
			{
				printf("No messages left!\n");
				break;
			}
			
			else
			{
				temp=node;
				while(temp != NULL)
				{
					printf("%d", temp -> priority);
					printf("%dn", temp -> destination);
					char *loc=(char *) malloc (strlen(temp -> message));
					strcpy(loc, temp -> message);
					printf("%s", loc);
					free(loc);
					temp=temp -> nextPtr;
				}
				printf("test=%d %d %s\n", t-> priority, t-> destination, t-> message);
			}


			break;
		case 3:
			//list messages by priority
				printf("Enter priority of the messages you would like to retrieve:\n");
				scanf("%d", &pnum);
				
				temp=node;
				while(temp != NULL)
				{
					//printf("%d", temp -> priority);
					//printf("%dn", temp -> destination);
					char *loc=(char *) malloc (strlen(temp -> message));
					strcpy(loc, temp -> message);
					//printf("%s", loc);
					free(loc);
					temp=temp -> nextPtr;
					if (t -> priority == pnum)
					{
						printf("%d %d %s\n", t -> priority, t -> destination, t -> message);
					}
				}


			break;
		case 4:
			//summarize message list
			break;
		case 5:
			//add new message
			printf("Enter priority, destination and message:");
			scanf("%d%d", &num1, &num2);
			fgets(text, MAX, stdin);
			add(&node, num1, num2, text);	
			break;
		default:
			printf("Invalid entry, please try again.\n");
			break;
	}
}while (choice <= 5);

	return 0;
}





void add(struct MSG **n, int priority1, int destination1, char *message1)
{
	struct MSG *temp, *node1;
	
	if(*n==NULL)
	{
		temp=(struct MSG *) malloc (sizeof(struct MSG));
		temp -> priority = priority1;
		temp -> destination=destination1;
		temp -> message=(char *) malloc (strlen(message1)+1);
		strcpy(temp -> message, message1);
		temp -> nextPtr=NULL;
		*n=temp;
		t=temp;
		printf("test=%d %d %s\n", t->priority, t->destination, t->message);
	}

	else
	{
		temp=*n;
		while(temp->nextPtr != NULL)
			temp=temp -> nextPtr;
		node1 = (struct MSG *) malloc(sizeof(struct MSG));
		node1 -> priority=priority1;
		node1 -> destination=destination1;
		node1 -> message=(char*) malloc(strlen(message1)+1);
		strcpy(node1 -> message, message1);
		node1 -> nextPtr = NULL;
		temp -> nextPtr = node1;
	}

}

Last edited by CMakesMeSad :(; 07-08-2009 at 11:06 PM.
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 11:30 PM   #21
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
You aren't printing anything other than the exact priority match. Is that what you want?

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 07-08-2009, 11:36 PM   #22
Registered User
 
Join Date: Jun 2009
Posts: 20
Quote:
Originally Posted by quzah View Post
You aren't printing anything other than the exact priority match. Is that what you want?

Quzah.
Thats kind of what I want it do. Lets say input the following 4 messages:
1 5 message1
5 10 message2
1 6 message3
2 7 message4

The first number being the priority, I want to be able to list all the messages that fall under a priority of my choice. So if I pick priority 1, I want the program to ouput

1 5 message1
1 6 message3

This is what its doing right now:
*******************
input priority: 5 or 2
nothing happens

input priority 1
1 5 message1
1 5 message1
1 5 message1
1 5 message1
*******************
I hope that makes sense.
CMakesMeSad :( is offline   Reply With Quote
Old 07-09-2009, 12:29 AM   #23
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Code:
for( t = list; t; t = t->next )
{
    if( t->priority == tofind )
    {
        printf("%d %d %s\n", t-> priority, t-> destination, t-> message);
    }
}
Should be fine, assuming your list actually isn't broken somehow. You should consider just displaying the whole list first, to make sure that actually works (basically the above loop, except don't bother checking priority).


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 07-10-2009, 01:04 PM   #24
Registered User
 
Join Date: Sep 2008
Location: Toronto, Canada
Posts: 507
In the struct try
Code:
char message[100];
(without the asterisk)

But I haven't tested it.
nonoob is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
why is strncpy segfaulting here? Calef13 C Programming 3 12-29-2008 03:27 PM
Segfaulting Distance Program radiohead C Programming 2 01-09-2006 08:48 PM
Why is it segfaulting? XSquared C Programming 7 03-30-2004 06:52 AM
a segfaulting algorythm demonus C Programming 8 08-11-2003 08:06 AM


All times are GMT -6. The time now is 05:57 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22