Wondering why this works w/out header files. I just finished it and commented out some of the header files and it still works in DEVC++. Why don't I need these header files for .lenght(), isalpha(), or toupper()?
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 <new> //for dynamic array works w/out it
//#include <string> // .length()
//#inlcude <cctype> //toupper() isalpha
usingnamespace std;
int main()
{
string str1;
int size = 0;
cout <<"Enter a sentence to be forced to upper case" <<endl;
getline (cin, str1);
size = str1.length();
char *str2 = new (nothrow) char[size]; //char pointer called str2
if(str2 == NULL) //feeble error checking attempt
{
cout <<"Unable to allocate memory." <<endl;
}
//loop through dynamic array whatever size it may be
for(int i = 0; i < str1.length(); i++ )
{
str2[i] = str1[i]; //deep copy str1 into str2
if(isalpha(str2[i]))//if element is alphabetic
{
str2[i] = toupper(str2[i]); //capitalize whole array if need be
}
}//end for
cout << str2 << endl;
delete[] str2; //deallocate dynamic array memory
system("PAUSE");
return 0;
}// end main
The particular implementation is including those headers in iostream. There's no guarantee that the program will compile in a different compiler without those headers.
Either the above or, you might not be re-compling(getting an error or warning stopping it from compiling) but simply using the old executable that was left over from the old compile when you had them included.
I think you should read the devc++ documentation. That will probably answer all your questions plus more!
i think GCC manual is what he should read.
gcampton wrote:
Either the above or, you might not be re-compling(getting an error or warning stopping it from compiling) but simply using the old executable that was left over from the old compile when you had them included.
Thanks will give it a read. I never thought to check the one header file I have left for the rest. I will uncomment the header files and get to the bottom of this.
I did recompile and today it spits out garbage chars at the end of my input from the keyboard. Anything at or above 8 chars gets some trash at the end? If (hopefully) my dynamic array is string.length() in size then how can there be more in there? Do I need a deliminating char or null char? Please help.
Yes I was using exact same code as above. Your version works, is it the way I am getting my input, cin? Your getline version is much better. Does cin just not work so well? Will try this out and get back. Thanks so much it is excruciating to be so close and not be able to bridge the gap.
ROmai, what is that and what does it mean. Are these functions of some transform class which inlcude toupper()? I am a beginner in c++ Prog II please elaborate.
http://www.cplusplus.com/reference/algorithm/transform/
It's an STL algorithm. From the first argument (an iterator) to the second (iterator as well), store the result of performing the predicate (argument 4, a pointer to function) in the range beginning at the third argument (iterator). It's basically the entire thing you were trying to do, force a sentence to all uppercase, in one line.
WOW, that is mindblowingly elegant. Nice code! Can force the string toupper() like this, put it into a dynamic array and then print it ? I would say that meets my requirements.
I still don't get it, how does string str1; work without include <string> using gcc, and how did blackcoder41 know the code compiled correctly ? using multiple accounts or something ? telepathy ?
Check the link he posted earlier, it provides a nice explanation. I was surprised too.
If you want to create a cstring out of a std::string for some reason (cstrings are less reliable but whatever suits you), you can call .c_str() on the string, which returns a cstring version of its contents.
Do you mean /bin at the end of the string or in the end of the program?
Can force the string toupper() like this, put it into a dynamic array and then print it ?
Do you mean copying the result, uppercase string into a dynamic array?
Just change the 3rd argument of transform (the output iterator) to the beginning of your array.