/!\Help in this prog. please /!\

Discussion in 'Programmer's Corner' started by !!Miss.EE!!, May 22, 2008.

  1. !!Miss.EE!!

    Thread Starter Active Member

    Oct 17, 2007
    38
    0
    Hi all ,

    I have a project in C language ,

    I've done every thing , and it gives me 0 error :D

    However, I want to check if it works , My part depends on my friend part ,

    put I've put here structure and initialized them , but still it didn't work :(,
    can any one help me please ~~~~~~~~~~~~~~~~~~~:p

    IT WAS A DISASTER >>>> :(


    can any one finds where I am wronged please .......
     
    Last edited: May 22, 2008
  2. !!Miss.EE!!

    Thread Starter Active Member

    Oct 17, 2007
    38
    0
    please can u help before tomorrow :confused:
     
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    All that means is that you don't have any syntax errors, errors in the way you have used C. The compiler can find these easily. You probably have semantic errors, which cause the program to produce incorrect results.
    I'm not sure what you mean by friend part. Does it mean that your friend wrote part of the code?
    Instead of telling us just that it didn't work, give us some details, such as the output your program produced.
    This is not helpful at all. Provide details about what the program produced as output.

    The biggest problem I see right away is that you are initializing array variables before you actually declare them. In the following code snippets you are initializing some fields of the dept and nation array.
    Code ( (Unknown Language)):
    1.  
    2. dept[0].Dcode=123;
    3. dept[0].Name="EE";
    4.  
    Code ( (Unknown Language)):
    1.  
    2. nation[0].Ncode=321;
    3. nation[0].Name="uae";
    4.  


    Later on, in main(), you get around to the declarations of the dept and nation arrays.
    Code ( (Unknown Language)):
    1.  
    2. int main ()
    3. {
    4. int i,j,k;
    5.  
    6. per_info personal[1];
    7. dept_info dept[1];
    8. nation_info nation[1];
    9.  
    Also, I should point out that you cannot assign a string constant to a character array variable, as you're attempting to do here
    Code ( (Unknown Language)):
    1.  
    2. dept[0].Name="EE";
    3.  
    and in the person array. The assignment actually occurs, but what gets stored is the address of the string constant, not the string of characters. To assign a string value to an array variable, you need to use a string function such as strcpy().

    Another thing to check is that each call to scanf has a control string that matches the types you are inputting to. Here's one that needs work.
    Code ( (Unknown Language)):
    1.  
    2. printf("\nDate of birth:");
    3. scanf("%d%*c%d%*c%d",&personal[i].month,&personal[i].day,&personal[i].year);
    4. [/i][/i][/i]

    You have four conversion specifiers (e.g., %d, %c), but only three variables to the right of the control string. With three variables, you need to have three conversion specifiers. Also, the conversion specifiers for character array variables should be %s, not %c (you have %*c).

    Same problem in the call to scanf to get the joining date.

    If you don't understand how to use scanf very well, it would be better to input to one variable at a time, instead of trying to input to three of them in a single scanf call.

    That should get you started on the right track.

    Mark
     
  4. !!Miss.EE!!

    Thread Starter Active Member

    Oct 17, 2007
    38
    0
    yeah thanks MARK ,

    I am myself did'nt know my instructior want from all the modefied .... however





    this is to write the DOB in the form : mm/dd/yyyy


    BUT the main QUESTION now ... were I should initialize the Departments and natinalitiy structures , and how ....

    that what I want to know for now .....
     
  5. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I absolutely don't understand what you are saying here.
    Judging by something you said later in your post, you actually want to do output here, not input. If that's the case, you need to use printf, not scanf. As I said before your control string should have the same number of conversion specifiers (the %d, %c things) as variables.
    Code ( (Unknown Language)):
    1.  
    2. printf("\nDate of birth:");
    3. scanf("%d%*c%d%*c%d",&personal[i].month,&personal[i].day,&personal[i].year);
    4. [/i][/i][/i]



    The 2nd printf call below will print the month, day, and year, with slashes between, in this form: 5/21/2008. It assumes that the month, day, and year are stored as int, not strings.

    Code ( (Unknown Language)):
    1.  
    2. printf("\nDate of birth:");
    3. printf("\n%d%/%d/%d",personal[i].month,personal[i].day,personal[i].year);
    4. [/i][/i][/i]




    Since you are declaring your struct array variables in main, that's where you want to initialize them.

    Code ( (Unknown Language)):
    1.  
    2. int main ()
    3. {
    4.   int i,j,k;
    5.  
    6.   per_info personal[1];
    7.   // Initialize personal variable here
    8.  
    9.   dept_info dept[1];
    10.   dept[0].Dcode=123;
    11.   dept[0].Name="EE"; // This won't work. Need to use strcpy()
    12.  
    13.   nation_info nation[1];
    14.   nation[0].Ncode=321;
    15.   nation[0].Name="uae"; //This won't work. Need to use strcpy()
    16.  
    17.   // and so on...
    18.  
     
  6. !!Miss.EE!!

    Thread Starter Active Member

    Oct 17, 2007
    38
    0
    I'm greatful with your replay Mark.... Thanks
     
  7. !!Miss.EE!!

    Thread Starter Active Member

    Oct 17, 2007
    38
    0
    BUT WHAT IS{ strcpy() },,, Beacuse I'm freshman and I didn't use it before ??
     
  8. Caveman

    Active Member

    Apr 15, 2008
    471
    0
    Try googling strcpy. It's not that hard to find this answer on your own.
     
Loading...