I have a program that is supposed to take user input for the command line but my program isnt working correctly. Here is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main(int argc, char* argv[])
{
//checks to see if user wants to delete their keys
if(argv[1] == "-d")
{
cout << "Are you sure you want to delete all of your saved keys? ";
char choice2;
cin >> choice2;
if(choice2 == 'y')
{
cout << "Deleting File..." << endl;
system("del serial.txt");
}
}
This is for a serial key storage program that i made. It is supposed to ask the user if they want to delete stored keys but it doesnt work when a '-d' is entered after the program name. Without anything in argv[1] though the program crashes. But the '-d' after the name doesnt to anything the program skips this block of code. How do I fix this?
that works if '-d' is after but when there is nothing entered the program crashes. How do I fix that? Do I have to check if there is a null in argv[1]?
Okay I didnt know about the and thing. After closer examination why does the strcmp comparison have to equal zero. In the example you linked to it shows that the 2 strings would return a 1. Does 'strcmp' return a 0 if the conditional is true in C++ or how does that exactly work?
A lot of C functions return ints and because C doesn't/didn't have a boolean type this can sometimes mean int or boolean depending on the function. If it means a boolean value then the normal conventions usually apply (i.e. 0=false otherwise true) however, if it really, really, means int, then the possible return values will depend on the particular function.
In the case of strcmp, 0, helpfully, means strings are equal. Basically, you need to check the docs. ;-)