GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 16-Oct-2009, 10:31
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  printf("Hello World");
  getchar();
  return 0;
}


its work in the compiler...showin output Hello world....
  #12  
Old 16-Oct-2009, 10:41
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Convert to structure


Ok, let's add the structure definition to it:

CPP / C++ / C Code:
#include <stdio.h>

struct Course
{
  int CreditHours; // 2, 3, or 4
  int Mark; // 0 to 100
};

int main()
{
  printf("Hello World");
  getchar();
  return 0;
}

Now, try to compile again.
  #13  
Old 16-Oct-2009, 10:44
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


can execute without any error,,,show the output Hello World....
  #14  
Old 16-Oct-2009, 10:59
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Convert to structure


Now, let's add the Student struct
CPP / C++ / C Code:
#include <stdio.h>

struct Course
{
  int CreditHours; // 2, 3, or 4
  int Mark; // 0 to 100
};

struct Student
{
  char Name[32]; // Up to 31 characters long
  Course Courses[3]; // An array of 3 courses
};

int main()
{
  printf("Hello World");
  getchar();
  return 0;
}

My best guess is that this will give you the error. If so, change it to:
CPP / C++ / C Code:
struct Student
{
  char Name[32]; // Up to 31 characters long
  struct Course Courses[3]; // An array of 3 courses
};
  #15  
Old 16-Oct-2009, 11:12
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


its executable if use

CPP / C++ / C Code:
struct Student
{
  char Name[32]; // Up to 31 characters long
  struct Course Courses[3]; // An array of 3 courses
};



and the output is Hello World
  #16  
Old 16-Oct-2009, 19:01
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Convert to structure


Well keep on adding stuff like that until it breaks!
Then fix it.
If you have trouble fixing it post back with the code and error.
  #17  
Old 16-Oct-2009, 21:58
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

struct Course
{
  int CreditHours; 
  int Mark; 
};

struct Student
{
  char Name[32];
  struct Course Courses[3]; 
};

{
 void PrintName(Student*student)
}
{
  int AddMarks(Student *student);

  int sum = 0;
  for(int i=0;i<3;++i) sum += student->Courses[i].Mark;
  return sum;
}

  int main()
{
  
  struct Student s ;
  strcpy(s.Name, "chekers");
  
  s.Courses[0].CreditHours = 3;
  s.Courses[0].Mark = 68;

  s.Courses[1].CreditHours = 2;
  s.Courses[1].Mark = 55;

  s.Courses[2].CreditHours = 4;
  s.Courses[2].Mark = 81;

  printf("Sum of Marks = %d",AddMarks(&s));
  getchar();
  return 0;
}


the errors shown here is

Code:
Compiling... m.c C:\Users\Viper\Desktop\m.c(16) : error C2449: found '{' at file scope (missing function header?) C:\Users\Viper\Desktop\m.c(18) : error C2059: syntax error : '}' C:\Users\Viper\Desktop\m.c(42) : warning C4013: 'AddMarks' undefined; assuming extern returning int Error executing cl.exe. m.exe - 2 error(s), 1 warning(s)
  #18  
Old 16-Oct-2009, 22:48
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Convert to structure


No, you added a whole bunch of wrong stuff.
BUILD your program ONE piece at a time.

1- Go back to where you had a good compile above.

2- add ONE statement.
3- compile and run.
Repeat 2 and 3 until you get the first problem and post us then.
  #19  
Old 17-Oct-2009, 10:49
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


ok...let start one by one...now i added 2 statement there....and shows 2 error.

CPP / C++ / C Code:
#include <stdio.h>

struct Course
{
  int CreditHours; // 2, 3, or 4
  int Mark; // 0 to 100
};

struct Student
{
  char Name[32]; // Up to 31 characters long
  struct Course Courses[3]; // An array of 3 courses
};
{
 void PrintName(Student*student)
}
{
  int AddMarks(Student *student);

  int sum = 0;
  for(int i=0;i<3;++i) sum += student->Courses[i].Mark;
  return sum;
}
int main()
{
  printf("Hello World");
  getchar();
  return 0;
}


Before add this statement ,there is no error...
CPP / C++ / C Code:
{
 void PrintName(Student*student)
}
{
  int AddMarks(Student *student);

  int sum = 0;
  for(int i=0;i<3;++i) sum += student->Courses[i].Mark;
  return sum;
}

After add the above one...its show 2 error that is

Compiling...
g.c
C:\Users\Viper\Desktop\g.c(14) : error C2449: found '{' at file scope (missing function header?)
C:\Users\Viper\Desktop\g.c(16) : error C2059: syntax error : '}'
Error executing cl.exe.

g.exe - 2 error(s), 0 warning(s)
  #20  
Old 17-Oct-2009, 14:29
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Convert to structure


Well in those two items there are several mistakes and there are some other things you should change as well.
Let's start with this...
CPP / C++ / C Code:
/* In strict C syntax 
   -this is the ONLY legal comment form. // is for C++ and others
    I know of no legal one liner for C
   -you should not declare a variable within code: */

for(int i=0;i<3;++i) sum += student->Courses[i].Mark;

/* They should always be declared at the beginning of a code block: */

int main(void)
{
  int i;
  for(i= 0; i < 3; ++i) sum += student->Courses[i].Mark;
  ...

/* also , There is no need to cram you code. Use spaces - they're cheap!
   It will be much easier to read  */
Next:
CPP / C++ / C Code:
/* A function has this form: */

return_type  function_name( input_arguments )
{
    stuff_to_be_done;
}

/* And NOT this: */     

{
 void PrintName(Student*student)
}

/* fix the curly braces */
Next:
CPP / C++ / C Code:
/* This semicolon ends the function right there... */

int AddMarks( Student *student);

/*  ... therefore the function does nothing!       */
Next:
CPP / C++ / C Code:
/* Now this is kinda tricky but hang in there with it... 
   In your function definition you are trying to declara a pointer: */      

void PrintName( Student * student)
{ stuff(); }

/* A function argument needs to have the datatype stated.  
   So , even though it is declared in the header area "Student" is NOT a 
   data "type" , it is a "data template identifier (name)" for the struct you designed.
   So you will need to state the data type of "Student".
   (Hint like you have for the "s" object in the main() of your previous code posting)
   
   You could also create a data type for the structure using a "typedef" but 
   that's kind of a separate issue. Just use the struct as you are for now, it 
   will help you to get the handle on pointers which is key.
*/
Let's see how you do now.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Structure with in a structure knockout_artist C Programming Language 3 19-Dec-2007 12:08
Allocating memory to a structure Printisor C Programming Language 4 29-Jun-2007 09:00
Convert the input into square yards pjpav Java Forum 1 08-Oct-2005 08:43
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:33.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.