Thread: if-else statement condition

  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
    28,412
    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')
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    5,108
    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
    4,513
    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
    28,412
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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:
    if-else statement condition-untitled1-jpg

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

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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..

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, 09:44 PM