The Annoying "else" function.

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;

     else if(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
Last edited on
I fixed the else problem, but help with the invalid input check will be greatly appreciated
Yeah after the cout<<answer, close your braces "}" before getting to the else structure
Also, the comma operator doesn't do what you think.

What exactly are you trying to do here?

else if(isalnum(*argv[1]),isalnum(*argv[2]));
im trying to make the program check if parameters a and b entered are alphanumeric
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;
Last edited on
Thankyou :) the usage of the garbage condition after the first
if
actually makes sense.
yet again thankyou for the help :)
Topic archived. No new replies allowed.