Help!

I wrote a code but I have not to result.
'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
I need to change all. on ...


#include <iostream>
using namespace std;
int main()
{

char str[50];
int i, j;
char str2[50];
int d;
cin >> str[50];



j = 0;
d = strlen(str);

for (i = 0; i < d; i++)
if (i < d - 2)
{
if ((str[i] == '.') && (str[i + 1] == '.') && (str[i + 2] == '.'))
{
str2[j++] = '.';
i += 2;
}
else
{
str2[j++] = str[i];
}
}
else
{
str2[j++] = str[i];
}


str2[j] = '\0';
strcpy(str, str2);
Last edited on
but I have not to result

You also have not to a good post title
and not to any code tags.
You are missing a bunch of brackets on your code blocks! Plz use code tags.

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
33
34
35
36
37
38
#include <iostream>
using namespace std;
int main()
{

char str[50];
int i, j;
char str2[50];
int d;
cin >> str[50];



j = 0;
d = strlen(str);

for (i = 0; i < d; i++) //<-- this for loop is missing open and close braces. 
if (i < d - 2)
{
if ((str[i] == '.') && (str[i + 1] == '.') && (str[i + 2] == '.'))
{
str2[j++] = '.';
i += 2;
}
else
{
str2[j++] = str[i];
}
}
else
{
str2[j++] = str[i];
}


str2[j] = '\0';
strcpy(str, str2);
// where is the closing brace for the main function? 


you need to either get a IDE that does formatting for you and learn how to indent properly or you will end up doing this more often.
Last edited on
There is so much wrong with this code what are you even trying to do?

1
2
3
4
cin >> str[50]; // <-- this here is also a problem.
	// you are trying to assign the input to and element that does not exist!
	// you declared a char array of 50 elements above so the range of indices you have is from 0 - 49.
	// you can't assign past element 49 of the array! 
Last edited on
I have to replace some punctuation marks with others
Thanks for the help!
Which IDE should I use? I have a visual studio community 2019 for C ++
Hello chebyrek,

I know that you know how to use code tags. That is evident in your first message
http://www.cplusplus.com/forum/beginner/273919/#msg1181974
and the 3 others.
http://www.cplusplus.com/forum/beginner/273942/#msg1182076
http://www.cplusplus.com/forum/beginner/274003/#msg1182426
http://www.cplusplus.com/forum/beginner/274049/#msg1182682

You have 2 ways to fix your code:
1
2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 

Or use strcpy_s as suggested. Because "strcpy()" is an old less safe C function.

The best alternative is to use a "std::string" and avoid the problem.

Andy
really depends on what you find comfortable to use. But visual studio is fine also look up you may have missed my edit ^
Thank you for your help, everything is working as it should.
1
2
3
char str[50];
//...
cin >> str[50];


I don't think this is what is meant. This defines a char array with 50 elements (0 - 49) and then input a character into element 50 - out of bounds for the char array.

Is this meant to be:

1
2
char str[50] {};
cin >> str;


Which will input entered chars into str.
Last edited on
@seeplus, doesn't that actually try to input a single character into element 51? Out of bounds for the defined C string
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
	char str[50] {};

	std::cin >> str;

	for (int i = 0; i < sizeof(str); ++i)
		std::cout << (int)str[i] << ' ';
}



outofboundsforthedefinedstring
111 117 116 111 102 98 111 117 110 100 115 102 111 114 116 104 101 100 101 102 105 110 101 100 115 116 114 105 110 103 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


It reads chars from the keyboard until a white space and stores then in str starting at &a[0]. It treats str as being of type char*

If you use std::cin.getline(), then you do specify the maximum size of the buffer.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
	char str[50] {};

	std::cin.getline(str, 50);

	for (int i = 0; i < sizeof(str); ++i)
		std::cout << (int)str[i] << ' ';
}



doesn't that actually try to input a single character into element 51? Out of bounds for the defined C string
100 111 101 115 110 39 116 32 116 104 97 116 32 97 99 116 117 97 108 108 121 32 116 114 121 32 116 111 32 105 110 112 117 116 32 97 32 115 105 110 103 108 101
2 99 104 97 114 97 0


You can specify the size of the input buffer for >> to a char* by using std::setw()

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>

int main()
{
	char str[20] {};

	std::cin >> std::setw(sizeof(str)) >> str;

	for (int i = 0; i < sizeof(str); ++i)
		std::cout << (int)str[i] << ' ';
}



doesn'tthatactuallytrytoinputasinglecharacterintoelement51?OutofboundsforthedefinedCstring
100 111 101 115 110 39 116 116 104 97 116 97 99 116 117 97 108 108 121 0


Also note P0487 in C++20 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0487r1.html) which aims to prevent buffer overflow in the >> case where no setw() is in effect.

If you consider:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>

int main()
{
	char str[20] {};

	std::cin >> str[10];

	for (int i = 0; i < sizeof(str); ++i)
		std::cout << (int)str[i] << ' ';
}


Then this will input a single character into element 10 (starting from 0) of the str char array.


abc
0 0 0 0 0 0 0 0 0 0 97 0 0 0 0 0 0 0 0 0

Last edited on
Topic archived. No new replies allowed.