Thread: help with one minor error

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    help with one minor error

    ok...this is really simple (even though i can't get it). My program only has one error which i can't seem to get out. Here is my code:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    
    class person
    	{
    public:
    	person()
    	{
    		fName = "\0";
    		lName = "\0";
    		number = 0;
    	
    	~person()
    	{
    	}
    
    private:
    	char fName[40];
    	char lName[40];
    	int number;
    };
    
    int main()
    {
    	
    	for(;;)
    	{
    		int choice = 0;
    		person p;
    		cout << "Enter the person's first name: ";
    		cin >> p.fName;
    		cout "\nEnter the person's last name: ";
    		cin >> p.lName;
    		cout << "\nEnter the person's telephone number: ";
    		cin >> p.number;
    
    		file("data.txt", ios::ate);
    
    		file << "\nID = ";
    		file << i;
    		file << "\nName: ";
    		file << fName;
    		file << " ";
    		file << lName;
    		file << "\nTelephone Number: ";
    		file << number;
    		file << "-----------------------------------\n\n";
    
    		cout << "Data stored in data.txt...\n";
    		cout << "Enter another?(y or n) ";
    		cin >> choice;
    
    		if(choice == 'n')
    		{
    			break;
    		}
    	}
    	return 0;
    }
    and i keep getting this error:
    Code:
    fatal error C1004: unexpected end of file found
    This error is pointing to just under the last close brace. I have tried everything like adding a brace/adding several braces and i have tried to take it away, but that doesn't work. BTW, i am using MSVC++ 6.0

    Thanks
    -Chris

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Where is the ending '}' after the constructor in the person-class?
    // Gliptic

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Smile I donīt get it...

    As I see it there must be more then one error, or ther will come more when you correct the missing }. Because you have made the class variables private, and trying to directly acces them with the object of the class. I might have missed something but as I see it, if you want to do that you must make the variables public, or make functions in the class that, from wich you can access them.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Yeah, I saw that too. I thought I maybe had misunderstood it so I didn't say anything.
    // Gliptic

  5. #5
    monotonously living Dissata's Avatar
    Join Date
    Aug 2001
    Posts
    341
    you have a semicolon after the } on you private constructor

    private:
    char fName[40];
    char lName[40];
    int number;
    };


    ^ the semi colon
    if a contradiction was contradicted would that contradition contradict the origional crontradiction?

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    My concerns include:

    At the end of the declaration of a class/struct interface there should be a semicolon, so I think the semicolon issue brought up is in error, not the code as written, on that issue.

    I agree with the missing } at the end of the default constructor.

    I agree with need for public accessor/mutator functions to when using private member class variables within main().

    You don't declare the variables i or file anywhere, yet you are using tbem.

    the variable i is never incremented, I suspect it should be based on what I think your program is trying to do.

    you declare the variable choice to be of type int but you assign it a char. This may cause a problem, may not. But unless you know what is happening I suggest you keep variable type matching as simple as you can.

    You may need to use strcpy() in your constructor if the compiler feels the = sign means assignment rather than intialization in the function body. If it works as is, fine. If not, that's a change I would make.

    So I agree. You are likely to uncover other errors once you fix the first one.

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    sorry about all of the dumb errors, guys but this code was written up pretty fast and wasn't given much thought so I decided to fix everything up except I can't fix this (crazy) error that I keep getting.

    My new code:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    
    class person
    	{
    public:
    	person()
    	{
    		
    		number = 0;
    	}
    	
    	~person()
    	{
    	}
    
    
    	char fName[40];
    	char lName[40];
    	int number;
    };
    
    person p;
    int i = 1;
    int main()
    {
    	
    	for(;;)
    	{
    		int choice = 0;
    		person p;
    		cout << "Enter the person's first name: ";
    		cin >> p.fName;
    		cout "\nEnter the person's last name: ";
    		cin >> p.lName;
    		cout << "\nEnter the person's telephone number: ";
    		cin >> p.number;
    
    		ofstream file("data.txt", ios::ate);
    
    		file << "\nID = ";
    		file << i;
    		file << "\nName: ";
    		file << p.fName;
    		file << " ";
    		file << p.lName;
    		file << "\nTelephone Number: ";
    		file << p.number;
    		file << "-----------------------------------\n\n";
    
    		i++;
            cout << "Data stored in data.txt...\n";
    		cout << "Enter another?(y or n) ";
    		cin >> choice;
    
    		if(choice == 'n')
    		{
    			break;
    		}
    	}
    	return 0;
    }
    And here is the error:
    Code:
      error C2143: syntax error : missing ';' before 'string'
    My response to this error:
    "...WHAT THE...??????"

    The error happend on this line:
    Code:
     		cout "\nEnter the person's last name: ";
    I know, I haven't got the slightest clue either but has anybody go any ideas?

  8. #8
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Talking

    cout "\nEnter the person's last name: ";


    don't you mean:

    cout << "\nEnter the person's last name: ";

    You forgot the <<.

    The errors don't always tell you exactly where the error is. Sometimes you have to look for them a few lines up.

    Anyway, fix the << and it should work. It worked for me

    Thanks,
    Marc
    No matter how much you know, you NEVER know enough.

  9. #9
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    WHOA!!!
    How the hell could I have missed THAT?? Thanks for pointing out that (SILLY) mistake....

  10. #10
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Talking

    no problem
    No matter how much you know, you NEVER know enough.

  11. #11
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    this is dragging on a bit, isn't it? Well I have another problem but first, here is my new code...
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    
    class person
    	{
    public:
    	person()
    	{
    		
    		number = 0;
    	}
    	
    	~person()
    	{
    	}
    
    
    	char fName[40];
    	char lName[40];
    	int number;
    };
    
    int choice = 0;
    person p;
    int i = 1;
    int main()
    {
    	
    	for(;;)
    	{
    		
    		cout << "Enter the person's first name: ";
    		cin >> p.fName;
    		cout << "\nEnter the person's last name: ";
    		cin >> p.lName;
    		cout << "\nEnter the person's telephone number: ";
    		cin >> p.number;
    
    		ofstream file("data.txt", ios::ate);
    
    		file << "\nEntry # ";
    		file << i;
    		file << "\nName: ";
    		file << p.fName;
    		file << " ";
    		file << p.lName;
    		file << "\nTelephone Number: ";
    		file << p.number;
    		file << "\n-----------------------------------\n\n";
    
    		i++;
            cout << "Data stored in data.txt...\n";
    		cout << "Enter another?(y or n) ";
    		cin >> choice;
    
    		if (choice == 'n')
    		{
    			return 0;
    		}
    	
    }
    	return 0;
    }
    My new problem is that at the end of my prog (when it asks you to input a 'y' or an 'n') it goes CRAZY! Just compile it and take a look!

    Any help will be apprieciated (is that spelt right...?)

  12. #12
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Talking

    Naughty!.. what's this?.. int choice = 0;
    And you're trying to input a character right?

    Well, you must replace int choice = 0; with char choice = 'x';

    It should work now

    Hope this helps,
    Marc
    No matter how much you know, you NEVER know enough.

  13. #13
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    oh another thing

    Why don't you use a do...while loop?

    do {
    //code
    }while (choice != 'n');

    The code will be much cleaner that way
    No matter how much you know, you NEVER know enough.

  14. #14
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    force of habit - I usualy say "...press a 1, 2 or 3 bla bla bla..." in a switch statment. That must be why I did that

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    it doesn't matter if you use char or int, you can do the same thing with both.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed