# Thread: How to solve this?

1. ## How to solve this?

I'm get stuck at question 2 below. Got no idea! Anyone can give me some orientation? thanks

A company wants to develop a program to manage their 12 cars
-Car's ID: it is a string which has 3 characters
-Car's status: its value is:
+ 1 if the car is hiring by a customer
+ 0 if the car hasn't been hired yet
-Customer's name: It is a string which contains full name of customer who is hiring the car.
If the car hasn't been hired yet, the string shouldn't have a value

Q1: Create a structure template that contains data of one car. Then use this template to
declare an array of structures that stores data of all company's cars (12 cars)

Q2: Create a function to assign the car's ID for 12 cars in the array of structures as the
follows:
- the first car in the list should have the ID : "C01"
- the second car in the list should have the ID : "C02"
...
- the last car in the list should have the ID : "C12"

2. What exactly do you have problem with? Did you try writting anything?

3. question 2: Create a function to assign the car's ID for 12 cars in the array.
I don't know how do do this

4. Have you done the first question?
.

5. Forum policy on homework is that *you* need to post up what you've tried, in code, or pseudo code, and show or tell us, what the problem is.

When you just say "I don't know", that's no help to us. We don't know why you don't know.

We're not here to do your homework for you. If you're not willing to try, we're not willing to help (with few exceptions).

So, post up what you have for your program, and tell us what's got you stumped.

6. What I don't know is just the algorithm, the way to do it. Because don't know the algorithm, I cannot write the code. Code for the Q1(I think it's easy, so didn't post it) :

#include<stdio.h>
typedef struct car
{
struct id;
int status;
char cus[20];
}car;
car list[12];
[/code]

7. Finally find out the way I used nested structure

Code:
```#include<stdio.h>
#include<conio.h>
void assign_id(void);
struct id
{
char st;
int nd;
int rd;
};
typedef struct car
{
struct id no;
int status;
char cus[20];
}car;
car list[12];
int main(void)
{
assign_id();
getch();
return 0;
}

void assign_id(void)
{
int i;
for(i=0;i<9;i++)
{
list[i].no.st='C';
list[i].no.nd=0;
list[i].no.rd=i+1;
}
for(i=9;i<12;i++)
{
list[i].no.st='C';
list[i].no.nd=1;
list[i].no.rd=i-9;
}
for(i=0;i<12;i++)
{
printf("ID of car #%d: ",i+1);
printf("%c%d%d\n",list[i].no.st,list[i].no.nd,list[i].no.rd);
}
}```

I tried to type char st='C';(instead of red words above) so that I would'nt have to assign values by using for loop (as you see in the assign_id function). But there were some errors & warning:

[Warning] no semicolon at end of struct or union
syntax error before '=' token
syntax error before '}' token
field `no' has incomplete type

Did I forget any rules of declaring/assigning values?
@Adak: Sorry for not posting anything. I just want to have some orientation, not the code (now I found it on my own, but is there any shorter way?) I'm a newbie and had no intention to break the law

8. Your struct id etc. is just defining a type; there are no variables created. You cannot initialize any of the fields of the variable until you actually define a variable.

9. Originally Posted by fenikkusu
Finally find out the way I used nested structure
...
but is there any shorter way?
IMHO you don't need a nested structure unless it's required of you and there's a shorter way by using a 4 char array as number of cars is limited to 12.
Code:
```#include <stdio.h>

void assign_id(void);

typedef struct car
{
char st[4];
int status;
char cus[20];
}car;

car list[12];

int main(void)
{
assign_id();
return 0;
}

void assign_id(void)
{
int i;

for (i=0; i<12; i++)
snprintf(list[i].st,sizeof list[i].st,"C%02d",i+1);

for (i=0; i<12; i++)
printf("ID of car #%d: %s\n",i+1,list[i].st);
}```

10. Thank you all. But I have never heard about this function snprintf(list[i].st,sizeof list[i].st,"C%02d",i+1); Can you clarify it for me?

11. You don't need nested structs, you don't need snprintf(), (whatever that is), which I would love to say is a special printf() function for printing out auto vin (serial) numbers, but you probably wouldn't laugh, anyway.

You need an array of structs, with various fields in them, and that's most of what you need. I can't believe you know how to define a struct, but not how to actually create one.

Post your most current code, and we'll see what's up with it. The sooner the better. Back in a bit.

30 minutes, and no response. Despite the "online now" footer, maybe you aren't here, after all. Another time, perhaps.

12. I'm not sure I understand your idea.
I just change snprintf into sprintf, and see that it also works (forgot that we have such a function to assign values to a string)
Code:
```#include <stdio.h>

void assign_id(void);

typedef struct car
{
char id[3];
int status;
char cus[20];
}car;

car list[12];

int main(void)
{
assign_id();
getch();
return 0;
}

void assign_id(void)
{
int i;

for (i=0; i<12; i++)
sprintf(list[i].id,"C%02d",i+1);

for (i=0; i<12; i++)
printf("ID of car #%d: %s\n",i+1,list[i].id);
}```

13. My idea was to see whether you had things sorted out or not, and if not, to suggest a struct that met your needs, without the complexity of a nested struct.

Looks like you're set.

14. OMG! You are like my teacher, often make me confused and nervous (because i'm not really good at this subject)

15. Originally Posted by fenikkusu
OMG! You are like my teacher, often make me confused and nervous (because i'm not really good at this subject)
OMG! You are like my students, often make me confused and nervous (because i'm not really good at just what they need to be really good at this subject)

j/k, I'm not a teacher.

Your last post before mine was a question, so I didn't want to see your problem remain unresolved.