I got the following C code here and following error messages and just don't know how to solve it:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#pragma warning (disable: 4996)
#define MAX_ROWS 20
#define MAX_COLS 30
typedef struct {
char data[MAX_ROWS][MAX_COLS];
int rows;
int cols;
} Matrix;
char neighbor_count(Matrix* m, int row, int col) {
int count = 0;
for (int i = row - 1; i <= row 1; i ) {
if (i < 0 || i >= m->rows) {
continue;
}
for (int j = col - 1; j <= col 1; j ) {
if (j < 0 || j >= m->cols) {
continue;
}
if (m->data[i][j] == '*' && (i != row || j != col)) {
count ;
}
}
}
return count;
}
void check_cell(Matrix* m, int row, int col) {
char neighbors_count = neighbor_count(m, row, col);
char current_cell = m->data[row][col];
if (neighbors_count < 2) { //Die Zelle stirbt an Vereinsamung
m->data[row][col] = ' ';
}
else if (neighbors_count > 3 && current_cell == '*') { //Die Zelle stirbt an Übervölkerung
m->data[row][col] = ' ';
}
else if (neighbors_count == 3 && current_cell == ' ') { //Aus der toten Zelle wird eine neue lebende Zelle
m->data[row][col] = '*';
}
else if (neighbors_count == 2 || neighbors_count == 3) { //Die Zelle lebt weiter
m->data[row][col] = '*';
}
}
void load_from_file(Matrix* m, const char* filename) {
FILE* f = fopen(filename, "r");
if (f == NULL) {
printf("File not found\n");
exit(-1);
}
int row_idx = 0;
char ch;
while ((ch = fgetc(f)) != EOF && row_idx < MAX_ROWS) {
if (ch == '\n') {
row_idx ;
continue;
}
m->data[row_idx][m->cols] = ch;
m->cols ;
}
m->rows = row_idx;
fclose(f);
}
void randomize(Matrix* m, int percent) {
m->rows = MAX_ROWS;
m->cols = MAX_COLS;
int cells = (m->rows * m->cols) * (percent / 100.0f);
srand(time(NULL));
while (cells > 0) {
int row = rand() % m->rows;
int col = rand() % m->cols;
if (m->data[row][col] == ' ') {
m->data[row][col] = '*';
cells--;
}
}
}
void print_matrix(Matrix* m) {
for (int i = 0; i < m->rows; i ) {
for (int j = 0; j < m->cols; j ) {
printf("%c", m->data[i][j]);
}
printf("\n");
}
printf("\n");
}
void step(Matrix* m) {
Matrix m_tmp = *m;
for (int i = 0; i < m->rows; i ) {
for (int j = 0; j < m->cols; j ) {
check_cell(&m_tmp, i, j);
}
}
*m = m_tmp;
}
int main() {
Matrix m;
// Menü zur Auswahl des Startzustands
printf("1. Aus Datei laden\n");
printf("2. Zufallszustand generieren\n");
int selection;
scanf("%d", &selection);
switch (selection) {
case 1: // Aus Datei laden
char filename[20];
printf("Bitte Dateinamen angeben: ");
scanf("%s", filename);
load_from_file(&m, filename);
break;
case 2: // Zufallszustand generieren
int percent;
printf("Prozentualer Anteil an lebenden Zellen: ");
scanf("%d", &percent);
randomize(&m, percent);
break;
default:
printf("Ungültige Eingabe\n");
return 0;
}
// Menü zur Auswahl der Animation
printf("1. Schrittweise Animation\n");
printf("2. Fließende Animation\n");
scanf("%d", &selection);
switch (selection) {
case 1: // Schrittweise Animation
while (1) {
print_matrix(&m);
step(&m);
getchar();
}
break;
case 2: // Fließende Animation
while (1) {
print_matrix(&m);
step(&m);
usleep(200000);;
}
break;
default:
printf("Ungültige Eingabe\n");
break;
}
return 0;
}
and this error messages
- E1072 A declaration cannot have a label. CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\CGoL Beleg Prog\Quelle.c 119
2.E1072 A declaration cannot have a label.. CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\CGoL Beleg Prog\Quelle.c 125
- LNK2019 Reference to unresolved external symbol "usleep" in function "main". CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\CGoL Beleg Prog\Quelle.obj 1
4.LNK1120 1 unresolved external CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\x64\Debug\CGoL Beleg Prog.exe 1
Here again the errors as a picture
Ich habe schon probiert die Anweisungen zu befolgen aber ich sehe wahrscheinlich den Baum vor lauter Wald nicht
CodePudding user response:
"A declaration cannot have a label" means that you cannot declare variables in the middle of a switch
/case
. You can solve this by adding a local compound statement:
case 1: // Aus Datei laden
{
char filename[20]; // this one was causing the problem
printf("Bitte Dateinamen angeben: ");
scanf("%s", filename);
load_from_file(&m, filename);
break;
}
Same thing with int percent;
.
"Reference to unresolved external symbol "usleep"" means that the compiler can't find usleep
. Most likely because it is obsolete in Windows. See this c , usleep() is obsolete, workarounds for Windows/MingW?.