Ok, so the program takes two inputs name, and grade. I will spare you the details. It works, however I have two issues. The first, I need all input data to display as red... I just have had NO luck getting that to work for me. Also, I need the output grades to be columnized (within reason for long names). as of now longer names push the grade over. Here is what I have so far, also not quite sure why I am getting a line 37 error...
// Program accepts names and grades and returns them
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxNameLength 512
#define MinStudents 1
#define MaxStudents 10
int main( int argc, char** argv )
{
int numStudents = 0;
char** names;
int i;
char longName[MaxNameLength]; // longest name allowed
char grades[MaxNameLength];
int nameLength;
// Find out how many students there are
while( (numStudents < MinStudents) || (numStudents > MaxStudents) )
{
printf( "Enter the number of students (%d-%d): ", MinStudents, MaxStudents );
scanf( " %d", &numStudents );
getchar(); // read the newline out of the way
}
printf( "\n" );
// Allocate the names array
names = (char**) malloc( numStudents * sizeof( *names ) );
if( !names ) // ( names == NULL )
{
printf( "Insufficient memory to allocate names array of length %lu\n",
numStudents * sizeof( *names ) );
exit( 1 );
}
// Get the names, allocating memory for each name as needed
printf( "Enter the %d names and grades:\n", numStudents );
for( i = 0; i < numStudents; i++ )
{
// Get the name
printf( "Name %d: ", i+1 );
fgets( longName, MaxNameLength, stdin );
nameLength = strlen( longName ); // may include newline
if( longName[nameLength-1] == '\n' )
{
longName[nameLength-1] = '\0';
nameLength--; // no longer includes newline
}
printf( "Grade %d: ", i+1 ); //get grades
fgets( grades, MaxNameLength, stdin );
// Allocate memory for the name string
names[i] = malloc( nameLength + 1 ); // + 1 for null termination
if( !names[i] )
{
printf( "Insufficient memory to allocate names[%d] string of length %d\n",
i, nameLength );
exit( 1 );
}
strcpy( names[i], longName ); // we just allocated the correct length, so no need for strncpy
}
printf( "\n" );
// Print the names and grades
for( i = 0; i < numStudents; i++ )
printf( "%s %s", names[i], grades );
// Clean up the memory we allocated
for( i = 0; i < numStudents; i++ )
free( names[i] );
free( names );
return( 0 );
}
Hope my paste job works!