Aj Almero Aj Almero - 1 year ago 92
C Question

How to display the contents of the array and prompt the user to make a selection {C}

The main object of the program is to display the values within the array called "channels" but i can't seem to get anything to show. After the display is shown I need to prompt the user to choose one of the four channels and display the chosen "channel"'s value. This is what i have so far. I also cannot use any loops. Please help.

#include <stdio.h>

// I'm using a structure to store all the values within the array

typedef struct

char name;
double n; //roughness
double slope;
double width;
double depth;


main ()

CHANNEL channels [4] = {
{"Channel1", 0.035, 0.0001, 10.0, 2.0},
{"Channel2", 0.020, 0.0002, 8.0, 1.0},
{"Channel3", 0.015, 0.0010, 20.0, 1.5},
{"Channel4", 0.030, 0.0007, 24.0, 3.0}


Answer Source

First, make the changes that Daniel Litvak suggests. Then, to get information from the user, you should do something like:

int main(void) {

    // ...
    printf("Pick a channel from 0-3\n");
    int c = 0;
    scanf("%i ", &c);

    CHANNEL chosen = channels[c];

    printf ("The channel chosen is %s, n = %f, slope = %f and the depth = %f", chosen.name, chosen.n, chosen.slope, chosen.depth);

This will prompt the user for an index denoting the channel's index in the array. You can also print out all of the channel choices first, if you wish.

For demonstrative purposes, I left the chosen channel in the variable chosen and you can proceed as you want.

EDIT: No error checking is done to ensure that c is within range. This is to avoid displaying any extra, confusing code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download