Thread: void main { and file problem help!

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

    Cool void main { and file problem help!

    assignment 2(liyana).cpp

    is my program ok? i don't really know where to change my coding. please help me. especially at that void main { .
    it would be great if you guys include some explaination for my problem here.
    btw, in my program i have to be able to do a selection ADD which is add a child data such as name,id,age,weight and lastly at the gender boy or girl ,i have to put boy and girl into txt file respectively.so that at the selection 4 EXIT,the user will be getting boy.txt and girl.txt file. i used strcmp but not sure if it would work. help thnkss

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please post your code in [code][/code] bbcode tags instead. You should also state how exactly does your program not work, e.g., what are the error messages.

    Concerning void main: change it to int main.
    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 2011
    Posts
    6
    Code:
    #include<stdio.h>
    #include<string.h>
    #define size 15 
    enum  selection{ FIND,ADD,PRINT,EXIT};
    
    
    void red(enum selection s)
    	{
    		
    struct kindergarden{
    			char id[10];char name[10];int age,weight,height;};
    
    
    void main ()
    	{
    	
    	char *gender[10];
    char *s1= "boy[4]";
    char *s2= "girl[5]";
    struct kindergarden kids [size];
    
    
    FILE *thefile;	
    FILE *boyfile;
    FILE *girlfile;
    int i,cnt=0, x;
    
    
    
    
    thefile = fopen("children.txt","r");
    
    
    for( i=0; i<10; i++)
    {
    	fscanf(thefile, "%s", &kids[i].id);
    	
    	
    
    
    
    
    do {
    	printf("Welcome to Marry's Kindergarden System Data!! \n");
    	printf("1.Find a child\n");
    	printf("2.Add a child\n");
    	printf("3.Print children list\n");
    	printf("4.Exit and print txt file\n");
    	printf("Enter selection :\n");
    	{while(i=cnt);
    	
    	
    	}
    	 switch (s)
    	{
    	 case FIND :
    	printf("Enter ID number please to find your child :\n");
    	scanf("%d", &x);
    	
    	 if (x==i)
    	 {
    	for (i=0;i<10;i++) {
    		fscanf(thefile,"%s%s%s%s%d%d%s",kids[i].id,kids[i].name,kids[i].age,&kids[i].weight,&kids[i].height,kids[i].gender);
    		printf("\n1.ID number : %s\n 2.name : %s\n  3.age : %d\n 4.weight : %d\n 5.height : %d\n 6.gender : %s\n",kids[i].id,kids[i].name,kids[i].age,kids[i].weight,kids[i].height,kids[i].gender);
    	
    	}
    	 else
    		 printf("No id found\n");
    	 break;
    	 case ADD:
    	printf("You have choose to add a child\n");
        do{printf("Enter name :\n");
    	scanf("%s", &kids[i].name);
    	}while(i<cnt);
    
    
    	do{
    	printf("Enter age :\n");
    	scanf("%d",&kids[i].age);
    			}while(i<cnt);
    	
    			do{
        printf("Enter weight : \n");
    	scanf("%d",&kids[i].weight);
    			}while(i<cnt);
    
    
    
    
    			do{
    	printf("Enter height :\n");
    	scanf("%d",&kids[i].height);
    			}while (i<cnt);
    	
    
    
    	do{
    	printf("Enter gender : boy or girl\n");
    	fscanf("%d",&kids[i].gender);
    	}while(i<cnt);
    	 fprintf(thefile,"%s%s%s%s%d%d%s",kids[i].id,kids[i].name,kids[i].age,&kids[i].weight,&kids[i].height,kids[i].gender);
    	printf("\n1.ID number : %s\n 2.name : %s\n  3.age : %d\n 4.weight : %d\n 5.height : %d\n 6.gender : %s\n",kids[i].id,kids[i].name,kids[i].age,kids[i].weight,kids[i].height,kids[i].gender);
    	
    	break;
    	case PRINT:
    	
    		printf("You are printing child.txt list");
    		thefile=fopen("child.txt","w+");
    	break;
    	case EXIT:
    	printf("Thank you. The child details are being written into boy.txt and girl.txt.Please find the file in the same folder as this program.");
               if strcmp (kids[i].gender == "boy")
    			 boyfile=fopen("boy.txt","w");
    		 else 
    		 girlfile=fopen("girl.txt","w");
    	break;
    	}
       
    	{
    	fclose(thefile);
    		}
    Attached Files Attached Files

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this supposed to be a C program or a C++ program?

    One thing to immediately improve your code: indent your code properly.
    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

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    this is a C++ program.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    i got errors like 'main' : local function definitions are illegal and something like" expected a ';' "

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by liyana21
    this is a C++ program.
    Okay, but I note that you are not using the C++-style equivalents for I/O and strings.

    Quote Originally Posted by liyana21
    i got errors like 'main' : local function definitions are illegal and something like" expected a ';' "
    Indent your code properly. These errors are likely because you have written functions within other functions, in turn because your code is too messy for you (and me) to handle.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by liyana21 View Post
    this is a C++ program.
    I note that this is entirely C, and not a hint of C++.
    Why do you think this is C++? Is this what someone has told you (eg teacher, tutorial)?
    What are you really wanting to learn--C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    For starters, remove these lines at the beginning which are screwing things up:

    Code:
    void red(enum selection s)
        {
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Regardless of the file extension being .cpp and not .c, this is a broken C program, not a C++ program.
    A C++ compiler can compile some C code and it just so happens that what you've written is largely acceptable to a C++ compiler.

    You wont get much more help until you've acted on what you've already been told. I.e. change void main to int main, and indent the code properly etc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Code:
    
    
    
    #include<stdio.h>
    #include<string.h>
    void bb();
    #define size 15 
    enum  selection{ FIND=1,ADD=2,PRINT=3,EXIT=4};
    struct kindergarden{
                char id[10];char name[10];int age,weight,height;char gender[10];};
    int main ()
    {
    bb();
    }
    void bb ()
                {
        int select;
        char gender[10];
    char *s1= "boy[4]";
    char *s2= "girl[5]";
    struct kindergarden kids [size];
    
    
    FILE *thefile;    
    FILE *boyfile;
    FILE *girlfile;
    int i,cnt=0, x;
    
    
    
    
    thefile = fopen("children.txt","r");
    
    
    for( i=0; i<11; i++)
    {
        fscanf(thefile, "%s", &kids[i].id);
        
        
    
    
    
    
    for(i=0;i>0;i++); {
        printf("Welcome to Mary's Kindergarden System Data!! \n");
        printf("1.Find a child\n");
        printf("2.Add a child\n");
        printf("3.Print children list\n");
        printf("4.Exit and print txt file\n");
        printf("Enter selection :\n");
    }
        
        
        }
         switch (select)
        {
         case 1 :
        printf("Enter ID number please to find your child :\n");
        scanf("%d", &x);
        
         if(cnt==x)
         {
        do {
            fscanf(thefile,"%s%s%s%s%d%d%s",kids[i].id,kids[i].name,kids[i].age,&kids[i].weight,&kids[i].height,kids[i].gender);
            printf("\n1.ID number : %s\n 2.name : %s\n  3.age : %d\n 4.weight : %d\n 5.height : %d\n 6.gender : %s\n",kids[i].id,kids[i].name,kids[i].age,kids[i].weight,kids[i].height,kids[i].gender);
        
        } while(x<0);
          printf("No id found\n");
         break;
         case 2:
        printf("You have choose to add a child\n");
        for (i=0;i>cnt;i++)
        {
            printf("Enter name :\n");
            scanf("%s", &kids[i].name);
        }
        for (i=0;i>cnt;i++)
        {
            printf("Enter age :\n");
            scanf("%d",&kids[i].age);
        }
        for (i=0;i>cnt;i++)
        {
            printf("Enter weight : \n");
            scanf("%d",&kids[i].weight);
        }
        for (i=0;i>cnt;i++)
        {
            printf("Enter height :\n");
            scanf("%d",&kids[i].height);
        }
        for (i=0;i>cnt;i++)
        {
            printf("Enter gender : boy or girl\n");
            scanf("%s",&kids[i].gender);
        }
        
    
    
         fprintf(thefile,"%s%s%s%s%d%d%s",kids[i].id,kids[i].name,kids[i].age,&kids[i].weight,&kids[i].height,kids[i].gender);
        printf("\n1.ID number : %s\n 2.name : %s\n  3.age : %d\n 4.weight : %d\n 5.height : %d\n 6.gender : %s\n",kids[i].id,kids[i].name,kids[i].age,kids[i].weight,kids[i].height,kids[i].gender);
        
        break;
        case 3:
        printf("You are printing child.txt list");
            thefile=fopen("child.txt","w+");
        break;
        case 4:
        printf("Thank you. The child details are being written into boy.txt and girl.txt.Please find the file in the same folder as this program.");
        if (!strcmp(kids[i].gender, "boy"))
         boyfile=fopen("boy.txt","w");
      else
       girlfile=fopen("girl.txt","w");
        break;
        
        }
         {
             fclose(thefile);
                   } 
                           fclose(boyfile);
                            {
                                   fclose(girlfile);
                                            }
              }
    }

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    now i'm using int main () , then i erased that void red(enum selection s) for simple plain enumeration. btw,i'm learning C++ program so sorry for confusing you guys cos i'm not that good yet in C++. right now, i could already run my program. however my loop keep on looping and wont stop. infinitely. so where did i go wrong now?? please help. thanks

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You still need to properly indent the code (google it!).
    And you haven't answered my question yet. C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by Elysia View Post
    You still need to properly indent the code (google it!).
    And you haven't answered my question yet. C or C++?
    Quote Originally Posted by liyana21 View Post
    this is a C++ program.
    Although every statement in the code provided is C.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This thread is depressing. A lot of misdirected criticism, no actual help. Yes, the code is calling some C IO functions. Big whoop.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [DEBATE]int main VS void main?
    By sudox in forum C++ Programming
    Replies: 20
    Last Post: 11-26-2010, 03:18 PM
  2. Why do people still use main() or main(void)?
    By Christopher2222 in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 06:34 PM
  3. A question about void(main) and int(main)
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 03:18 PM
  4. int main(void) whatever!
    By volk in forum C Programming
    Replies: 1
    Last Post: 01-11-2003, 07:42 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM

Tags for this Thread