Thread: Need help with this code for homework!

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Need help with this code for homework!

    I'm trying to get this code working properly. I'm just learning pointers and I'm still confused. I've tried to write this program as best I can but I'm still getting a few errors and because I'm new at this, I don't know how to debug it or if I'm even on the right track

    pseudo-code:
    Find out how many student names (and grades) are to be entered
    Allocate the names and grades arrays
    Prompt for and read the names and grades
    Read the name into a temporary string
    Allocate memory big enough to hold the name
    Copy the name from the temporary string to the allocated string
    Read the grade
    Print the names and grades
    Free the memory we allocated


    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define ArraySize 10
    #define MaxNameLength 512
    
    int main(int argc, char** argv)
    {
    	char name[MaxNameLength +1];
    	char *studentNames = name;
    	int numNames;
    	float grade;
    	int i;	
    	
    	studentNames =malloc(MaxNameLength * sizeof(char));
    	
    		printf( "Enter the number of students (1-10): ");
    		fgets(numNames, MaxNameLength, stdin);
    		printf( "Enter the %d names and grades: /n", numNames);
    				
    		for (i = 0; i < numNames[i]; i++)
    		{
    			printf( "Name %d: %s/n", i, numNames[i]);
    			printf( "Grade %d: %.2f/n", i, grade[i]); 
    		}
    	
    	free(studentNames);
    	studentNames = NULL;
    	
    	return ( 0 );
    }

    Any help is appreciated

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you are using MSVS debugging it is as simple as pressing F5, F9, F10, F11, or shift F10 for the various oft-used actions. You can also right click various lines and set breakpoints or you can choose run to cursor. The program will run to the cursor and return back to the Debugger.
    You can set watches on variables and expressions and MSVS will show you in a tree view all the variables in scope, their value or validity (as in the case of pointers), and tons more. You can also step through for loops and examine the loop variable, array indices, etc, etc. You can also view the call stack and click on any portion of it to go to that function. This is handy for finding out who called who and when.

    Outside of the debugger you can right click on class names, variables, etc, etc, and click either Go To Declaration or Go To Definition which is very handy. Sometimes MSVS complains and this doesn't work but most of the time it does.

    These are the oft-used keys for the debugger:
    F5 - start debugging
    F10 - step over (useful for stepping over functions you know work or are very long)
    F11 - step into (steps into the function, loop, etc)
    shift F10 - step out (steps out of the function)
    F9 - toggle breakpoint

    I would help with your code but I think this is a good exercise for you to learn how to debug your own code by using the debugger in your compiler. If you don't have MSVS most other debuggers are very similar in operation.
    When you get a job programming there probably will not be enough time for others to show you exactly how all of the code works so it is essential you know how to use your debugger so you can do some code spelunking.
    Last edited by VirtualAce; 11-12-2007 at 06:59 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    I'm not using a debugger; thats the problem. We aren't using anything like VB. We're using a tool called Cygwin (not a big fan) so I'm stuck debugging it by myself but it's hard to do when you're not exactly sure what you're looking for. Thank you though

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ch147 View Post
    I'm not using a debugger; thats the problem. We aren't using anything like VB. We're using a tool called Cygwin (not a big fan) so I'm stuck debugging it by myself but it's hard to do when you're not exactly sure what you're looking for. Thank you though
    cygwin gdb works fine.

    EDIT: As for what's wrong with the code... Practically every single line is wrong in one way or another..
    Last edited by brewbuck; 11-12-2007 at 07:05 PM.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    What is gdb?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ch147 View Post
    What is gdb?
    GDB is the GNU debugger, which is usually available in gcc-based environments. Google is more informative than I could be.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    I'll check it out. thank you

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But a debugger, however good it may be [and GDB is quite good] only works when you achieve an executable, and to do that, you need to compile the code - your code doesn't compile as posted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM