Thread: Help with arrays

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    7

    Help with arrays

    How would i get the following code to print 'error' or 'true' only once after the whole array comparison in task 2 instead of printing after comparing each section of the array?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    int main() {
    
    
    char name_array[10][15];
    const char * name[15];
    int i;
    
    
        task1:
            for(i=0;i<10;i++){
                printf("\nEnter your name:\n");
                scanf("%s", name_array[i]);
            }
    
    
    
    
        task2:
            system("cls");
            printf("Enter your name:   ");
            scanf("%s", name);
    
    
            for(i=0;i<10;i++){
    
    
                if (strcmp(name_array[i], name)!=0){
                    printf("error\n");
                } else if (strcmp(name_array[i], name)==0){
                    printf("true\n");
                }
            }
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    If you want to keep processing the array once an error is found in the loop then you could add a flag and set it to 0 within the loop if there is an error (initialise it to 1 before entering the loop) and then after the loop print error or true based upon the value of the flag. E.g.

    Code:
    int success = 1;
    for (i = 0; i < 10; i++) {
        /* ... */
    
        if (strcmp(name_array[i], name) != 0) {
            success = 0;
        
        /* ... */
    }
    
    if (success != 1) {
        /* ... */
    } else {
        /* ... */
    If you don't necessarily want to keep processing the loop once an error is found then you can use the break keyword to terminate the loop early and either use it combined with the flag strategy above, or check the value of i after the loop is terminated (if i != 10 then the loop terminated early)

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    7
    thanks bro, that worked but it has a flaw. The value of 'success' is dependent on the last element of the array so even if one of the element ofthe array compared was true and the last element is false then it would still print 'error'

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Loj Buchanan View Post
    thanks bro, that worked but it has a flaw. The value of 'success' is dependent on the last element of the array so even if one of the element ofthe array compared was true and the last element is false then it would still print 'error'
    Yes, of course it does.

    Maybe paste your latest code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

Tags for this Thread