Hi! I am having trouble with the flipped function of my code. I want it to display the opposite of whatever was given by the user(in terms of upper and lowercase letters), but it just displays the letters in all uppercase.
I will post my code, then my assignment instructions incase you would like further details. I appreciate all the feedback, thanks:)
Code:
#include <iostream>
#include <string>
using namespace std;
Instructions:
Write a program with three functions: upper, lower, and flip. Each function should accept a C-string as an argument. The upper function should step through all the characters in the string, converting each to uppercase. The lower functions should step through all the characters in the string, converting each to lowercase. The flip steps through the string, testing each character to determine if it is upper or lowercase. If upper, it should convert to lower, if lower it should convert to upper.
Your code takes in a string that will be a mix of upper and lower.
Then you turn that string into all upper.
Then you turn it into all lower.
So what kind of string are you passing to the function flip? Is it a mixed string? No, you just turned it into all lower. So what will flip do? Turn it into all upper.
That would work. Better, give each function a copy of the string that they can work on. That way, it doesn't matter what order you call the functions in.
While I'm here, for the future when you have the choice, don't use arrays. Don't use C-strings. Use a proper C++ std::string. I really mean this.
Arrays are not for beginners. C-strings are only when you have no choice. Lots of teachers start with them, and I really don't know why. They think they're doing you a favour, teaching C instead of C++ :/
That appears to be the case here. The instructions specifically say that the functions should accept a C-string.
Ctuser, it's a good habit to separate the doing from the displaying. In this case, upper(), lower() and flip() should just modify the string. The main program should then print the modified string. This makes the program much more flexible. What if the next assignment is to write upper case strings to a file? You wouldn't be able to use your current upper() function without changing it.
Prefer for loops to express the loop. They make loops much easier to read because all the looping logic is in one place all the "do this each time through the loop" logic is somewhere else.
Search for "DMH" on the comments for other points.
Enter a line: UPPER & lower CaSe MaTtEr In C++.
Upper: UPPER & LOWER CASE MATTER IN C++.
Lower: upper & lower case matter in c++.
Flipped: upper & LOWER cAsE mAtTeR iN c++.