Thread: Problems With C++ Code ( from a book )

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Problems With C++ Code ( from a book )

    hey guys im new here. I recently started learning c++. I was reading through my book ( Mike McGrath C++ in easy steps third edition ) when i came to compiling a program i was making from the book. i had loads of errors. yet it was copied exactly from the book.

    Here is the code:

    Code:
    #include <iostream>
    using namespace std ;
    
    // Arrays Program.
    
    int main()
    {
    
    // Program code goes here.
    
    	// Declared then initiaised.
    	float nums[3] ;
    	nums[0] = 1.5 ; nums[1] = 2.75 ; nums[2] = 3.25 ;
    
    	// Declared and initialised.
    	char name[5] = { 'm', 'i', 'k', 'e', '\0' } ;
    	int coords[2] [3] = { { 1, 2, 3 } , { 4, 5, 5 } } ;
    	}
    
    	cout << "nums[0]: " << nums[0] << endl ;
    	cout << "nums[1]: " << nums[1] << endl ;
    	cout << "nums[2]: " << nums[2] << endl ;
    	cout << "name[0]: " << name[0]<< endl ;	
    	cout << "string: " << name << endl ;
    	cout << "coords[0][2]: " << coords[0][2] << endl ;
    	cout << "coords[1][2]: " << coords[1][2] << endl ;
    
    return 0 ;
    
    }
    Here are the errors from my compiler ( MiniGW )

    Code:
    arrays.cpp:20: error: expected constructor, destructor, or type conversion befor
    e '<<' token
    arrays.cpp:21: error: expected constructor, destructor, or type conversion befor
    e '<<' token
    arrays.cpp:22: error: expected constructor, destructor, or type conversion befor
    e '<<' token
    arrays.cpp:23: error: expected constructor, destructor, or type conversion befor
    e '<<' token
    arrays.cpp:24: error: expected constructor, destructor, or type conversion befor
    e '<<' token
    arrays.cpp:25: error: expected constructor, destructor, or type conversion befor
    e '<<' token
    arrays.cpp:26: error: expected constructor, destructor, or type conversion befor
    e '<<' token
    arrays.cpp:28: error: expected unqualified-id before "return"
    arrays.cpp:30: error: expected declaration before '}' token
    if anybody could please help me that would be appriciated. i dont know if its me or the book thats making the errors. Btw if anyone else has the book its page 19

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    can anyone help me?

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    anybody help me?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Posting 3 times in under an hour is not going to expedite the help process. It, however, will annoy everyone who can help you thus causing them to silently skip this thread.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Count your open & close braces. They should be equal, but you have an extra } where it shouldn't be.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    Code:
    #include <iostream>
    using namespace std ;
    
    // Arrays Program.
    
    int main()
    {
    
    // Program code goes here.
    
    	// Declared then initiaised.
    	float nums[3] ;
    	nums[0] = 1.5 ; nums[1] = 2.75 ; nums[2] = 3.25 ;
    
    	// Declared and initialised.
    	char name[5] = { 'm', 'i', 'k', 'e', '\0' } ;
    	int coords[2] [3] = { { 1, 2, 3 } , { 4, 5, 5 } } ;
    	} // MAIN ENDS HERE
    
    	cout << "nums[0]: " << nums[0] << endl ;
    Recheck

  7. #7
    One Level Below Beginner
    Join Date
    Jul 2009
    Location
    Corner of Nowhere and Yonder
    Posts
    19
    Look at what valthyx is stating, then look back at your code,

    Keep going back and forth until you see it.

    You have one thing, you do not need.


    Also, some books are wrong on purpose, just for you (the reader) to debug and learn from....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problems.....once again
    By NicAuf in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2007, 02:55 PM
  2. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  3. looking for book with sample code
    By stella in forum C++ Programming
    Replies: 5
    Last Post: 06-25-2003, 10:48 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 03:04 PM
  5. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 02:02 AM