1

I'm creating this program in C++ that requests the user enter a movie title with upper and lowercase letters. It determines and displays the number of upper and lowercase letters in the title. It then creates 3 strings that converts all of the characters in the original to uppercase, then to lowercase, then to the opposite case. It displays these strings separately. The issue arises after it displays the amount of upper and lowercase letters. The program outputs an error message saying that it must request Runtime to terminate it in an unusual way. I need some help identifying the problem and fixing it. Thanks Here is my code:

#include <cstdlib> #include <iostream> #include <string> #include <cctype> using namespace std; int main(int argc, char *argv[]) { string title, upper, lower, swap; int upCount = 0, lowCount = 0; cout << "Please enter your favorite movie title:\n(it should have upper and lowercase letters)" << endl; getline(cin, title); for (int i = 0; i < title.size(); i++) { if (isupper(title.at(i))) upCount++; else if (isalpha(title.at(i))) lowCount++; } cout << title << " has " << upCount << " uppercase letters and " << lowCount << " lowercase letters." << endl; for (int i = 0; i < title.size(); i++) { upper.at(i) = title.at(i); lower.at(i) = title.at(i); swap.at(i) = title.at(i); } for (int i = 0; i < title.size(); i++) { if (isupper(title.at(i))) swap.at(i) = tolower(int(swap.at(i))); else swap.at(i) = toupper(int(swap.at(i))); upper.at(i) = toupper(int(upper.at(i))); lower.at(i) = tolower(int(lower.at(i))); } cout << upper << endl << lower << endl << swap << endl; system("PAUSE"); return EXIT_SUCCESS; } 
4
  • What have you done to debug your code? What line does it fail on? Commented Feb 1, 2016 at 2:07
  • @jmarkmurphy it seems to fail anywhere after the second for loop. I don't know any way to debug this like I typically would because crashes when it's running. Commented Feb 1, 2016 at 2:10
  • Do you have access to a source debugger? If not you could output simple messages to a log file or the console like "I got here", and maybe even a few important variable values for that location. It may take a few iterations, but you can then narrow down the failure location. Commented Feb 1, 2016 at 2:13
  • Sorry I don't know what source debugger is. However, I was able to identify that the program fails as soon as it gets to the second for loop by using cout statements. Commented Feb 1, 2016 at 2:24

1 Answer 1

2

You never initialize upper, lower, or swap to any value. When you do upper.at(i), lower.at(i), or swap.at(i) the program will break.

You can fix this by setting upper, lower, and swap all equal to title before that second loop.

Change:

... cout << title << " has " << upCount << " uppercase letters and " << lowCount << " lowercase letters." << endl; upper = lower = swap = title; //Add this for (int i = 0; i < title.size(); i++) ... 

Output:

Please enter your favorite movie title: (it should have upper and lowercase letters) V for Vendetta V for Vendetta has 2 uppercase letters and 10 lowercase letters. V FOR VENDETTA v for vendetta v FOR vENDETTA 
Sign up to request clarification or add additional context in comments.

6 Comments

I'm sorry if I'm being really stupid right now but I don' fully understand the errors you've pointed out. I don't think its running out of bounds because the length of title will always be one more than its last index, and < operator prevents it from going any farther than this last index. I set upper, lower and swap to title in the second for loop. I then set them to the uppercase, lowercase, or opposite versions of themselves.
@CZou48 You're right about the out of bounds. I read the statements wrong. However, as of the second loop, upper, lower, and swap are all uninitialized, which means you can't use .at() to set anything yet.
Can you tell me how I would fix this? How can I initialize these strings without knowing what the user is going to enter for title.
I added it to my answer.
Oh sorry, I just realized I could set the strings to each other without setting every individual letter
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.