Hello,

I'm looking for some help with this short and simple program which I've had problems with for ages. The code for the last solution was apparently right, just didn't work. Anyway here is the latest one:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{	
	const double dx=0.1; 
	double x=0.0, y;
	
	for (int i=0; i<=140; i++)
	{ 
		y= double sin double x; 
		x=x+dx;
		cout << x << \"t" << y << endl;
	}
	return 0;
}

I'm getting a couple of errors:
error C2017: illegal escape sequence
error C2062: type 'double' unexpected

Thanks for the help

>y= double sin double x;
That's nonsensical. Perhaps you meant:

y = sin ( x );

>cout << x << \"t" << y << endl;
You accidentally flipped the \ and ". It should be:

cout << x << "\t" << y << endl;

You also need to be aware that the standard library trig functions work in radians, and not degrees.

>y= double sin double x;
That's nonsensical. Perhaps you meant:

y = sin ( x );

>cout << x << \"t" << y << endl;
You accidentally flipped the \ and ". It should be:

cout << x << "\t" << y << endl;

Fair enough with the last point. Stupid mistake.

As for the first, I started off with a simple sin (x) but still had problems...the program even suggested a list of alternate options, and that was one of them, hence I used it to see if I could make it happy. Thanks though

>the program even suggested a list of alternate options
sin is overloaded for multiple floating-point types, but you're not calling it in an ambiguous way. What compiler are you using?

>and that was one of them
No, you just read it wrong. It probably gave you a declaration to describe the function that was expected:

double sin ( double x );

You also need to be aware that the standard library trig functions work in radians, and not degrees.

I'm aware of that. Defining sin (x) between 0 and 14 degrees would be even more boring than 0-14 radians. Do I have to define that the variable im using is in radians.... I don't really understand and apologize if my questions are stupid.

the program even suggested a list of alternate options
sin is overloaded for multiple floating-point types, but you're not calling it in an ambiguous way. What compiler are you using?

Free Microsoft Visual C++ 2008 Express

Interesting. Try this code and paste the errors/warnings you get.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{	
  const double dx=0.1; 
  double x=0.0, y;

  for (int i=0; i<=140; i++)
  { 
    y = sin ( x );
    x=x+dx;
    cout << x << "\t" << y << endl;
  }
  return 0;
}

Interesting. Try this code and paste the errors/warnings you get.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{	
  const double dx=0.1; 
  double x=0.0, y;

  for (int i=0; i<=140; i++)
  { 
    y = sin ( x );
    x=x+dx;
    cout << x << "\t" << y << endl;
  }
  return 0;
}

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
fatal error LNK1120: 1 unresolved externals

Had the same sort of error at college when I made the program. Unresolved externals. No one could explain to me in concise terms what "unresolved externals" actually are.

>unresolved external symbol _WinMain@16
You're using the wrong project type. Your project is a Win32 project when it should be a console project. That error pops up because Win32 projects expect WinMain to be the entry point into the program instead of main.

>No one could explain to me in concise terms what "unresolved externals" actually are.
"Unresolved external" is a linker error. It says that you use a function or variable in your code, but that function or variable was never fully defined. For example, this code will give you an unresolved external error:

void foo(); // Declared, but not defined

int main()
{
  foo();
}

It compiles fine, but when the linker tries to find the definition of foo, it can't, and bombs. If you fully define foo, the error goes away:

void foo()
{
  // Defined now
}

int main()
{
  foo();
}

In your case, the problem is the definition of the entry point. A standard C++ program starts with main, but the C++ runtime is the one that calls main, not you. You define it for the C++ runtime, but somewhere in the C++ runtime you have this little guy:

initialize_runtime();
main(); // Run the program
cleanup_runtime();

That's just peachy in the standard program that I asked you to run. However, if your project is a Win32 project, the C++ runtime expects WinMain:

initialize_runtime();
WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
cleanup_runtime();

Since the C++ runtime expects WinMain, but you actually defined main, you get an unresolved external error when the linker can't find WinMain. The solution is to switch project types, or change your code to that of a Win32 application.

Thanks.

I understand your points about the two errors too. The idiot in my lab was way off in his suggestions.

The demonstrators were the ones who told us to use a win32. But they also said my code was fine for it.

Thanks again.

The sin values being returned by the program are one out of sync. i.e the starting x used must be x+dx, which I didn't want. I had a shot at fixing it, but realised the thinking behind it is wrong...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.