Thread: HW Help - Struct and Strings

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    HW Help - Struct and Strings

    Hi,

    I'm banging my head over this homework assignment after spending nearly 5 hours on it myself. I'm giving in and asking for help.

    Here is the code I have so far and once I get past this part I will be able to complete the rest of the assignment easily.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "myHeader.h"
    
    int main() {
    	
    	struct taskList *schedule;
    	
    	printf("Enter a task name, unit time, and prerequisite. If no prerequisite, enter none:\n");
    	scanf("%c %d %c", &schedule->task, &schedule->time, &schedule->preReq);
    	printf("\n\n\nTask: %c \nTime: %d \nPreRequisite: %c\n", schedule->task, schedule->time, schedule->preReq);
    
    	return 0;
    }
    Code:
    struct taskList {
    	char task;
    	int time;
    	char preReq;
    };
    Example input:
    Bake 30 Mix

    Bake - Task that needs to be completed
    30 - Time it takes to complete
    Mix - Prerequisite task, must be completed first

    Issue I have:
    I am unable to get the strings Bake and Mix stored into their respective variables and with the code I have posted above I get this error: Segmentation Fault in Unix.

    I've been told to use malloc() but I never used it before and not sure how to go about implementing it if I do in fact need to use it.

    This assignment must be in C even though I prefer C++.

    Thank you for your help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Darklegion View Post
    I've been told to use malloc() but I never used it before and not sure how to go about implementing it if I do in fact need to use it.
    malloc is a pretty fundamental command. You should look it up somewhere...anyway, right now you are using a pointer:

    Code:
    	struct taskList *schedule;
    If you remove the asterisk, you won't have to malloc any memory because "schedule" will be an actual instance of "taskList". In that case, you will need to change your notation from indirect to direct:
    Code:
    schedule->task  //indirect
    schedule.task   //direct
    Alternatively, here's how you would malloc the pointer:
    Code:
    	struct taskList *schedule = malloc(sizeof(struct taskList));
    You then need to free the memory at the end of the program:
    Code:
            free(schedule);
    This assignment must be in C even though I prefer C++.
    Hmmm. The fact that you do not understand pointers and apparently spent hours struggling with this is nothing to be ashamed of, but it is evidence that you cannot code in either C or C++ (no offense!). So I am kind of curious why you would feel this way? I'm just raising the issue because it seems silly to be starting out with a (unjustifiable) negative attitude, esp if it is because of something even more silly said by someone else...in any case good luck, cboard is a great place to look for help.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    you are scanning for a string not a character therefor you should use %s not %c.
    You should decleare an array for your string. to set aside memory or maybe you can just post your assigment and ill try to do it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    struct taskList {
    	char task[100];
    	int time;
    	char preReq[100];
    };
    
    
    int main() {
    	
    	struct taskList schedule;
        int i;	
    	printf("Enter a task name, unit time, and prerequisite. If no prerequisite, enter none:\n");
    	scanf("%s %d %s", schedule.task, &schedule.time, schedule.preReq);
    	printf("\n\n\nTask: %s \nTime: %d \nPreRequisite: %s\n", schedule.task, schedule.time, schedule.preReq);
    
        getch();
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    to malloc, you should do

    Code:
    schedule= malloc (sizeof(struct taskList));
    also, your struct should have an array of chars (this makes strings) instead of just a single char, which would be one instead of many. Your code looks like this:
    Code:
    struct taskList {
    	char task;
    	int time;
    	char preReq;
    };
    It should look like

    Code:
    struct taskList {
    	char task[30]; //30 is just a random number; it should be whatever you want or what
                                   //your assignment requires
    	int time;
    	char preReq[30]; //same as above
    };
    With that change, you should change from

    "%c" to %s"

    for example

    Code:
    scanf("%s %d %s", schedule->task, &schedule->time, schedule->preReq);
    but like someone said, you dont HAVE to make a pointer and malloc it. You can just make a direct structure and then access its variables like this

    Code:
    schedule.task, schedule.time,...

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Thank you for the help so far, I will take a look over it and see how to implement it.

    As for my comment about wanting to program in C++ rather than C:

    I've been programming in C++ for 2 years now and understand the syntax inside and out. For some reason the syntax of C throws me off on everything.

    This is my third assignment in programming with C so my time spent using C has been very minimal.

    Edit: I would also like to add that I have done an assignment similar to this in the past but it was in C++ and I am having trouble converting that assignment into this C assignment.
    Last edited by Darklegion; 03-29-2010 at 08:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. using array of strings in a struct
    By t014y in forum C Programming
    Replies: 6
    Last Post: 02-03-2009, 01:17 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM