Help with parralell array of objects

This is a discussion on Help with parralell array of objects within the C++ Programming forums, part of the General Programming Boards category; ok, i was almost positive that there was nothing wrong with this code and everything, but when i compile it ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    45

    Help with parralell array of objects

    ok, i was almost positive that there was nothing wrong with this code and everything, but when i compile it i get a lot of errors that i know are wrong. does anybody know if there are some stupid mistakes im overlooking or anything?

    Code:
    #include<iostream.h>
    
    
    class Shipping
    {
    	public:
    		double rate;
    		char code;
    };
    
    void main()
    {
    	int x;
    	char method
    	Shipping info;
    	char info[3].code = {'a', 't', 'r', 'h'};
    	double info[3].rate={14.95, 10.25, 8.75, 25.99};
    	cout<<"Please enter a shipping method code: ";
    	cin>>method;
    	for(x=0; x<4; ++x)
    	{
    		if( method==info[x].code)
    			cout<<"The rate for your shipping method is $"<<info[x].rate;
    		if(x=4)
    			cout<<"You have entered an incorrect shipping method";
    	}
    }

    thanks ahead of time for any help given.

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    First you forgot a semi-colon( on line 3 of the main() function.
    Second you declared Shipping info then you redefined info to be an array of char then tried to access a member in the Shipping class with the member operator .
    Third the rest of the problems seem to stem from the second problem above.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    thanks! fixed every problem....except now i have one error and i dont have a clue what it means. on the line under the void main() statement, it has an error that says "error C2447: missing function header (old-style formal list?)"

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    That usually means a syntax error. It could be that it looks like a function to the compiler. Verify your syntax.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    wait...i put the semi colon in the wrong spot...i'm still getting all these errors! it says im missing a semi colon before the '.' in info[3].code and just about every other non-numeric/alphabetical character.

  6. #6
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Post your revised code please.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    Code:
    #include<iostream.h>
    
    
    class Shipping
    {
    	public:
    		double rate;
    		char code;
    };
    
    void main()
    {
    	int x;
    	char method;
    	Shipping info[3];
    	char info[3].code = {'a', 't', 'r', 'h'};
    	double info[3].rate={14.95, 10.25, 8.75, 25.99};
    	cout<<"Please enter a shipping method code: ";
    	cin>>method;
    	for(x=0; x<4; ++x)
    	{
    		if( method==info[x].code)
    			cout<<"The rate for your shipping method is $"<<info[x].rate;
    		if(x=4)
    			cout<<"You have entered an incorrect shipping method";
    	}
    }

  8. #8
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Remove the char.
    You cannot initialize an array of objects like that. You must do each one seperatly.
    You have reservered space for only 3 you seem to want 4, but you want to initialize only the 4 one that does not exist.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  9. #9
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Do the same with the double and the initialization of rate.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  10. #10
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Here is the revised code.
    I don't guarantee it works, just compiles.

    Code:
    #include<iostream.h>
    
    class Shipping
    {
    public:
    	double rate;
    	char code;
    };
    int main(int argc, char** argv)
    {
    	int x;
    	char method;
    	Shipping info[4];
    	
    	info[0].code = 'a';
    	info[1].code = 't';
    	info[2].code = 'r';
    	info[3].code = 'h';
    
    	info[0].rate = 14.95;
    	info[1].rate = 10.25;
    	info[2].rate =  8.75; 
    	info[3].rate = 25.99;
    	cout<<"Please enter a shipping method code: ";
    	cin>>method;
    	
    	for(x=0; x<4; ++x)
    	{
    		if( method==info[x].code)
    			cout<<"The rate for your shipping method is $"<<info[x].rate;
    		if(x==4) // you had x = 4 that is assignment
    			cout<<"You have entered an incorrect shipping method";
    	}
    	return 0; // indicate sucess
    }
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    thanks for the help!

  12. #12
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    No problem. One more thing though, never use void main() always use int main(int argc, char** argv). If you don't understand why, you will once you get a good grasp on programming.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    always use int main(int argc, char** argv)
    Or int main(). The problem with void was that other programs might check its return value. I don't believe that the using the arguments is necessary.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 12:26 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21