LOL, it compiles and runs here, but I am getting garbage chars at the end of long input?? Should I try to force my entire string to upper, then copy it into the dynamic array, and then just print the dynamic array? I am sooo close and want to get on w/ my day.
Well I solved my own problem eventually. I had a problem with the array size, which adds an extra garbage char. Here is the working code. I changed size+1 to size for the dynamic array size and one of my for loops was stopping at last instead of size. With these two logical errors fixed it now works. Thansk all for the above help but it is way over my head so I will use my caveman version>
/*
BRENT SMITH -- UNIT 3 PROGRAMMING ASSIGNMENT
Using dynamic arrays, Write a program that prompts the user to input a string
and outputs the string in uppercase letters.
(use a char array to store the string.)
*/
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string str1;
int size;
cout <<"Enter a sentence to be forced to upper case" <<endl;
getline (cin, str1);
size = str1.length();
char *str2 = new (nothrow) char[size+1]; //char pointer called str2 // remeber to add room for terminal char.
if(str2 == NULL) //feeble error checking attempt
{
cout <<"Unable to allocate memory." <<endl;
}
//deep copy str1 into *str2
for(int i = 0; i < str1.length(); i++ )
{
str2[i] = str1[i];
}
int last = (size); //one past last char is where I want my deliminator
str2[last] = 0; //add deliminating char to dynamic array
//loop through dynamic array whatever size it may be
for(int i = 0; i < size; i++ )
{
if(isalpha(str2[i]))//if element is alphabetic
{
str2[i] = toupper(str2[i]); //capitalize whole array if need be
}
}
cout << str2 << endl;
delete[] str2; //deallocate dynamic array memory
system("PAUSE");
return 0;
}// end main