Thread: I get an error but could not find it, please help

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    6

    Angry I get an error but could not find it, please help

    Hi guys,
    I get an error, want to sort names but i could not solve the problem.
    Please help me.

    Program code
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    int main(void) {
        char *name[5];
        char *temp;
        int i, j;
        
        for(i=0;i<5;i++) {
            printf("%d. name enter:",i+1);scanf("%s", &name[i]);     
    
    
        }
        
        for (i=0;i<5;i++){
           for (j=0;j<4;j++) {
               if (strcmp(*name[j],*name[j+1])>0)            
               {                                        
                   temp=name[j];
                   name[j]=name[j+1];
                   name[j+1]=temp;
                }
           }
         }
        
        
        for(i=0;i<5;i++)            
            puts(*name[i]);    
        
            
        getch();
        return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is this program supposed to do?
    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
    Dec 2015
    Posts
    6
    it is can't sort name, gives error

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    6
    what is the correct spelling ?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, here's the problem:
    Code:
    char *name[5];
    the above declares an array of 5 pointers to char. Great, but that means that there is no space allocated for the names. To keep it simple, I would recommend something like:
    Code:
    char name[5][100];
    This way, you at least can store names of up to a length of 99.

    Next, this is wrong:
    Code:
    scanf("%s", &name[i]);
    Combined with my previous suggestion, you would write:
    Code:
    scanf("%99s", name[i]);
    Your sort implementation looks suspect too. Your compiler may be warning you; pay attention to the warnings. Note that you cannot assign to arrays, so you might use strcpy instead.
    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

  6. #6
    Registered User
    Join Date
    Dec 2015
    Posts
    6
    Thank you very much, but when two parameterized for loop error, please can you run ?
    Here is error
    Code:
    for(i=0;i<5;i++) {       
            for (j=0;j<4;j++) {
               if (strcmp(*name[j],*name[j+1])>0)            
               {                                        
                   temp=name[j];
                   name[j]=name[j+1];
                   name[j+1]=temp;
                }
           }
         }
    Last edited by miralay; 12-12-2015 at 12:35 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to the last sentence of my previous post.
    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

  8. #8
    Registered User
    Join Date
    Dec 2015
    Posts
    6
    dev c++ ide error mesage is windows has stopped working.
    I've read, but I couldn't.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your strcmp call is wrong because the arguments are characters, but strcmp expects strings, or rather pointers to the first characters of strings.

    Your swap implementation is wrong once you change to use an array of arrays because arrays cannot be assigned. I already suggested strcpy as part of an alternative.
    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

  10. #10
    Registered User
    Join Date
    Dec 2015
    Posts
    6
    anyway, thanks a lot. I'll do it repeat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you find the error?
    By neeha_khan in forum C++ Programming
    Replies: 20
    Last Post: 04-15-2013, 07:02 AM
  2. Can't find any error
    By Khokhar in forum C Programming
    Replies: 2
    Last Post: 03-09-2013, 09:44 AM
  3. Can't seem to find the error here.
    By DavidP in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2008, 01:26 PM
  4. one error....can't find it
    By ammanbesaw in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2007, 10:36 AM
  5. Cant find the error
    By Coder87C in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 01:57 AM

Tags for this Thread