Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in main function in "TestCase tests" have i an error
"a value of type const char* can not be use".
what shuld be do!!
i don´t have any sujestion. i have try whit casting but , didn´t worked!!

What I have tried:

#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char* extract(char* input) {
	char* ptr;
	ptr = strstr(input, "::");
	char* answer;

	if (isprint(*ptr) == 0) {
		answer = input;
	}
	else {
		while (ptr != NULL) {
			answer = ptr;
			ptr = strstr(ptr + 1, "::");

		}
		answer += 2;
	}
	printf("%s\n", answer);
	return answer;
}
// Ueberspringe alle Leerzeichen
// Rueckgabe ist Zeiger auf das erste Nichtleerzeichen
char* leerzeichen(char* input) {
	while (*input == ' ')
		input++;
	return input;
}

// Scanne durch string solange bis wir auf ein
// Leerzeichen oder das Ende des Strings treffen.
// Effektiv ueberspringen wir ein Wort.
// Rueckgabe: Zeiger auf Ende oder Leerzeichen.
char* zeichen(char* input) {
	while (*input != '\0' && *input != ' ')
		input++;
	return input;
}


int count(char* input) {
	int cnt = 0;

	// Solange das Ende nicht erreicht ist:
	// 1. Ueberspringe alle Leerzeichen
	// 2. Falls Zeichen gefunden
	//     (a) setze Zaehler hoch
	//     (b) Gehe zu Wortende

	while (*input != '\0') {
		input = leerzeichen(input);
		if (*input != '\0') {
			cnt++;
			input = zeichen(input);
		}
	}
	return cnt;
}


typedef enum {
	OK,
	FAIL
} Test;

Test testCount(char* input, int expected) {
	Test t;

	if (expected == count(input)) {
		t = OK;
	}
	else {
		t = FAIL;
	}
	return t;
}


typedef struct {
	char* input;
	int expected;
} TestCase;


void runTests(int no, TestCase test[]) {
	Test t;
	int i;

	for (i = 0; i < no; i++) {
		printf("Test %d: ", i);
		t = testCount(test[i].input, test[i].expected);
		if (OK == t)
			printf("OK \n");
		if (FAIL == t)
			printf("FAIL \n");
	}
}

#define ARRAY_SIZE 9
int main() {
	const int testNo = ARRAY_SIZE;
	TestCase tests[ARRAY_SIZE] = {
		 <big>{"", 0},
		 {"Hallo", 1},
		 {"  Hallo", 1},
		 {"Hallo", 1},
		 {"  Hallo  ", 1},

		 {"Hal lo", 2},
		 {" Hal lo", 2},
		 {"Hal lo ", 2},
		 {" Hal lo  ", 2}
	};</big>	runTests(testNo, tests);

}
Posted
Updated 27-Nov-18 1:49am

Try changing the defiintion of TestCase as follows:
C++
typedef struct {
	const char* input;
	int expected;
} TestCase;
 
Share this answer
 
Comments
Member 14068859 28-Nov-18 2:41am    
Thanks
I copy and paste your code into an online C++ system, and it works: https://www.onlinegdb.com/online_c++_compiler[^]

It compiles cleanly, it runs, and I get:
Test 0: OK 
Test 1: OK 
Test 2: OK 
Test 3: OK 
Test 4: OK 
Test 5: OK 
Test 6: OK 
Test 7: OK 
Test 8: OK

So ... we would need to know what compiler, what line, and so forth.

Alternatively, change your TestCase struct to accept const strings:
typedef struct {
	const char* input;
	int expected;
} TestCase;
You'll have to match that with all your function calls though or they won;t compile either!
 
Share this answer
 
Comments
Member 14068859 28-Nov-18 2:41am    
Thanks
OriginalGriff 28-Nov-18 3:13am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900