Thread: my function doesn't work! it should work

  1. #1
    Unregistered
    Guest

    Unhappy my function doesn't work! it should work

    this is just part of my program the problem are when the program asks user to put code if the user puts nothing just press enter. the program should go to ShowData() and show all records in Manufacterer.txt in A : drive but it doesn't show all of record inside that file
    the second problem is variable calls 'position' . it works as an index so if there are 5 records in Manufacterer.txt when the user puts the new record. the position should be 5. it uses fread doesn't it? but i don't know how to find the last position in the file.

    sorry i am very new with C
    thanks very much for helping
    Code:
    #include <stdio.h>
    //#include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    
    void ShowData();
    	struct Manufacturer //creating your own data type
    	{
    		char code[6],name[16], address[21],contact[11];//63 type long
    		int position;
    		bool pass;
    	};
      main(void)
    {
    	long offset;
    	int test;
    	struct Manufacturer m;
    	m.pass=true;
    FILE *f;
    	f=fopen("a:\\Manufacturer.txt","ab");//"w" c
    
    	puts("Enter code:");
    	gets(m.code);
    	fflush(stdin);
    	printf("%d",test);
     	while (strlen(m.code)>0)
     	{
     
    		
    			puts("Enter name:");
    			gets(m.name);
    			
    			puts("Enter Address: ");
    			gets(m.address);
    			
    		
    			puts("Enter contact");
    			gets(m.contact);
    			m.position;
    			fwrite(&m, sizeof(m),1,f);
    			
       	
       		puts("Enter code");
       		gets(m.code);
       		m.position=m.position+1;
       	
    }
    	;
    fclose(f);
    
    ShowData();
    }
    	void ShowData()
    	{
    	struct Manufacturer ma;
    	int count = 1;
    	FILE *f;
    
    	
    	f=fopen("a:\\Manufacturer.txt","r+");
    		
    		while (fread(&ma,sizeof(ma),1,f)==1)
    	{
    		printf("Show");
    		printf("Record %d\n",count++);
    		puts(ma.code);
    		puts(ma.name);
    		puts(ma.address);
    		puts(ma.contact);
    		puts(ma.position);
    		printf("inside");
    	}
    	fclose(f);
    	}

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Shadow

    You don't need the semi-colon after ShowData() nor the semi-colon after the closing brace.
    You need the semi-colon after ShowData(); because it's a function prototype. And you need a semi-colon after the closing brance because it's the closing brance of the struct.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    You need the semi-colon after ShowData(); because it's a function prototype. And you need a semi-colon after the closing brance because it's the closing brance of the struct.
    You know what? You're right. Woopsy It's early...

    - edit -
    I said fooey on it. It just goes to show ya what sloppy code will do to a person.
    Last edited by Shadow; 05-02-2002 at 06:01 AM.
    The world is waiting. I must leave you now.

  4. #4
    Unregistered
    Guest
    i was in a hurry to post it but after i made it look more tidy and ran the program it still doesn't work!
    can anyone tell me what 's wrong with my program
    Thanks
    Code:
    #include <stdio.h>
    //#include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    //********* prototype******//
    void ShowData();
    	struct Manufacturer //creating your own data type
    	{
    		char code[6],name[16], address[21],contact[11];//63 type long
    		int position;
    		bool pass;
    	};
    //*********main********//	
      main(void)
    {
    	long offset;
    	int test;
    	struct Manufacturer m;
    	m.pass=true;
    	FILE *f;
    	f=fopen("a:\\Manufacturer.txt","ab");//"w" c
    
    	puts("Enter code:");
    	gets(m.code);
    	fflush(stdin);
    	printf("%d",test);
     	while (strlen(m.code)>0)
     	{
     			puts("Enter name:");
    			gets(m.name);
    			puts("Enter Address: ");
    			gets(m.address);
    			puts("Enter contact");
    			gets(m.contact);
    			m.position;
    			fwrite(&m, sizeof(m),1,f);
    			puts("Enter code");
       		gets(m.code);
       		m.position=m.position+1;
       }
    	fclose(f);
    	ShowData();
    }
    
    //*******function************//
    	void ShowData()
    	{
    	struct Manufacturer ma;
    	int count = 1;
    	FILE *f;
    
    	f=fopen("a:\\Manufacturer.txt","r+");
    		
    		while (fread(&ma,sizeof(ma),1,f)==1)
    		{
    		printf("Show");
    		printf("Record %d\n",count++);
    		puts(ma.code);
    		puts(ma.name);
    		puts(ma.address);
    		puts(ma.contact);
    		puts(ma.position);
    		printf("inside");
    		}
    	fclose(f);
    }

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Remove m.position; in your while loop and initialize m.position.
    Code:
    long offset;
    int test;
    struct Manufacturer m;
    m.pass=true;
    FILE *f;
    m.position = 0;
    Use printf to print an integer:
    Code:
    printf("%d\n", ma.position);

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try this:


    Code:
    
    #include <stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    //********* prototype******//
    void ShowData();
    bool isEmpty( char *str );
    
    	struct Manufacturer //creating your own data type
    	{
    		char code[6],name[16], address[21],contact[11];//63 type long
    		int position;
    		bool pass;
    	};
    //*********main********//	
      int main(void)
    {
    #define UNDEFINED 12345
    
    
                    int test = UNDEFINED; //...hmmm...unused...
    	long offsettest = UNDEFINED; //  ...unused...
    	int increment = 0;
    	struct Manufacturer m;
                    FILE *f = fopen("a:\\Manufacturer.txt","wb");//no such "ab", see below...
    fseek(f, 0, SEEK_END); ///...to append...
    
    	printf("%d",test);
    
    while (isEmpty(m.code) == false)
    {
    memset(m, 0, sizeof(struct Manufacturer) ); //...initialize all to zero...
    
     			puts("Enter name:");
    			gets(m.name);
    			puts("Enter Address: ");
    			gets(m.address);
    			puts("Enter contact");
    			gets(m.contact);		
    			puts("Enter code");
       		                gets(m.code);
       		                m.position = ++increment;
                                                    m.pass=true;
    
                                                    fwrite(&m, sizeof(m),1,f);
       }
    	fclose(f);
    	ShowData();
    }
    
    //*******function************//
    	void ShowData()
    	{
    	struct Manufacturer ma;
    	int count = 1;
    	FILE *f;
    
    	f=fopen("a:\\Manufacturer.txt","rb");
    		
    		while (fread(&ma,sizeof(ma),1,f)==1)
    		{
    		printf("Show");
    		printf("Record %d\n",count++);
    		puts(ma.code);
    		puts(ma.name);
    		puts(ma.address);
    		puts(ma.contact);
    		printf("%i", ma.position); //..."puts" is for strings only...
    		printf("inside");
                                    getch();
    		}
    	fclose(f);
    }
    
    
    
    
    bool isEmpty( char *str ){
    
    int len = strlen(str);
    int i = 0;
    
    while(i < len)
     {
      if( !isspace(str[i]) && str[i] != '\n' )
       return false;
    
     i++;
     }
    
    return true;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Two comments

    1. This is very bad
    gets(m.code);
    fflush(stdin);

    gets offers no protection against buffer overflow, and fflush(stdin) is undefined (it might work for you, but it will not work for me).

    Search the board for endless discussions

    2. bool pass;
    Now you're writing C++
    There is no bool type in C

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Note: Salem thinks C++ is the Devils Language...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    All too many people can't tell the difference

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >There is no bool type in C
    In C99 there is a bool type, but for the time being it's best to conform to C89 until the new standard becomes more widespread.

    >Note: Salem thinks C++ is the Devils Language...
    Well, seeing as how so many people think C++ and C are the same thing...

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Is it bool or BOOL?..I know in VC6.0/mfc I use BOOL to get it to work but what does the standard say?

  12. #12
    Unregistered
    Guest
    BOOL is a typedef for an int. Depending on your compiler, it will occupy either 2 or 4 bytes of memory. On the other hand, the C++ bool uses a single bit. I think

    BTW: who says C++ is not C?

  13. #13
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    #include

    include <stdbool.h> is missing in the source code!
    Ünicode¬>world = 10.0£

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is it bool or BOOL?..I know in VC6.0/mfc I use BOOL to get it to
    >work but what does the standard say?
    BOOL is a typedef declared in windows.h, it's not standard.

    >BTW: who says C++ is not C?
    Um...everyone who knows what they're talking about.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM