Why does this work w/out header files?

Pages: 12
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()?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 
using namespace 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.
I think you should read the devc++ documentation. That will probably answer all your questions plus more!
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.
Last edited on
i think helios is right..

brokenbot wrote:
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.
it is compiling good, no errors..
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 found the answer, tried to open the header to prove it myself.
http://www.cplusplus.com/forum/beginner/889/#msg3185
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.
the same code as the above? anyway here's my solution..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main() {
    string str;
    cout << "Enter string: ";
    getline(cin, str);
    for(int x=0; x<str.length(); x++) {
      if(isalpha(str[x]))
        str[x]=toupper(str[x]);
    }
    cout << str;
    return 0;
}
Last edited on
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.
std::transform(str.begin(), str.end(), str.begin(), &toupper);
Last edited on
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.
Last edited on
This loop
1
2
3
4
5
for(int x=0; x<str.length(); x++) {
    if(isalpha(str[x]))
      str[x]=toupper(str[x]);
    }
}

is equivalent to this line
std::transform(str.begin(), str.end(), str.begin(), &toupper);

http://www.cplusplus.com/reference/algorithm/transform/

It basically calls toupper() to every character in str.
i copy and paste your code.. it works smoothly on my computer.
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.
It worked for me last night too, now I am getting things like /BIN at the end. Maybe it is my console??
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 ?
Last edited on
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.

1
2
3
4
std::string str = "lowercase string";
char *cstr = new char[32];
*std::transform(str.begin(), str.end(), cstr, &toupper) = 0;
std::cout << cstr;


EDIT: corrected the missing binary null
Last edited on
Pages: 12