programming issue with nested if statement

Okay I have and issue going on where my nested if statement keeps returning 0 no matter what letter of the alphabet I choose. The project was for us to take the input of a letter of the alphabet and create a program to display the corresponding integer, ex: A || B || C = 2. We had to create a nested if version and a switch version, either two different source codes or one, I chose the latter, anyway my switch statement works perfect but as I stated my nested if returns 0 for every character. I don't want the answer just someone to point me in the right direction to fix the problem. All right enough babbling here is the code:

#include <iostream>

using namespace std;

//prototype

int nestedIf (); //nested if program

int switchUp (); //switch program

char letterPick (); //letter input program

int main ()
{
int number; //number for letter of alphabet
char program = '1' || '2'; //program picker

cout << "Which program would you like to run?" << endl;
cout << "Type 1 for Nested if statement, " << endl;
cout << "or 2 for Switch statement" << endl;
cin >> program;

{
if (program == '1') //nested if statement program

program = nestedIf();

else if (program == '2') //switch statement program

program = switchUp();

else if ((program != '1' || '2'))

cout << "Not correct value" << endl;
else
program = -1;

}


return 0;
}

int nestedIf () //function for nested if statement
{
char letter;
int number;

letter = letterPick();

{
if (letter == 'A'||'a'||'B'||'b'||'C'||'c')
int number = 2;
else if ( letter == 'D'||'d'||'E'||'e'||'F'||'f')
int number = 3;
else if (letter == 'G'||'g'||'H'||'h'||'I'||'i')
int number = 4;
else if (letter == 'J'||'j'||'K'||'k'||'L'||'l')
int number = 5;
else if (letter == 'M'||'m'||'N'||'n'||'O'||'o')
int number = 6;
else if (letter == 'P'||'p'||'Q'||'q'||'R'||'r'||'S'||'s')
int number = 7;
else if (letter == 'T'||'t'||'U'||'u'||'V'||'v')
int number = 8;
else if (letter == 'W'||'w'||'X'||'x'||'Y'||'y'||'Z'||'z')
int number = 9;
else
int number = -1;
}

cout << "The corresponding number is: " << number;

return 0;
}

int switchUp () //program for switch statement
{
char letter;
int number;

letter = letterPick();

switch (letter)
{
case'A': case'a': case'B': case'b': case'C': case'c':
number = 2;
break;
case'D': case'd': case'E': case'e': case'F': case'f':
number = 3;
break;
case'G': case'g': case'H': case'h': case'I': case'i':
number = 4;
break;
case'J': case'j': case'K': case'k': case'L': case'l':
number = 5;
break;
case'M': case'm': case'N': case'n': case'O': case'o':
number = 6;
break;
case'P': case'p': case'Q': case'q': case'R': case'r': case'S': case's':
number = 7;
break;
case'T': case't': case'U': case'u': case'V': case'v':
number = 8;
break;
case'W': case'w': case'X': case'x': case'Y': case'y': case'Z': case'z':
number = 9;
break;
default:
number = -1;
break;
}

cout << "The corresponding number is: " << number;

return 0;
}

char letterPick() //function for letter input
{
char letter;
cout << "Please enter a letter of the alphabet " << endl;
cout << "and the program will return its corresponding numerical value:" ;
cout << endl;
cin >> letter;

return(letter);
}


char program = '1' || '2';

What exactly is this meant to do? It tries to set the value of program to TRUE. It's nonsensical, and a few lines later you write in a value to program anyway.

letter == 'A'||'a'||'B'||'b'||'C'||'c')

This always comes out as TRUE. Always always always. You have misunderstood how programming works. You cannot just say something out loud, like

"if letter equals a or b or c" and translate that into code like
if (letter == a || b || c)

Let's examine this:
if (letter == a || b || c)

It says:if any of the following three things are true:
letter == a -This might be true, might not be
b - What, just plain b? Yes. Is 'b' false? Well, it's not zero, and in C++, not zero comes out as true, so this is TRUE.
c - What, just plain c? Yes. Is 'c' false? Well, it's not zero, and in C++, not zero comes out as true, so this is TRUE.
So b and c are always TRUE, so the whole thing comes out as TRUE, always.

So, where you put:
letter == 'A'||'a'||'B'||'b'||'C'||'c')
I expect you meant
letter == 'A'||letter == 'a'||letter == 'B'||letter == 'b'||letter == 'C'||letter == 'c')
Last edited on
You're checking your character incorrectly.

try..

if(letters == 'A' || letters =='a' || letters =='B'...)

In order to avoid checking each case you could use the cctype library w/ functions toupper/tolower to convert the characters to upper or lower case then just check against the individual letters.
@moschops, the instructor wanted us to use a nested if statement and a switch statement, so what the first part is basically saying is that if you want to run the function nestedIf press 1 or if you want to run the switch statement press 2, i understand they essentially do the same thing, we had the option to either create two seperate source codes or combine everything into one which is what i basically did if only i can get my nested if statement to run. This is my first programming class so I may not have a complete understanding of programming yet, but I will get there

@georgewashere thanks i'm going to give that a try
This:
char program;
creates a char object, named program.

This:
char program = '1' || '2'; //program picker
creates a char object, named program, and then tries to set it to some kind of TRUE value. Setting it to some value here is completely useless. What are you trying to do with this line of code?
You know what I understand exactly what your saying, I just took = '1' || '2' part out and code ran exactly the same. I was thinking I had to assign values for the object, but now I realize that it's no need because the if/else statement will handle if the user types 1 or 2. Thanks, now i have to get this nested if statement right and I know what I have to do with that instead of saying letter =='A' || 'B' || etc, I need to make sure the computer knows for each variable that I'm checking it against the object stored in letter (if i said that right) any way it needs to be letter =='A' || letter =='a' || etc. Thanks for everyones help.
Topic archived. No new replies allowed.