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!

Recommended Answers

All 4 Replies

The first, I need all input data to display as red

There is no standard for changing colors. I'm assuming you're on Windows. I don't see you using windows.h... which is needed for Windows console color. Here's a little example:

If you're using a *nix based system, you'll need to use special \033 codes:

The reason you're getting an error on line 37 is because malloc() returns a void pointer. This is so that malloc can allocate any type of memory, whether it be int, double, or something else. So to make the conversion to char work, you're going to need to cast it:

names[i] = (char *)malloc( nameLength + 1 );    // + 1 for null termination

Hope this helps

Thank you, the code helped... but I am still a bit confused regarding colors. Yes I am on windows, I simply need the input I type in to appear red. Nothing more... huummmm

Taken from the code snippet I linked to previously:

HANDLE  hConsole;
    int k;
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  // you can loop k higher to see more color choices
  for(k = 1; k < 255; k++)
    // pick the colorattribute k you want
    SetConsoleTextAttribute(hConsole, k);
    cout << k << " I want to be nice today!" << endl;

It's fairly simple, as you can see. The second parameter in SetConsoleTextAttribute() is the color you want. In this example, it loops through all 255 possible colors. Try running the example above, and then modify it to your needs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.