Pelles C forum
C language => Beginner questions => Topic started by: John1 on December 17, 2018, 10:43:19 PM
-
Hello,
I´m quite new to C and try to learn. After some time of trying different codes, I decided to post here. Among various Problems in the Program, I will start with one. Sorry for my english.
The Program gets the input from fgets fgets(car,sizeof(car),stdin) and give the value to an Array char* carA[100]; with carA[z++] = car; .
Strange is that the Index is incrementing, but the Values at the Output are not correct :
void listCars(void) {
for(int i = 0; i <= (int)strlen(*carA)-1; i++) {
printf("Cars rented : %s\n", carA[i]);
}
}
-
Your array structure is unknown, so hard find errors, and this is weird for me:
i <= (int)strlen(*carA)-1;
the lenght of the first string array element - one ?
an example with a static string array#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int __cdecl main(void)
{
char buf[80], astr[10][100]; // an array of strings
int cnt = 0; // string counter
while(fgets(buf,sizeof(buf),stdin)) { // read to buffer, not really needed with a static string array
if (*buf == 10 || cnt >= 10) break; // exit if plain ENTER or array limit
int len = strlen(buf);
buf[len-1] = 0; // remove LF
strcpy(astr[cnt], buf); // copy to array
cnt++; // advance next
}
for (int i=0; i<cnt; i++)
printf("%d %s\n", i, astr[i]);
return 0;
}
-
Hello TimoVJL,
Thank you for your answer.
I use char* carA[100]; to declare the Array.
I changed i <= (int)strlen(*carA)-1; to i < z; .
I tried a bit to change my Code similar to your Example, but I couldn´t let it run, due an Exception 255. To not copy and paste your Code and learn something.
Here is the Code I use to add Elements to the Array, maybe you may see some Errors.
void addNewCar(void) {
char* car = (char*)malloc(100*sizeof(char));
printf("Add a new Car : ");
if(fgets(car,sizeof(car),stdin) == NULL) {
printf("Wrong Input\n");
dump_buffer(stdin);
free(car);
return;
}
carA[z++] = car;
free(car);
return;
}
-
couple things:
1. use if (fgets(car, 100, stdin) == NULL) as sizeof(car) is a pointer size.
2. don't free memory in addNewCar(), do it end of program
3. use tempory buffer for input and malloc only needed memory and copy that string to it.
-
I changed it following the first two advises.
3. use tempory buffer for input and malloc only needed memory and copy that string to it.
Sorry I couldn´t understand the third advise ?
Actually it puts still the wrong Elements in the Array. Input: Car one - Car two - Car three
void addNewCar(void) {
car = (char*)malloc(100*sizeof(char));
printf("Add new Car : ");
if(fgets(car,100,stdin) == NULL) {
printf("Wrong Input\n");
dump_buffer(stdin);
free(car);
return;
}
carA[z++] = car;
return;
}
Output :
Element : Car one and Index : 0
Rented Car : Car one
Element : Car three and Index : 1
Rented Car : Car three
Element : Car three and Index : 2
Rented Car : Car three
-
Something like this:
int addNewCar(void)
{
char buf[100];
printf("Add a new Car : ");
if (fgets(buf, 100, stdin) == NULL || *buf == 10)
{
printf("Wrong Input\n");
return 0;
}
int len = strlen(buf);
buf[len-1] = 0;
char *car = (char *)malloc(len);
strcpy(car, buf);
carA[z++] = car;
return len;
}
-
I tired this code but the output is still wrong. I tried also strcpy(carA[z++], buf); without success.
Why the Array doesn´t get the correct Elements ?
Thank you
-
A test code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* carA[100];
int z;
int addNewCar(void)
{
char buf[100];
printf("Add a new Car : ");
if (fgets(buf, 100, stdin) == NULL || *buf == 10)
{
printf("Wrong Input\n");
return 0;
}
int len = strlen(buf);
buf[len-1] = 0;
char *car = (char *)malloc(len);
strcpy(car, buf);
carA[z++] = car;
return len;
}
void listCars(void)
{
for (int i = 0; i < z; i++)
printf("Cars rented : %s\n", carA[i]);
}
int main(void)
{
while (z < 100 && addNewCar());
listCars();
return 0;
}
-
Timo, thank you for your patience. Now it works :)
I changed the the malloc to car = (char*)malloc(100*sizeof(len)); before sizeof was with char. Syntax error, maybe because char have more bits and create an undefined behaviour ?
May you give me an advise on how to make the programs data persistent after quitting the Program, I thought to Files or Database ?
Have a good evening
John
-
I changed the the malloc to car = (char*)malloc(100*sizeof(len));
Not a good idea, as len is a correct size for that ANSI string. Unicode needs more room, but fgets() was used.
A fgets can read strings from file and fputs can write them.