if statement - Comparison with string literal C++ -
i'm writing function program allows student copy template text file. function checks user's input see if desired template allowed class.
i'm getting error "comparison string literal results in unspecified behavior" on lines 21 , 25. have done "cout << name" verify variable storing correctly, is, know that's not problem.
#include <iostream> #include <string> #include <fstream> using namespace std; //template check //first check see if student allowed use template int templatecheck() { //declare file name variable char name[256]; //prompt user input cout << "enter file name: "; //cin user input cin >> name; //begin check //cs221 first template can't use if(name == "/home/cs221temp.txt") cout << "you not allowed use cs221 templates./n"; //cs 321 other template can't use else if (name == "/home/cs321temp.txt") cout << "you not allowed use cs321 templates./n"; //any others okay (i commented these out since i'm working on function itself) //else //copytemplate(); return 0; }
this statement
if(name == "/home/cs221temp.txt")
compares pointers being equal (which unlikely), not contents.
want
if(strncmp(name,"/home/cs221temp.txt",256) == 0)
or
std::string name;
in function.
Comments
Post a Comment