Thread: I dont know where i did mistake.Help plz

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    72

    I dont know where i did mistake.Help plz

    hi friends;
    program asks to user " FirstName ,Secondname, Surname, IdNumber" the user input the answer for example;"thomas edison carter 123456" there must be one space between every answer.After user press "enter" and the same question comes again,(program asks to user " FirstName ,Secondname, Surname, IdNumber").this time user input different answers for example:
    julia robert ang 132 /*After presses enter*/
    the new question comes.The maximum name surname second name and idnumber digits only can be 6.

    for example i inputed these answers

    Code:
    FirstName ,Secondname, Surname, IdNumber :thomas edison carter 123456
    FirstName ,Secondname, Surname, IdNumber :julia robert ang 132
    FirstName ,Secondname, Surname, IdNumber :shio angela pars 1534
    FirstName ,Secondname, Surname, IdNumber :tommy sabri zahan 53
    FirstName ,Secondname, Surname, IdNumber :smart idea is 3245
    ------------------------------------------

    this question will be answered 5 times. And the computer write all the answers in its memory and after all the questions are answered,
    the program will show them as

    Code:
    thomas edison carter 123456
    julia  robert ang    132   
    shio   angela pars   1534  
    tommy  sabri  zahan  53    
    smart  idea   is     3245

    my problem is this,
    i did all these things but, in the example above but this answer comes to screen

    Code:
    thomas edison carter 12345j
    julia robert 132          s
    shio angela pars 1534     t
    tommy sabri zahan 53      s
    smart ideaais 3245 is 3245


    My wrote code is :



    Code:
    #include <stdio.h>
    int main()
    {
        
        int i,i1,i2,i3,length_dizi,cyclex;
        int spacex=0;
        char dizi[26];
        char matrx[4][26];
    
    
    for (cyclex=0;cyclex<5;cyclex++)
    {
        for(i=0;i<=26;i++)
        {
            matrx[cyclex][i]=' '; /*All matrix is space*/
            dizi[i]=' ';         /*All array is space*/
        }
    
    
        printf("1.Name, 2.Name, Surname, IDnumber :");
        gets(dizi);
    
    
       for(i=0;dizi[i]!='\0';i++) /*number of array*/
       {
         length_dizi=i;
       }
    
    
       spacex=0;
       for(i=0;i<=length_dizi;i++){
        if(dizi[i]==' ') spacex++;
    
    
    
    
    
    
          if((dizi[i]!=' ')&&(spacex==0)&&(i<7))  {matrx[cyclex][i]=dizi[i];}
          if((dizi[i]!=' ')&&(spacex==1)&&(i<14)) {matrx[cyclex][i]=dizi[i];}  
          if((dizi[i]!=' ')&&(spacex==2)&&(i<21)) {matrx[cyclex][i]=dizi[i];}
          if((dizi[i]!=' ')&&(spacex==3)&&(i<27)) {matrx[cyclex][i]=dizi[i];}
    
    
    
    
       }
    }
    for(i=0;i<=4;i++)
    {
       for(i2=0;i2<27;i2++) {printf("%c",matrx[i][i2]);}
    printf("\n");
    }
    getche();
    }
    Last edited by rac1; 11-19-2011 at 10:00 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Exactly what are you trying to accomplish?
    What is it doing that is not in line with your goal?
    What errors is it giving you?

    Please don't come in here posting code without giving us some idea what the actual question is...

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Sorry CommonTater, i have been dealing from 11:00 to 18:00 , i didnt get the solution and my brain messy :S . i updated the topic

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmm. You seem to be using array indexes and for() loops for everything. I suppose that is fine as a beginner exercise, but, eg, there are standard functions to do stuff like this:

    Code:
        for(i=0;i<=26;i++)
        {
            matrx[cyclex][i]=' '; /*All matrix is space*/
            dizi[i]=' ';         /*All array is space*/
        }
    Namely, memset().

    This practice becomes a problem here:

    Code:
    for(i=0;i<=4;i++)
    {
       for(i2=0;i2<27;i2++) {printf("%c",matrx[i][i2]);}
    printf("\n");
    }
    Because what you've done is circumvent using c strings, which are null terminated. That's what gets() created. Which, BTW, gets() is a depreciated function -- use fgets() instead. Anyway, one issue is that there is a '\0' in there somewhere and guess what? '\0' is not printable, so it does not appear, eg, an array like this:

    Code:
    char eg[]="hello\0world";
    If printed character by character, will appear as:
    helloworld

    Now, you could probably work all this out using your existing methodology, but unless you have a really good reason to do so (like, your professor said you must use for() loops instead of standard functions), IMO you should just use the opportunity to learn C properly. Break each line into its constituent parts ("FirstName ,Secondname, Surname, IdNumber") via scanf(), and then everything will be much easier to format and work with.
    Last edited by MK27; 11-19-2011 at 10:33 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    hımm.Thank you MK27.I will apply your advises.For example i will start with writing 1 array and display its answer.You are true about thinking the program in parts, so i didn't this program in parts and i can't find where i am wrong.
    for example i know strcpy, strncmp, strlen but, teacher says you shouldnt use them.the project before this, i get zero, because the usages of codes is restricted in these projects, on the other hand , i look forums books etc , i learn but , he wants to write the codes from what we learned in the lessons.Again thanks too much to all friends.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rac1 View Post
    i know strcpy, strncmp, strlen but, teacher says you shouldnt use them.
    Your teacher is an idiot.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    Your teacher is an idiot.
    I dunno about that. Doing things this way is a decent C specific exercise, because it focuses on an itty-bitty-gritty low level perspective. Think of byte by byte state machine parsing. Fortunately, we don't have to do every simple generic task that way, of course! But: honestly, it is probably good to know, just because that's low level logic, and if it makes you uncomfortable, you should choose another language to work in.

    So this task is kind of extreme (as in: extremely tedious), but I am sure it will help get someone's head straight, vis, what manipulating memory chunks/segments/streams involves.

    Quote Originally Posted by rac1 View Post
    You are true about thinking the program in parts, so i didn't this program in parts and i can't find where i am wrong.
    for example i know strcpy, strncmp, strlen but, teacher says you shouldnt use them.
    That's that then. But you might still be able to think about it from the higher level perspective of such functions -- eg, are you allowed to write functions? If so, you can mimic them. More specifically, you could write a custom function to parse each input line into pieces. Stuff like that is an important skill because they can improve the performance of critical sections of code dramatically.

    Can you use pointers? That is a good way to "bookmark" points in a string (and you can change spaces to '\0' to further delimit). I think dealing with discrete parts here will, on the whole, be easier than man-handling the whole string.

    Hopefully this gives you some ideas:

    Code:
    #include <stdio.h>
    
    int main(void) {
    	char str[]="one two three",
    		*p[3];
    	int i = 0, e = 1;
    
    	p[0] = str;
    
    	while (1) {
    		if (str[i] == ' ') {
    			p[e++] = &str[i+1];
    			str[i] = '\0';
    		} else if (!str[i]) break;
    		i++;
    	}
    
    	for (i = 0; i < 3; i++) {
    		printf("%s\n", p[i]);
    	}
    
    	return 0;
    }
    Keep in mind while() and for() loops, and if/else() and switch() blocks, can be re-written as each other -- that is just a matter of style.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Finally, i did it works perfect Thanks too much all friends, now the big problem is this:
    program will lexicographically\numerically orders the given data.
    a: by considering the FirstName
    b: by considering the SecondName
    c: by considering the Surname
    d: by considering the Number
    user will choose a,b,c,or d and the program will order.
    i can order them but, the program will be long , if i want to order the data 'aaa' and 'aab' 'aaac'
    i fail .
    i need smart code have you any idea?
    Also Mk27 ; we learned functions but we wont use this in this project
    also i dont know pointers,
    Last edited by rac1; 11-19-2011 at 03:51 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is this how the data is arranged now?

    thomas edison carter 12345
    julia robert 132
    shio angela pars 1534
    tommy sabri zahan 53

    What about this data?
    smart ideaais 3245 is 3245

    And is it now in a char array (2 dimensional, with rows and columns), or where is it?

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    i defined my multidimensional array as, char mtrx[4][27]
    Code:
    and i want to order this datas 
    mtrx[0][0-5] =thomas
    mtrx[1][0-5] =julia
    mtrx[2][0-5] =shio
    mtrx[3][0-5] =tommy
    mtrx[4][0-5] =smart
    Code:
    i should order them like this;
    mtrx[1][0-5] =julia
    mtrx[2][0-5] =shio
    mtrx[4][0-5] =smart
    mtrx[0][0-5] =thomas
    mtrx[3][0-5] =tommy
    after all the array will be displayed in this order,
    i thought that i should create a disply[4][27]
    for example i should firstly find the min letter ('orderings are about alphabethical')
    and my disply[0][0-27]=mtrx[1][0-27] ( [0-27]= all the array of first line,i can do the equalization by for loop.Strcpy and its library wont be used in this program(''professor said'')
    after i will compare all the letters and find the min letter between mtrx[4][27].i find the first array of display so i wont compare the mtrx[1][0-27] again.
    the disply[1][0-27] and min found array (which is min) will be equal.
    My thoughts was this.But the application is fail. Because it is messy, i know there should be good and short solution for this, but what. The "What" is your thoughts.
    Thank you for your attention.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You get input for your array. You will adjust the order of the array through sorting, but you do not need another array for display. The one array of data, will do fine. Get rid of the display array, for now, at least.

    Don't worry about the display. That is a detail that can be put off for now.

    Are you able to get the input correctly, into the array (not sort it, just get it into the array?) ?
    Last edited by Adak; 11-20-2011 at 02:51 AM.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    i did all the input and displayings, now i am dealing with orderings.i send you private message and my code.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's looks good.
    Attached Images Attached Images I dont know where i did mistake.Help plz-rac1-png 

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Thanks Adak You helped me too much.
    Huge Thanks too you, i did the program. it works as yours without using stdlib,h math,h ,functions and pointers.
    i learned too much but my 25-30 hours has gone.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yep, programming takes time, and programming without the string functions, takes a lot more time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 04:41 PM
  2. What is my mistake ?
    By Freelander1983 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2007, 09:31 AM
  3. if you dont like linux threads, dont read...
    By ... in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-03-2003, 11:26 AM