4 Replies - 547 Views - Last Post: 19 August 2009 - 05:35 PM Rate Topic: -----

#1 wizarD.ubit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-August 09

Array of Character in Array of Structure

Posted 19 August 2009 - 05:06 PM

Hey GuYs!

i hve got a problem with array of characters ;


PROBLEM:

There are 4 semesters in University. Store 3 names of student of lenght [15] and roll in each 4 semester?


Condition :-
1 .Semester 1
(a) Student Name(1)
(b ) Roll No of student

(a) Student Name (2)
(b ) Roll no of student

(a ) student Name (3)
(b ) Roll no of Student
.
.
.


4 .Semester 4
(a) Student Name
(b ) Roll No of student



INPUT =
1.Students Name of Length[15]
2.Roll no of students.



ERROR:

the compiler is asking the Name first time
than it is only asking the roll # .

OUTPUT

Enter the name hbk
Enter the roll # 3
Enter the name Enter the roll # (Not asking name )
Enter the name Enter the roll # (Not asking name )
Enter the name Enter the roll # (Not asking name )


---------------------------------------------------------------------------------------------

#include<stdio.h>
#include<conio.h>

void main(void)
{
clrscr();

typedef struct STUDENT
{
char name[15];
int roll[3]
};
STUDENT bscs[3];


int i,j;


for( i=0 ; i<3 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{

printf("\nENTER THE NAME PLEASE");
scanf("%s",&bscs[i].name[j]);

printf("ENTER THE ROLL #");
scanf("%f",&rawf);

}
}


getch();
}


ERROR:

the compiler is asking the Name first time
than it is only asking the roll # .

OUTPUT

Enter the name hbk
Enter the roll # 3
Enter the name Enter the roll # (Not asking name )
Enter the name Enter the roll # (Not asking name )
Enter the name Enter the roll # (Not asking name )

This post has been edited by wizarD.ubit: 19 August 2009 - 05:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array of Character in Array of Structure

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Array of Character in Array of Structure

Posted 19 August 2009 - 05:14 PM

void main() needs to be int main. See link for details

Move the structure declaration up above main() so that is not inside any function body.

scanf("%s",&bscs[i].name[j]);

name[j] is just a single character, not a pointer to a character array. That line should be scanf("%s",&bscs[i].name);. And move that line (and the previous printf()) up so that it is between the two loops.

This post has been edited by Ancient Dragon: 19 August 2009 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Array of Character in Array of Structure

Posted 19 August 2009 - 05:19 PM

Shouldn't the struct be outside the main?

And
scanf("%f",&rawf);//you should be receiving the bscs[i].roll

Was This Post Helpful? 0
  • +
  • -

#4 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Array of Character in Array of Structure

Posted 19 August 2009 - 05:28 PM

View Postponcho4all, on 19 Aug, 2009 - 04:19 PM, said:

Shouldn't the struct be outside the main?


Most programs I have seen declare then outside any function so that the structure can be referenced anywhere in the program. Most commonly structures are declared in a header file. But there are no C or C++ rules that prohibit declaring them inside a function as the OP posted. The problem with that is the structure scope is limited to the function in which it is declared -- other functions will not be able to use it.

This post has been edited by Ancient Dragon: 19 August 2009 - 05:29 PM

Was This Post Helpful? 1
  • +
  • -

#5 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Array of Character in Array of Structure

Posted 19 August 2009 - 05:35 PM

View PostAncient Dragon, on 19 Aug, 2009 - 04:28 PM, said:

View Postponcho4all, on 19 Aug, 2009 - 04:19 PM, said:

Shouldn't the struct be outside the main?


Most programs I have seen declare then outside any function so that the structure can be referenced anywhere in the program. Most commonly structures are declared in a header file. But there are no C or C++ rules that prohibit declaring them inside a function as the OP posted. The problem with that is the structure scope is limited to the function in which it is declared -- other functions will not be able to use it.


Thank You for the explanation Ancient Dragon
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1