Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This program let us know which customers in our database has more than 100$ debt.

What I have tried:

C++
#include<stdio.h>

main()
{
    int i,j;
    int IDname;
    int IDfound;
    IDfound =0;
    char custIDs[10]= { 'A', 'B', 'C', 'D', 'E', 'F','G','H','I','J'};
    float custbalances[10]={99.99 , 24.0 , 131.10, 50.00 , 170.00 , 450.00 , 65 , 82.5 ,250.00 , 999 };

    printf(" ****CUSTOMER BALANCE CENTER ****\n\n\n");

    for( i = 0 ; i< strlen(custIDs) ; i++)
    {
        if(custbalances[i] > 100 )
        {
            printf(" %c is a defaulter!\n", custIDs[i]);
        }
    }

    for( j = 0 ; j< strlen(custIDs) ; j++);                   
    {
        if(custbalances[j] < 100)
        {
            printf(" %c is all good\n ", custIDs[j]);
        }
    }

    return 0;
}
Posted
Updated 5-Sep-22 21:36pm
v2

Because you have an array of 10 characters that has no null termination. The strlen function expects a pointer to a character string that is terminated with a null character. Here are the docs for it : strlen - C++ Reference[^]
 
Share this answer
 
Comments
CPallini 6-Sep-22 2:06am    
5.
In this case strlen() is the wrong option. As Rick points out, strlen() expects a C style character string. Now you could, of course, alter custIDs variable to add a nul char at the end, e.g.
C
char custIDs[11] = { 'A', 'B', 'D', 'E', 'F', 'G', 'H' 'I', 'J', 0};
but that just abuses the idea of what a custID might be and how C strings work. A better solution would be to use a #define to capture the number of customers in the "database" e.g.
C
#include <stdio.h>

#define CUST_SIZE 10

int main()
{
    int i, j;
    char custIDs[CUST_SIZE] = { 'A' ... 'J' };
    /* other definitions, etc ... */
      

    for(i = 0; i < CUST_SIZE; ++i)
    {
       /* do work */
    }

    for( j = 0; j < CUST_SIZE; ++j )
    {
       /* do other work */
    }
}


Note that we really don't need separate variables for each for loop, since the two loops are independent of each other. Better yet, if you have C99 or later (confusingly, perhaps, that's C11) you an use a for looped scoped variable, e.g.
C
for(size_t i = 0; i < CUST_SIZE < ++i)
 
Share this answer
 
Comments
CPallini 6-Sep-22 2:06am    
5.
Sharyar Javaid 6-Sep-22 11:16am    
Hey man, thanks for the solution. but the 2nd for loop still is not working. I did everything as you mentioned. what could be the reason?
k5054 6-Sep-22 13:09pm    
I seem to have missed the trailing semicolon in the line: for(j = 0; ...);. You also have a stray space at the end of the "is all good\n "; line, which may not be what you want.
Now would be a good time to get acquainted with the Debugger... It may have helped you narrow down this issue, right away...
In C, using an array of chars as a string means that the string is 0 terminated, the last char must be a 0. As already told in previous answers.

By the way, what happens if a customer have a balance of exactly 100 ?
Quote:
Why is my 2nd for loop(the one with j) is not working in this C program?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
Comments
CPallini 6-Sep-22 2:06am    
5.
Patrice T 6-Sep-22 2:24am    
Thank you
In addition to all the other suggestions, you only need a single loop:
C++
for( i = 0 ; i< CUST_SIZE ; i++) // see solution by k5054
{
    if(custbalances[i] > 100 )
    {
        printf(" %c is a defaulter!\n", custIDs[i]);
    }
    else
    {
        printf(" %c is all good\n ", custIDs[j]);
    }
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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