Hello all,
Im new to programming, and I have been trying to compile a file that would add two separate numbers without the use of the user input, for example, add numbers based on the parameters. this is what I have so far.
1 2 3 4 5 6 7 8 9 10
if (argc==3)
{ double x =atof(argv[1])
double y =atof(argv[2]);
double answer = x+y;
cout<<answer;
elseif(isalnum(*argv[1]),isalnum(*argv[2]));
cout<<"Invalid user input"<<endl;;
return 0;
}
what I'm stuck on is
1. The program refuses to compile after reaching else. keeps saying expected primart expression before "else"
2. trying to output the line 'invalid user input' for non alphanumeric parameters specified.
eg, if either parameters 1ofasf and 2fsaof is given, the cout should read "invalid user input" regardless if one is right and one is invalid
In that case, use the AND (&&) operator. Also, put your garbage condition before the first if, otherwise if you have alphanumeric inputs for arg1 and arg2, but argc is still 3, then you won't identify the invalid user input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
if (argc==3)
{
if(isalnum(*argv[1]) && isalnum(*argv[2]))
{
cout<<"Invalid user input"<<endl;// Had two semicolons
}
else
{
double x =atof(argv[1]);//was missing a semicolon
double y =atof(argv[2]);
double answer = x+y;
cout<<answer;
}
}
else
{
cout << "Invalid user input"<<endl;
}
return 0;