Selection Assignment

Write the program convertChar that will read in one character (type, char) and then

if the input is uppercase, print the lowercase letterif the input is lowercase, print the uppercase letter
if the input is anything else, just print the character

When testing a character, use the character value, not the ASCII (numeric code) value. For example, use 'a' rather than 97.

Also, do not write an if-statement for every possible character. Rather, use a range check to determine whether the char variable's value is within a particular range. For example, if the char variable (input) is between 'a' and 'z', inclusive, then the input was lower case. Your code must have no more than TWO if-statements.

Be sure to prompt the user for input and label all results.

Your programs execution should produce output like the following examples (user input in bold blue):

Example 1:
Enter a character: h
The uppercase of 'h' is 'H'.

Example 2:
Enter a character: B
The lowercase of 'B' is 'b'.

Example 3:
Enter a character: ]
You enter non-letter, ']'.

NOTE: The examples above are EXAMPLES! Your code should work for any character that the user might enter.

Here is my attempt

#include <iostream>// Includes here (NOTE: No semicolons in #includes)

using namespace std;

int main ()// main program that executes code.
{
cout << "Enter a character: ";

char c;

cin >> c;

cout << "The uppercase of character is: " << c << endl;

return 0;
}
Look up std::isalpha() and friends in the header <cctype>.
http://en.cppreference.com/w/cpp/string/byte
Here is my another attempt
#include <iostream>// Includes here (NOTE: No semicolons in #includes)

using namespace std;

int main ()// main program that executes code.
{
char c;
cin >> c;
char (65);
char c2;
cin >> c2;

if (c >= 'A' && c >= 'z')
c2 = (char)(c + 32);
cout << "upper-low" << endl;
else if (c >= 'a' && c>= 'Z')
c2 = (char)(c - 32);
cout << "lower-upper" << endl;
else
cout << "non-letter" << endl;
return 0;

}
Write the program countPositive that solves previous problem. Your program should

print a title for the program
prompt for the input
print the result, that is, the number of input values that are greater than 0, i.e., positive.

Assume that all values are integers. Store your program in the file countPositive.cpp. Be sure to check your answers.

here is my attempt
#include <iostream>// Includes here (NOTE: No semicolons in #includes)

using namespace std;

int main ()// main program that executes code.
{
int n1;
int n2;
int n3;
int count = 0;

if (n1 > 0)
count = count + 1;
if (n2 > 0)
count = count + 1;
if (n3 > 0)
count = count + 1;
//display count
return 0;
}
Write the program calculate that will read in two double values and then an integer. If the integer is 1, then print the sum of the doubles; if the integer is 2, then print the product of the doubles; if the integer is 3, print the doubles in ascending (increasing) order; if the integer is any other value, print an error message.

You must use a switch statement to test the integer input. You can assume that the first two inputs are double and that the third input is an integer. Be sure to prompt the user for input and label all displayed results.

Be sure not to do any of the three possible calculations unless and until the corresponding integer value is entered. In other words, only add the values if the integer is 1, only multiply if the integer is 2 and only sort them if the integer is 3.

Store your program in the file calculate.cpp. Be sure to check your answers.

Here is my attempt
#include <iostream>// Includes here (NOTE: No semicolons in #includes)

using namespace std;

int main ()// main program that executes code.
{
int n1, n2, n3
switch (op){
case 1:
(n1 + n2)
break;
case 2:
(n1 * n2)
break;
case 3:
if (n1 < n2)
cout << n1 << << n2;
else cout << n2
break;
default:
cout << 'error'.
}
Topic archived. No new replies allowed.