error C2664:

Can someone please tell me what im doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char *GenerateNumber(int Len)
{
	using namespace std;
	char *nick;
	int i;
	nick = (char *) malloc (Len);
	nick[0] = '\0';
	srand(GetTickCount());
	for (i = 0; i < Len; i++) {
		sprintf_s(nick, "%s%d", nick, rand()%10);
	}
	nick[i] = '\0';
	return nick;
}


Error is:

1>Compiling...
1>wOthers.cpp
1>.\wOthers.cpp(104) : error C2664: 'int sprintf_s(char *,size_t,const char *,...)' : cannot convert parameter 2 from 'const char [5]' to 'size_t'
1>        There is no context in which this conversion is possible

Anyhelp ore guidens is most aprichiated.
Thanks to all that takes the time.

And sorry for my bad gramma i realy try to spell correct.
Last edited on
The 2nd argument of sprintf_s is the size of the array you passed.
You have other problems, too. Like stepping outside the bounds of your array. If you're writing to nick[Len], you need to allocate Len+1.

Also, srand() probably shouldn't be called there. It probably should only be called once at program startup.


There are also style issues... like passing ownership (ew), and using malloc in C++ (ew).

A better way to do this would be like so:

1
2
3
4
5
6
7
8
9
10
std::string GenerateNumber(int len)
{
  std::string nick;
  nick.reserve(len);  // optional, but good to do

  for(int i = 0; i < len; ++i)
    nick.push_back( (rand() % 10) + '0' );

  return nick;
}


Simpler code, less chance of memory leak, less chance of heap corruption, and probably faster.
Hi! I'm juliet and i'm new to computer programming and I can't detect the errors in mysourcecode of circle. Can you please help me? asap. thanks!

#include<iostream>
using namespace std;
int rad;
doubel pi,area,circum;
int main()
{
pi=3.1416;
cout<<"Enter radius:";
cin<<rad;
area=pi*r*r;
circum=2*r*r;
cout<<"The area of circle is:"<<area<<"\n;
cout<<"The circumference of the circle is:"<<circum<<"\n;
return 0;
}



..Please help me,, It's makes me dizzy..
Last edited on
Disch: Thanks for the heklp cant quit get it to work.
I will work on it a litel later. And come back to it.

And to: Juliet92
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>



int main(float pi,float area,float circum,float r));
{
	using namespace std;

	float rad;
	pi =3.1416;
	cout<<"Enter radius:";
	cin << rad << endl;
	r = rad

	area = pi * r * r;
	circum = 2 * r * r;
	cout << "The area of circle is:"<<area<<"\n;
	cout << "The circumference of the circle is: " << circum << "\n;
return 0;
}

I still codent get it to work aint got more time atm.
Think you shude check that you use spaces where seting veribals.
If this is not just in VS C++ Express.
Hope this helps you out a litel a lot less errors atleast.
Ok using
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char *GenerateNumber(int Len)
{
	using namespace std;
	char *nick;
	int i;
	nick = (char *) malloc (Len);
	nick[0] = '\0';
	srand(GetTickCount());
	for (i = 0; i < Len; i++) {
		sprintf(nick, "%s%d", nick, rand()%10);
	}
	nick[i] = '\0';
	return nick;
}

I get to build the prog but it dosent run.
Nothing happens. And i get these errors.

1>wOthers.cpp
1>.\wOthers.cpp(104) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(366) : see declaration of 'sprintf'
1>.\wOthers.cpp(144) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(366) : see declaration of 'sprintf'
1>.\wOthers.cpp(146) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(366) : see declaration of 'sprintf'
1>.\wOthers.cpp(178) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(366) : see declaration of 'sprintf'
1>.\wOthers.cpp(186) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(366) : see declaration of 'sprintf'
1>.\wOthers.cpp(232) : warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        C:\Program Files\Microsoft Visual Studio 9.0\VC\include\string.h(79) : see declaration of 'strcat'
1>.\wOthers.cpp(236) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(237) : see declaration of 'fopen'
1>Linking...
1>run.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>run.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>run.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>run.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>run.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>run.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>enter.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>enter.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>enter.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>enter.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>enter.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>enter.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>restart.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>restart.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>restart.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>restart.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>restart.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>restart.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>Dowload.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>Dowload.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>Dowload.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>Dowload.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>Dowload.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>Dowload.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>Intstall.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>Intstall.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>Intstall.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>Intstall.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>Intstall.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>Intstall.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>Connect.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>Connect.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>Connect.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>Connect.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>Connect.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>Connect.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>ssyn.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>ssyn.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>ssyn.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>ssyn.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>ssyn.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>ssyn.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>udp.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>udp.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>udp.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>udp.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>udp.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>udp.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>wProg.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>wProg.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>wProg.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>wProg.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>wProg.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>wProg.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>wOthers.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>wOthers.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>wOthers.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>wOthers.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>wOthers.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>wOthers.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>wThreads.obj : warning LNK4229: invalid directive '/FILEALIGN:0x200' encountered; ignored
1>wThreads.obj : warning LNK4229: invalid directive '/RELEASE' encountered; ignored
1>wThreads.obj : warning LNK4229: invalid directive '/opt:nowin98' encountered; ignored
1>wThreads.obj : warning LNK4229: invalid directive '/IGNORE:4078' encountered; ignored
1>wThreads.obj : warning LNK4229: invalid directive '/IGNORE:4089' encountered; ignored
1>wThreads.obj : warning LNK4229: invalid directive '/FIXED:NO' encountered; ignored
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\Watru\Desktop\prog\Release\BuildLog.htm"
1>Ganja - 0 error(s), 85 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

What am i doing wrong?
All help is most aprichiated.
@Juliet: You shouldn't take over a thread owned by someone else. Please try to post your own topic. But Non-the-less.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
using namespace std;

// Never put variables up here. Not even if aliens will eat your brain if you don't
// I don't care how much they torture you.

// Never put any ints, chars, floats, doubles here. EVER.
// Say no to Global Variables.

int main()
{
  //int rad; // You declare rad as an int.
  const double pi = 3.1416; // Pi... pi never changes.
  double area, circum; // Its double, not doubel :)
  double rad; // You'll notice why I did this. Read lower.

  cout << "Enter radius:"; // This is fine

  // You read rad in:
  //cin >> rad; // Insertion Operator is >>. You used the Extraction Operator <<
  cin >> rad;

  // Rad is never used... but r appears?
  // And shouldn't r be a double?
  // Change r to rad, and rad to be a double.
  area = pi * rad * rad;
  circum = 2 * rad * rad;
  cout << "The area of circle is: " << area << "\n"; // missed a '\"' here.
  cout << "The circumference of the circle is: " << circum << "\n"; // missed '\"'

  return 0;
}


Then for you WetCode. Use std::strings. Do not work in the heap without objects. Its risky and not easy. Here's the code similar to Disch. You're gonna need to change who receives the car to a std::string as well. If you need a cstring later, std::strings come with a method to help with it.
1
2
3
4
5
6
7
8
9
10
std::string GenerateNumber(int len)
{
	std::string nick;
	nick.reserve(len);

	for (int i = 0; i < len; ++i)
    nick += (rand() % 10) + '0';

  return nick;
}
Topic archived. No new replies allowed.