I have an assignment at school and every student is provided a code to run a program. I got trouble with the strcpy command. When I debug the program a warning appears.
//Copyright: Nguyen Duc Minh Khoi (email: ducminhkhoi@gmail.com) - March 2015 NEW
//REMEMBER: Do not include any other library. Otherwise you will get 0 mark
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
usingnamespace std;
char map[10][20]; //Variable stores map
vector<string> step; //Variable stores steps
int numberOfSteps = 0;
void readFiles(char *);
void printCurrentMap(char[10][20]); //convenience for checking your map
int main(int argc, char* argv[]){
//for convenience just let filename = "testcase/testcase_1_X.txt" with X from 1 to 5: the order of your testcases.
//REMEMBER: change filename = argv[1] when submit !!!!!! Otherwise you will get 0 mark
char * filename = argv[1];
readFiles(filename);
/////////////////////////////////////////////
//TODO: Complete Your code after this line
//Hint: You may find it easier to use functions to remove duplicate codes.
// However, you can code anything else. Your input is read in 2 variables
// char map[10][20] and vector<string> step (just like 1 dimensional array for convenience)
//END TODO
/////////////////////////////////////////////
return 0;
}
//Description: print the current map for easier debugging
//INPUT: the current map variable
//OUTPUT: no output, but it will print the current map on screen
void printCurrentMap(char current_map[10][20]){
cout << endl;
cout << "Current Map:" << endl;
cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
for (int i = 0; i < 10; i++){
cout << 9 - i << "| ";
for (int j = 0; j < 20; j++){
cout << current_map[i][j] << " ";
}
cout << "|" << endl;
}
cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
cout << " 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 " << endl;
}
//Description: read char map[10][20] and vector<string> step from file
//INPUT: file name in char *
//OUTPUT: no output, but it will change the global variables map[10][20] and vector step
//--------Given to Students----------//
void readFiles(char* testcase_1_1){
string line;
ifstream myfile(testcase_1_1);
int i = 0;
if (myfile.is_open()){
while (getline(myfile, line)){
if (i < 10){
strcpy(map[i++], line.c_str());
}
elseif (i >= 10){
for (int j = 0; j < line.length(); j += 6){
numberOfSteps++;
string word = line.substr(j, 5);
step.push_back(word);
}
}
}
myfile.close();
}
else {
cout << "Unable to open file";
}
}
By the power invested in my, I shall make OP's warning Appear.
Could you please post the warning? And FYI, warnings don't prevent programs from compiling, you don't have to deal with the warning right now/at all, you can just continue writing your code. If its not compiling, they you got an error, not a warning. Always copy paste your errors.
In this case, you forgot to #include <cstring>
Edit:
*Turns out its error and not a warning*
A wild Error appears
ERROR wants to fight!
ERROR USES NO-COMPILE
A critical hit!
What will Programmer do?
Programmer selects "Run"
Got Away Safely!
Error 1 error C4996: '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. c:\users\administrator\documents\visual studio 2013\projects\project5\project5\source.cpp 70 1 Project5
Warning 2 warning C4018: '<' : signed/unsigned mismatch c:\users\administrator\documents\visual studio 2013\projects\project5\project5\source.cpp 73 1 Project5
Here is my problem. An error and a warning for strcpy(map[i++], line.c_str());. thanks my friend
A wild Warning appears
Programmer used -w flag it's super effective
Warning fainted
Programmer is hurt by recoil
By the way, you can't use strcpy() because you'll need to #include <cstring> which you can't because ``Do not include any other library. Otherwise you will get 0 mark''
> 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
that's just your compiler being too much helpful. strcpy() doesn't check that you have enough space reserve to not write out of bounds, strcpy_s() is a not standard function that ask for the size of the buffer