Error in this program.

Error in this program.

Page 1 of 1

9 Replies - 794 Views - Last Post: 16 January 2010 - 06:00 AM Rate Topic: -----

#1 kramness  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-December 09

Error in this program.

Posted 16 January 2010 - 02:02 AM

can u help me find the error in this program ?
this is magic 8 game

#include <stdlib.h>
#include <time.h>
#include <iostream.h>

template <typename T, int N>
char (&array(T(&)[N]))[N]; // Error

int main()
{
  const char *messages[] = {
    "I'm a little teapot",
    "This is a message",
    "If you can read this, you're too close",
    "Han shot first",
    "What are you looking at?"
  };


  srand ( (unsigned)time ( 0 ) );

  for ( int i = 0; i < 10; i++ ) {

   [color="#FF0000"] int r = rand() % sizeof array ( messages );[/color]

    cout<< messages[r] <<'\n';

  }


return 0;
}


the red fonts has an error on it. can u help me ? thank you

*** MOD EDIT: Added code tags. Please :code: ***

Is This A Good Question/Topic? 0
  • +

Replies To: Error in this program.

#2 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Error in this program.

Posted 16 January 2010 - 02:48 AM

Compiled and ran fine for me, what error did you get?

oh and changed a bit of the top around to get rid of some warnings

#include <iostream>

using namespace std;

template <typename T, int N>
char (&array(T(&)[N]))[N];

int main()
{
	const char *messages[] = {
		"I'm a little teapot",
		"This is a message",
		"If you can read this, you're too close",
		"Han shot first",
		"What are you looking at?"
	};


	srand ( (unsigned)time ( 0 ) );

	for ( int i = 0; i < 10; i++ ) 
	{
		int r = rand() % sizeof array ( messages );

		cout<< messages[r] <<'\n';
	}


	return 0;
}

and one more thing...use the bbcode Code tags when posting code

This post has been edited by carltech: 16 January 2010 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Error in this program.

Posted 16 January 2010 - 03:33 AM

What compiler are you using? If its an ancient compiler (e.g. Turbo C 3.0), then it probably doesn't support templates, or much else of C++ for that matter.
Was This Post Helpful? 0
  • +
  • -

#4 kramness  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-December 09

Re: Error in this program.

Posted 16 January 2010 - 03:35 AM

#include <iostream.h>
#include <stdlib.h>
#include <time.h>



template <typename T int N>
char (&array(T(&)[N])[N];

int main()
{
	const char *messages[] = {
	"I'm a little teapot",
	"This is a message",
	"If you can read this, you're too close",
	"Han shot first",
	"What are you looking at?"
	};


	srand ( (unsigned)time ( 0 ) );

	for ( int i = 0; i < 10; i++ )
	{
	int r = rand() % sizeof array (messages);

	cout<< messages[r] <<'\n';
	}


	return 0;
} 


this is the error in my program :

Attached Image

i cant understand it.
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Error in this program.

Posted 16 January 2010 - 03:49 AM

The problem is your compiler; you may aswell give up on Turbo C++ 3.0, you will never get it to do what you're trying to do here. its almost 20 years old, and it pre-dates the C++ standard by many years, which means its missing out loads of features, such as templates and many useful C++ libraries.

Try running a modern C++ IDE and compiler instead; then you'll be able to use all the modern tools that you like. This will help you in the long run - I expect you found that example code somewhere on the web - you'll probably find the the majority of code/snippets for C++ you find online will not compile with Turbo C++ 3.0, and most tutorials and books will also expect you to have a modern compiler too.
To name a few modern IDEs which come with new compilers:
- wxDev-C++ http://wxdsgn.sourceforge.net/
- Visual C++ 2008 Express http://www.microsoft.com/Express/VC/
- Code::Blocks http://www.codeblocks.org/

This post has been edited by Bench: 16 January 2010 - 03:56 AM

Was This Post Helpful? 1
  • +
  • -

#6 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Error in this program.

Posted 16 January 2010 - 04:06 AM

should have figured it would be turbo.

another good one is dev-C++ http://www.bloodshed.net/devcpp.html

I used GCC http://gcc.gnu.org/i...l/binaries.html
the MinGW version through the command line
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Error in this program.

Posted 16 January 2010 - 04:28 AM

View Postcarltech, on 16 Jan, 2010 - 11:06 AM, said:

should have figured it would be turbo.

another good one is dev-C++ http://www.bloodshed.net/devcpp.html
That link is outdated, its not a good idea to suggest someone replace an outdated IDE with another outdated IDE. I've already posted a link to the updated version of Dev-C++ in my previous reply; its called wxDev-C++ - its the same IDE with a newer compiler, and without bugs.
http://wxdsgn.sourceforge.net/

This post has been edited by Bench: 16 January 2010 - 04:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Error in this program.

Posted 16 January 2010 - 05:24 AM

MyB. I usually use VS for big projects or gcc with a terminal or CMD for smaller projects(up to 2 or 3 files).

Sorry I didn't notice your link to dev-C++. I haven't kept up with it over the last few years

This post has been edited by carltech: 16 January 2010 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 kramness  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-December 09

Re: Error in this program.

Posted 16 January 2010 - 05:43 AM

what version of dev c++ will i download.?
Was This Post Helpful? 0
  • +
  • -

#10 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Error in this program.

Posted 16 January 2010 - 06:00 AM

the newest one is wxdev-c++ that bench posted
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1