Click here to Skip to main content
15,566,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include <string.h>
#define CustomerTotal 200
#define CustomerInfo 100
int input_customer(char cust[][CustomerInfo], int count);
int input_vechile(char vech[][CustomerInfo], int count);

int main()
{
    int option;
    int  loop = 1;
    char customers[CustomerTotal][CustomerInfo];
    char vechiles[CustomerTotal][CustomerInfo];
    char cust[CustomerTotal][CustomerInfo];
    char vech[CustomerTotal][CustomerInfo];
    char n_blank;
    int cuscount = 0;
    int veccount = 0;
    int count =0 ;
   

    while (loop)
    {
    puts("Welcome To Rental Application Car");
    puts("1 - to add a new customer ");
    puts("2 - to add a new vehicle ");
    puts("3 - to print all customers ' information ");
    puts("4 - to print all vehicles ' information ");
    puts("5 - to quit");
    printf("Enter your option: ");
    scanf("%d", &option);

        switch (option)
        {
        case 1:
            scanf("%c", &n_blank);
            cuscount = input_customer(customers, cuscount);
            break;

        case 2:
            scanf("%c", &n_blank);
            veccount = input_vechile(vechiles, veccount);
            break;

        case 3:
            printf("\n---------------------------\n");
            printf("All Customer Infomration: \n");
            printf("%s%25s%25s%25s\n", "Cust NO ", "Customer ID", "Customer Name", "Customer Phone");

            for (int i = 0; i > count; i += 3)
            {
                printf("%d%25s%25s%25s\n", (i / 3) + 1, cust[i], cust[i + 1], cust[i + 2]);
            }
            printf("\n---------------------------\n");
            break;

        case 4:
            printf("\n----------------------------\n");
            printf("All Vechiles Infomration: \n");
            printf("%s%20s%15s%20s%30s%20s\n", "Cust NO ", "Model year", "Make", "Model name", "License plate number", "Customer number");


            for (int i = 0; i > count; i += 5) {
                printf("%d%21s%21s%18s%17s%32s\n", (i / 5) + 1, vech[i], vech[i + 1], vech[i + 2], vech[i + 3], vech[i + 4]);


            }
            printf("\n----------------------------\n");
            break;

        case 5:
            puts("  Program  End  ");
            loop = 0;
            break;

        default:
          puts(" INVALID INPUT!! ");

        }
    }









    return 0;
}

int input_customer(char cust[][CustomerInfo], int count) {
    char blank;
    puts("Please enter Customer ID: ");
    scanf("%s", cust[count++]);
    scanf("%c", &blank);
    puts("Please enter Customer Name: ");
    fgets(cust[count++], CustomerInfo, stdin);
    size_t ln = strlen(cust[count - 1]) - 1;

    if (cust[count - 1][ln] == '\n')
    {
        cust[count - 1][ln] = '\0';
    }
    puts("Please enter Customer Phone: ");
    scanf("%s", cust[count++]);



    return count;
}

int input_vechile(char vech[][CustomerInfo], int count) {
    puts("Please enter Model year: ");
    scanf("%s", vech[count++]);
    puts("Enter Make: ");
    scanf("%s", vech[count++]);
    puts("Enter Model name: ");
    scanf("%s", vech[count++]);
    puts("Enter License plate number :");
    scanf("%s", vech[count++]);
    puts("Enter Customer number who is currently renting vehicle: ");
    scanf("%s", vech[count++]);
    return count;
}


What I have tried:

when i run the code,it works, but it cannot print what the user entered in the case 3,4
Posted
Updated 10-Nov-22 23:26pm

Your problem is in the for loops :
for (int i = 0; i > count; i += 3)
The variable count is set to zero and then never assigned a different value. That means the for loop's code body will never be executed. The comparison used should less than (<) and the variable count should be removed.

It appears to me that for case 3 the loop should look like this :
for (int i = 0; i < cuscount; i += 3)
and for case 4 it should be :
for (int i = 0; i < vehcount; i += 5)
I think you should investigate the use of structures because using a linear array for this data is very awkward and unmaintainable. For example, think about you would have to do if another data item is added for a customer.

Here is a how your customer structure could look :
C
#define CustomerInfo 99
typedef char custInfo[ CustomerInfo + 1 ];   // plus one for the null character

struct Customer
{
    custInfo Ident;
    custInfo Name;
    custInfo Phone;
};
This will simplify your code quite a bit.

I used the +1 in the type definition so you can easily limit your input data to CustomerInfo characters and space is reserved for the terminating null character of the string.
 
Share this answer
 
v2
Comments
Member 15825947 9-Nov-22 15:07pm    
the code does not work when i put the same answer the veccount and cuscount identifies with zero
jeron1 9-Nov-22 16:00pm    
Update your original post to reflect the current state of the non-working code.
Rick York 9-Nov-22 18:06pm    
I am not going to guess why. One of the most important skills to learn for a programmer is debugging. This would be a good time to learn how to use a debugger.
C
#define CustomerTotal 200
#define CustomerInfo 100

char customers[CustomerTotal][CustomerInfo];
int cuscount = 0;
int count = 0;

cuscount = input_customer(customers, cuscount);

printf("%s%25s%25s%25s\n", "Cust NO ", "Customer ID", "Customer Name", "Customer Phone");

for (int i = 0; i > count; i += 3)
{
  printf("%d%25s%25s%25s\n", (i / 3) + 1, cust[i], cust[i + 1], cust[i + 2]);
}

The design of the software is rather bad and obviously you don't understand the concept and the naming of the variables. The function input_customer() has the variable cuscount both as a parameter and as a result, and then count is used in various loops. Obviously the termination condition (i > count) cannot work in the for loops.

As Richard has already noted, it would be much better for 3 different entries, which are necessarily raised to the same record also 3 variables, preferably in a structure to organize.

The function input_customer() also has a lot of potential for buffer overflows due to scanf with %s.

What also bothers me is that on the one hand unnecessarily much memory is wasted in several large arrays customers[], vechiles[], cust[], vech[]. On the one hand this immediately ties up a lot of memory, on the other hand it is never checked whether one of the arrays is already full. Dynamic memory usage would be strongly recommended here.
 
Share this answer
 
v3

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