My C code doesn't work?

Hi, I'm currently following a C tutorial and I'm stuck. I can't get past this point/error. The program is supposed to output four phone numbers, but it's not finished yet.

#include <stdio.h>

int main() {

char phoneNumbers[4] [256] = {

"+49265367000",

"+49347318001",

"+49134812002",

"+49127414003",

};

if (showMenu() == 0) {

showPhoneNumbers(phoneNumbers);

} else {

addPhoneNumber();

}

return 0;

}

void showPhoneNumbers(char myPhoneNumbers[4][256]) {

printf("Phone numbers: \n");

for(int i = 0; i < 4; i++) { //this line is always red

printf("(%d) %s \n", i, &myPhoneNumbers[i]);

}

}

void addPhoneNumber(){

printf("Add new number");

}

int showMenu() {

int selection;

printf("###### Welcome ######\n");

printf("(0) Display phone numbers\n");

printf("(1) Add new number\n");

scanf(" %d", &selection);

printf("\n\n");

if (selection == 0 || selection == 1) {

return selection;

} else {

printf("Input invalid\n");

return showMenu();

}

}

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
JanaL161
1 year ago

In C, data and functions must be declared before the first use.

Either you push main() down or set

int showMenu();
void showPhoneNumbers(char myPhoneNumbers[4][256]);
void addPhoneNumber();

at the beginning of the file. The definition of the functions can thus also remain below.

This is because C times was designed for single-pass compilers. That is, it was not possible to skip around in the source text in order to dissolve something like that.