if-else statement condition

This is a discussion on if-else statement condition within the C Programming forums, part of the General Programming Boards category; Hi, i got problem with my coding i'm using structure array because of the question of my assignment. So i ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    8

    if-else statement condition

    Hi, i got problem with my coding
    i'm using structure array because of the question of my assignment.
    So i have testing for many hour already and i'm still stuck.
    I want the code just printing the list[0] and list[1] index but it's still printing the list[3] index also, i have found the problem which is my if-else condition does not work..it is still compile but it does'nt work

    Code:
        
    printf("\n--Current Student Information--\n");    
    for(int i = 0;i<4;i++){
            if(list[i].name != NULL){
                printf("\nPrintName: %s\nPrintAge: %d\n",list[i].name,list[i].age);
                *numOfCus = *numOfStu + 1;
            }else
                return(*numOfStu);
    }
    Please sharing your experience..Thank you so much
    Last edited by YouMe; 07-04-2012 at 04:07 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by YouMe
    I want the code just printing the list[0] and list[1] index but it's still printing the list[3] index
    That is because your loop condition is i < 4 rather than i < 3.

    Quote Originally Posted by YouMe
    also, i have found the problem which is my if-else condition does not work..it is still compile but it does'nt work
    Probably because you should be using strcmp to compare with an empty string instead. (Or just test if list[i].name[0] == '\0')
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    Quote Originally Posted by laserlight View Post
    That is because your loop condition is i < 4 rather than i < 3.

    Probably because you should be using strcmp to compare with an empty string instead. (Or just test if list[i].name[0] == '\0')
    Oh The loop because i entered the structure array info for list[0] and list[1]..so i thought after entering the for-loop and then entering the if-else statement it will return to the main function

    it mean when list[i].name is not equal to NULL..it will enter the if-else..so for now the situation list[0],list[1] now have the information
    list[2] and list[3] does not have the info...it will printout the list[0] and list[1] and then return to the main function after the if-else is false..
    so that's why i stuck..and just now you say using list[i].name[0] == '\0'...still ignored the condition..
    Last edited by YouMe; 07-04-2012 at 04:37 AM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    O_o

    I'm not following.

    Are you saying you want the loop to stop looping at the first index that references a "NULL" string?

    Soma

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    ok like this

    Situation:
    list[0] and list[1] index has the information..already input list[0].name and others..

    So,
    Code:
    printf("\n--Current Student Information--\n");
    for(int i = 0;i<4;i++){ if(list[i].name != NULL){ printf("\nPrintName: %s\nPrintAge: %d\n",list[i].name,list[i].age); *numOfCus = *numOfStu + 1; }else return(*numOfStu);
    }
    so the first looping will become for list[0] correct?
    then check the condition if the list[0].name is not equal to NULL ( it's mean the array is not empty)
    It's will enter the if-else when the condition is true becuase list[0].name have string in it..so it's not empty
    the print all the Name,Age for the list[0]
    Out from the if-else statement

    now for list[1]
    (doing the same thing)

    now for list[2]
    Not contain anything, no string...so it's false
    so return(numOfCus); will executed to return to the main function..

    now my problem is..the code printing all over from list[0],list[1],list[2],list[3]...although list[2] and [3] not contain anything..it still printing but display unknown symbol for list[2] and list[3]

    can understand it?
    Last edited by YouMe; 07-04-2012 at 04:54 AM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,382
    How are you initializing your "name" variables in the "list" struct? An uninitialized character array is not "NULL". Perhaps you need to monitor for a different parameter to determine when the loop should terminate (for instance, initialize "age" to zero and stop the loop when a listing is reached where "age" is equal to zero).

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    I don't understand the first question...can you give an example what you saying ?^^..i have problem because still new

    So, you're saying i should use list[i].age in the condition?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    It still the same although the printf is different..customer instead of student..don'nt mind that

    Code:
    struct Info {
        char name[70];
        int age;
    };
    
    int addStudents(Info list[],int *numOfStud){
        printf("--Now in addStudents Function--\n\n");
        *numOfStud = 0;
    
        for(int i = 0;i<2;i++){
            if(list[i].name != NULL){
                printf("\nIndex [%d]\n",i);
                printf("Name: ");
                scanf("%s",&list[i].name);
                printf("Age: ");
                scanf("%d",&list[i].age);
              }
        }
        
        printf("\n--Current Students Information--\n");
        for(int i = 0;i<3;i++){
            if(list[i].name != NULL){
                printf("\nPrintName: %s\nPrintAge: %d\n",list[i].name,list[i].age);
                *numOfStud = *numOfStud + 1;
            }else
                return(*numOfStud);
        }
    }
    
    void main(){
         Info list[10];
        int numOfStud;
    
    
        printf("\n--Now in Main--\n\n");
        addStudents(list,&numOfStud);
        printf("\n--Back to Main--\n\n");
        printf("Number of Students: %d\n\n",numOfStud);
    
    
        system("pause");
    }
    Output:
    Name:  Untitled1.jpg
Views: 120
Size:  88.2 KB

    Just input two index only..it still printing until 3 index

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    O_o

    Variables aren't "zeroed" by default; you are checking for an assumed value when it is unknown.

    o_O

    Actually, that's my bad. The above is true, but you are checking the address of an array on the stack so it will never be null.

    Set the values to a defined type and check for a "zero length string" as laserlight suggested earlier.

    Soma

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    Quote Originally Posted by phantomotap View Post
    O_o

    Variables aren't "zeroed" by default; you are checking for an assumed value when it is unknown.

    o_O

    Actually, that's my bad. The above is true, but you are checking the address of an array on the stack so it will never be null.

    Set the values to a defined type and check for a "zero length string" as laserlight suggested earlier.

    Soma
    I'm not understand, can you show where is it to correct it?..i mean an example

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,589
    This line has a warning in MinGW GCC

    Code:
    scanf("%s",&list[i].name);
    Code:
    warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[70]' [-Wformat]|
    I had to edit your code to get it to compile with a C Compiler.
    It is likely that your are using an C++ Compiler.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct Info {
        char name[70];
        int age;
    } Info;
    NOTE: main returns a int
    Read the FAQ FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    Hint: An array name is not like a normal variable name.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  13. #13
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    yes i'm using MVisualStudio2010...

    but i thought the main problem here is the if-else condition right?

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    2,589
    Quote Originally Posted by YouMe View Post
    yes i'm using MVisualStudio2010...

    but i thought the main problem here is the if-else condition right?
    The reason you are getting garbage out of list[i].name is because you never put anything valid into that location.

    Note: The if/else likely has problems, also.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  15. #15
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    haha, so complicated...
    my head is so hot right...it's been 12 hour...haha
    i'm still stuck in that function.....i still have 7 other function to go...i can't continue to other function when i'm stuck because this function is the main problem..

    Thank you for responding you guys...i appreciate..it' is really my fault because i can't understand what you trying to say to fix my code....
    i'm still new in this c-language..

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to implement this condition..
    By dswartz2 in forum C Programming
    Replies: 11
    Last Post: 05-23-2011, 02:01 PM
  2. if condition
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 05:25 AM
  3. If condition
    By rits in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 05:54 AM
  4. Complicated Condition
    By zmi in forum C++ Programming
    Replies: 4
    Last Post: 05-05-2009, 06:15 PM
  5. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 08:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21