Using characters in a conditional statement in C++

if statement in c
if statement with char - c++
if statements with strings c
strcmp
if statement with char java
statements in c
how to compare characters in if statement
char in c

I am trying to write a program which asks for tuition credits and for undergraduate or graduate classes. User enters the number of credits, then must enter U for undergraduate or G for graduate. I am having issues with the conditional statement, if the user enters U, then the price of the undergraduate credits will compute and output, similar with graduate. I am trying to enter U at the IF condition, but either one price or the other outputs.

#include <stdlib.h>                             
#include <iostream.h>                          

int main ()                                    
{                                             
 const double Under = 380.00 ; 
 const double Grad = 395.00 ;
 char U , G ; 
 double First, Second, Third, Fourth, Fifth, Sixth ;

 cout << endl << endl ;       
 cout << " To calculate your tuition enter the amount of credits, then enter type of"  ; 
 cout << endl ; 
 cout << " classes." ; 

 cout << endl << endl ; 
 cout << " Enter number of credits. " ; 
 cin >> First ; 
 cout << endl << endl ;

 cout << " Enter U for Undergraduate or G for Graduate: " ; 
 cin >> Second ; 

 cout << endl << endl ; 
 cout << " Your tuition total is: " ; 

 Third = First * Under ; 

 Fourth = First * Grad ; 

 if ( Second == U )
 cout << Third ;

 else 
 cout << Fourth ; 
 cout << endl << endl ; 

 system ("Pause");                      

 }                                               

Ok I see a few problems here.

The main one is that characters in C++ have single quotes, like this 'c'. This is most likely the cause of your error. Since you never initialized U anywhere, either do initialize it to 'U' or try

if ( Second == 'U' )
    cout << Third ;

Second, though this is not necessarily an error typing cout<<endl<<endl; is a little wasteful as it flushes the buffer for cout twice with only 1 character added in between. typing cout<<'\n'<<endl; would fix that.

Using Characters in If Else Statement, Use single quotes for single characters. Use double quotes for multiple characters. In printf %c is for a single character and %s is for strings. You can not use the comparison operator== with C-strings you need to use strcmp(). What is Continue statement in C++ ; Write A C++ Program To Find The Average Of N Numbers By Using The For Loop. Write A C++ Program Using A For Loop With Continue. Write A C++ Program To Use Backward Goto. Write A C++ Program To Use Forward Goto. Write A C++ Program To Solve The Function By Using The Nested Conditional Statement.

You never give a value to U. Right now its content is garbage which is why you get random behavior. Try either assigning 'U' to your variable U or changing the confiditional to:

if( Second == 'U' )

Common conditional examples in C, We have just implemented the above logic in the code using 'if', 'else if' and 'else'. Check Whether a Character is Vowel or Consonant. In this  The switch statement provides a convenient mechanism to execute one out of several possible fragments of code, depending on the value of an integral variable or expression. A typical example that illustrates the use of the switch statement is handling a menu selection. In a text-based interface, a program could present the user with a menu of

It is more or less all of the already stated:

  1. Remove the declaration of char U since it is never used
  2. Change the type of Second to char (remove from the double list and add char Second;)
  3. Change the if statement to if ( ( Second == 'U' ) || ( Second == 'u' ) )

[C Language] If Statement With A Character Condition, [C Language] If statement with a character condition. Posted 14 February 2007 - 07:36 PM. Hi, A slightly different problem this time. I'm trying to implement an if  I am having issues with the conditional statement, if the user enters U, then the price of the undergraduate credits will compute and output, similar with graduate. I am trying to enter U at the IF condition, but either one price or the other outputs.

I don't see U = 'U' anywhere. You is declared at beginning, but never initialized. You U is just a variable. You have to assign the character 'U' in it.

Using Char In If Else If Statements, A char is a single character such as 'y' or 'n'. You are comparing a char to an array. After you scan the input into the variable, print it, see what it  Every conditional statement return 1 or 0. These values are int. So if you do printf("%d",a>b); then either 1(true) or 0(false) will be printed. In your example you are using ternary operator a<b?a:b. If condition is true then a will be printed else b.

Second is declared as a double, but it looks like you are expecting the user to enter a character.

Compare characters to characters in if statement : char Calculation , Compare characters to characters in if statement : char Calculation « Data Type « C Tutorial. Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement's body to execute. You can use an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be ignored, but if the if statement is false, it will

Conditionals, If a, b, c and d are int variables this condition can be true (if a is less than b and c is int x = 10; int y = 15; char ch1 = 'm'; char ch2 = 'n'; float f1 = 1.2345; float f2 Now we come to where we combine conditional statements with flow of control. Learn if and else in C. Learn nested if else and conditions. This type of decision taking is done by using if statement in C. (min 6 characters) Password

If condition with character datatype - C programming, If condition with character datatype in C programming. Example of Vowels in C Programming Duration: 4:21 Posted: Feb 27, 2016 Conditional operator and an if..else statement. Use of the conditional operator instead of an if-else statement might result in more concise code in cases when you need conditionally to compute a value. The following example demonstrates two ways to classify an integer as negative or nonnegative:

C Program to Check Whether a Character is an Alphabet or not, C ifelse Statement. In C programming, a character variable holds an ASCII value (an integer number between 0 and 127) rather than that  Writing in C style, you could write something like if (strcmp(option, "N") == 0)or several other things. But it would be better to get used to using std::string in C++ code; and it's more direct. If option was a std::string, your 'if' statement would have been correct. share|improve this answer|follow |.

Comments
  • Thanks Mario, I see where i made my mistake. I was expecting Second to return a character without declaring it and i also like that the user can use a capital and lower case 'U'. Thanks for you assistance.