@helios, Corpus & Grey: You guys haven't really addressed the other problems in this code.
@nayeret43: This code has a few issues with it.
Line 13 & 17: Your declaring 2 variables with the same name. Both are character arrays too. Why not use the C++ string type?
Line 29: Your using
cin >>
to take input into a character array. You have no bounds checking or anything. I highly suggest you read:
http://www.cplusplus.com/forum/articles/6046/ to learn how to take string input correctly.
Lines X: You've called a variable "string". In C++ string is a known type, and your going to cause problems with this later on. You should select a better name for your character arrays like "buffer" or "charArray" as they are not technically strings.
Line 36: This may not be an issue, but I cannot recall if
cin >>
will null terminate your char array. If not, then the strlen() function is going to return an incorrect value because you haven't init your array.
Line 40: You should be able to use the isAlpha() function to do this instead.
Line 41: You don't need to set a flag here, you should just
cout
the error and return. This will save execution time and code.
Line 50: This line isn't needed, as if the variable is not false it's implicitly true and will execute. This line also causes an error because you've wrapped the return and made the compiler think that it may not find a valid return statement.
Line 54: There is actually a function called toUpper() and toLower() that will make a character uppercase or lowercase.
e.g
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <string>
using namespace std;
int main() {
string input = "";
cout << "Enter a string: ";
getline(cin, input);
cout << "You have entered: " << input << endl;
for (unsigned i = 0; i < input.length(); ++i)
input[i] = toupper(input[i]);
cout << "Uppercased: " << input << endl;
for (unsigned i = 0; i < input.length(); ++i)
input[i] = tolower(input[i]);
cout << "Lowercased: " << input << endl;
return 0;
|
You need to become more familiar with the data types and functions that are provided within the language. This will save you writing excess code to do what can already be done.