Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1

    Question About Character Array Being Cut Off During Course of For Loop


    Hello, so I'm doing this exercise that requires me to change a given character array in such a way that every 3rd character would be converted to an uppercase form.

    The book also hinted at the fact that I would simply need to subtract 32 from a given character's value to obtain its uppercase form.

    So I declared the character array to be 70 characters long since that's how long the quote from Arthur C. Clarke was.

    I initialized it, and set up a for loop that would examine each character throughout the course of the loop.

    I then set the current character index to be 32 less than its value, to obtain its uppercase form.

    I then printed out the entire array before and after the loop to demonstrate the change of the array during the course of the program.

    The array became cut off during the course of the program, and didn't necessarily change every other 3rd character (or maybe I'm just counting wrong, I'm not sure, it just appears like every other 4th character is changed).

    Anyway, here's my code:
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main(){
    
    // 3. Declare a character array, and initialize it to a suitable string. 
    // Use a loop to change 
    // every other character to uppercase.
    
    // Hint: In the ASCII character set, values for uppercase characters are 32 less than their 
    // lowercase counterparts.
    
    
    	char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
    	char letter = 'A';
    
    	cout << "Here is the normal quote from Arthur C. Clarke: " << endl 
    		 << endl
    		 << character_array 
    		 << endl << endl << endl;
    
    		for(int i = 2; i < strlen(character_array); i++){
    
    			
    			character_array[i] = character_array[i] - 32;
    
    			i = i + 3;
    
    		}
    
    		cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    			 << endl
    			 << endl
    			 << character_array
    			 << endl << endl << endl;
    
    
    
    
    		return 0;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    You are indeed changing the fourth character look at this snippet:

    Code:
    		for(int i = 2; i < strlen(character_array); i++){
    ...
    			i = i + 3;
    
    		}
    You increment i once in the body of the loop, plus don't forget that the for() loop is increments i by one. Instead of i++ you could use i += 3 in the loop.

    Next what happens when the character is not a lower case letter?

    For example what happens when your character is a space?

    Remember the space has an ASCII value of 32.

    What happens when you place a value of 32 - 32 in a string?

    Since this is a C++ program I recommend you use a std::string instead of the C-string and use the toupper() function to convert your character to upper case. Not all computer system use the ASCII character set so just subtracting a number is not guaranteed to work properly, but toupper() will work no matter what character set you're using.

    Jim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by jimblumberg
    You are indeed changing the fourth character look at this snippet:

    Code:
    		for(int i = 2; i < strlen(character_array); i++){
    ...
    			i = i + 3;
    
    		}
    You increment i once in the body of the loop, plus don't forget that the for() loop is increments i by one. Instead of i++ you could use i += 3 in the loop.

    Next what happens when the character is not a lower case letter?

    For example what happens when your character is a space?

    Remember the space has an ASCII value of 32.

    What happens when you place a value of 32 - 32 in a string?

    Since this is a C++ program I recommend you use a std::string instead of the C-string and use the toupper() function to convert your character to upper case. Not all computer system use the ASCII character set so just subtracting a number is not guaranteed to work properly, but toupper() will work no matter what character set you're using.

    Jim
    Ah cool, yeah I know about toupper(), it was mentioned in one of the previous chapters, just the author's 'hint' threw me off the trail heh, thanks a bunch!

    Good idea, I'll go read the documentation on how toupper() works again.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by jimblumberg
    You are indeed changing the fourth character look at this snippet:

    Code:
    		for(int i = 2; i < strlen(character_array); i++){
    ...
    			i = i + 3;
    
    		}
    You increment i once in the body of the loop, plus don't forget that the for() loop is increments i by one. Instead of i++ you could use i += 3 in the loop.

    Next what happens when the character is not a lower case letter?

    For example what happens when your character is a space?

    Remember the space has an ASCII value of 32.

    What happens when you place a value of 32 - 32 in a string?

    Since this is a C++ program I recommend you use a std::string instead of the C-string and use the toupper() function to convert your character to upper case. Not all computer system use the ASCII character set so just subtracting a number is not guaranteed to work properly, but toupper() will work no matter what character set you're using.

    Jim

    After looking at a reference on Cplusplus.com, I found out I could use the putchar() function in combination with toupper() to make it work, but after trying to implement what they did, its just giving me the regular unchanged string.

    Here's the website I'm referring to:

    http://www.cplusplus.com/reference/cctype/toupper/


    Here's my revised code with the newly added portions in bold, please let me know where I'm going wrong in this one:
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <ctype.h>
    // stdio.h and ctype.h are used for later string manipulations regarding forcing every
    // every 3rd character to become uppercase
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(){
    
    // 3. Declare a character array, and initialize it to a suitable string. 
    // Use a loop to change 
    // every other character to uppercase.
    
    // Hint: In the ASCII character set, values for uppercase characters are 32 less than their 
    // lowercase counterparts.
    
    
    	char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
    	char letter = 'A';
    
    	cout << "Here is the normal quote from Arthur C. Clarke: " << endl 
    		 << endl
    		 << character_array 
    		 << endl << endl << endl;
    
    		for(int i = 2; i < strlen(character_array); i+=3){
    			
    			letter = character_array[i];
    
    			// This will assign the current letter we want to examine to the current
    			// element
    			
    			putchar(toupper(letter));
    
    			// This utilizes putchar which will take in the letter, and force
    			// it to become uppercase via the toupper() function
    
    			
    
    		}
    
    		cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    			 << endl
    			 << endl
    			 << character_array
    			 << endl << endl << endl;
    
    
    
    
    		return 0;
    
    
    
    
    
    
    
    
    
    
    
    
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Why are you using that C-stdio function putchar()? This function prints the character, I thought you were trying to modify the string not print a singe character. Also even if you did just want to print that single character why not just stick with the C++ insertion operator<<?

    Do you only want to print that one character or do you want to alter the string so that every third character is upper case?

    Why are you trying to do every third character your instructions say every other character?

    // 3. Declare a character array, and initialize it to a suitable string.
    // Use a loop to change
    // every other character to uppercase.
    Also remember that arrays in C/C++ start at zero.

    Jim
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by jimblumberg
    Why are you using that C-stdio function putchar()? This function prints the character, I thought you were trying to modify the string not print a singe character. Also even if you did just want to print that single character why not just stick with the C++ insertion operator<<?

    Do you only want to print that one character or do you want to alter the string so that every third character is upper case?

    Why are you trying to do every third character your instructions say every other character?



    Also remember that arrays in C/C++ start at zero.

    Jim
    Good point, I was trying to follow an example from CPlusPlus.com, my bad.

    Good point on the instructions. I can't believe I missed that minute point.

    Regardless, I tried printing the current letter during the course of the loop, and that doesn't do anything. Nor does printing the string during the course of the loop show any improvement.

    I'm a bit stuck.

    Here's my code thus far again:
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(){
    
    // 3. Declare a character array, and initialize it to a suitable string. 
    // Use a loop to change 
    // every other character to uppercase.
    
    // Hint: In the ASCII character set, values for uppercase characters are 32 less than their 
    // lowercase counterparts.
    
    // Status: This is still only giving me the original unchanged form of the string
    // for the final
    output


    char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
    char letter = 'A';

    cout << "Here is the normal quote from Arthur C. Clarke: " << endl
    << endl
    << character_array
    << endl << endl << endl;

    for(int i = 0; i < strlen(character_array); i++){

    letter = character_array[i];

    // This will assign the current letter we want to examine to the current
    // element

    letter = toupper(letter);

    cout << letter;

    // This utilizes putchar which will take in the letter, and force
    // it to become uppercase via the toupper() function



    }

    cout << endl << endl << endl;

    cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    << endl
    << endl
    << letter
    << endl << endl << endl;




    return 0;


    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Wait a second, I think I maybe am close to getting it, I have to write an if statement that will make sure that the value of i is an odd amount so it can skip every other character in the sequence.

    The code I have does go through every character in the string, but it changes every single character to uppercase, which I want to only limit to odd numbered i values in the for loop.

    I wrote an if statement that will block other characters from being changed to upper case, BUT the program crashes. I think its in the logic behind how I wrote it though.

    I'm trying to write an if statement that will compare the length of the entire string to the current value of i, and if there's no remainder (aka its an odd number), then make the current character uppercase.

    Well, here's my code thus far:

    Code:
    #include <iostream>
    
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(){
    
    // 3. Declare a character array, and initialize it to a suitable string. 
    // Use a loop to change 
    // every other character to uppercase.
    
    // Hint: In the ASCII character set, values for uppercase characters are 32 less than their 
    // lowercase counterparts.
    
    // Status: This is still only giving me the original unchanged form of the string
    // for the final output
    
    
    	char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
    	char letter = 'A';
    
    	cout << "Here is the normal quote from Arthur C. Clarke: " << endl 
    		 << endl
    		 << character_array 
    		 << endl << endl << endl;
    
    		for(int i = 0;  i < strlen(character_array); i++){
    			
    			letter = character_array[i];
    
    			// This will assign the current letter we want to examine to the current
    			// element
    
    			if(strlen(character_array) / i != 0){
    			
    			letter = toupper(letter);
    
    			}
    
    			cout << "This is the value of letter: ";
    
    			cout << letter;
    
    			cout << endl;
    
    			cout << "This is the value of i: " << i << endl;
    			
    			// This utilizes putchar which will take in the letter, and force
    			// it to become uppercase via the toupper() function
    
    
    	
    		}
    
    		cout << endl << endl << endl;
    
    		cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    			 << endl
    			 << endl
    			 << letter
    			 << endl << endl << endl;
    
    
    
    
    		return 0;
    
    
    }
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    I've noticed a couple of things. First you seem to be missing an include file for the strlen() function.

    Second in the following snippet:
    Code:
    		for(int i = 0;  i < strlen(character_array); i++){			
    ...
    			if(strlen(character_array) / i != 0){
    What happens the first time through this loop?

    Is it possible to divide by zero?


    Jim
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by jimblumberg
    I've noticed a couple of things. First you seem to be missing an include file for the strlen() function.

    Second in the following snippet:
    Code:
    		for(int i = 0;  i < strlen(character_array); i++){			
    ...
    			if(strlen(character_array) / i != 0){
    What happens the first time through this loop?

    Is it possible to divide by zero?


    Jim
    Oh wow, good point, I should have known better.

    I added another embedded loop, but now its giving me an error, obviously because of a mismatched pair of brackets { }, but I can't figure out where I should add another remaining end bracket, }

    Should I go with a for loop approach instead?

    Here's my code thus far:

    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(){
    
    // 3. Declare a character array, and initialize it to a suitable string. 
    // Use a loop to change 
    // every other character to uppercase.
    
    // Hint: In the ASCII character set, values for uppercase characters are 32 less than their 
    // lowercase counterparts.
    
    // Status: This is still only giving me the original unchanged form of the string
    // for the final output
    
    	char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
    	char letter = 'A';
    
    	cout << "Here is the normal quote from Arthur C. Clarke: " << endl 
    		 << endl
    		 << character_array 
    		 << endl << endl << endl;
    
    		for(int i = 0;  i < strlen(character_array); i++){
    			
    			letter = character_array[i];
    
    			// This will assign the current letter we want to examine to the current
    			// element
    
    			if(i > 1 && i < strlen(character_array){
    
    			if(strlen(character_array) / i != 0){
    			
    			letter = toupper(letter);
    			}
    
    			}
    
    			cout << "This is the value of letter: ";
    
    			cout << letter;
    
    			cout << endl;
    
    			cout << "This is the value of i: " << i << endl;
    			
    			// This utilizes putchar which will take in the letter, and force
    			// it to become uppercase via the toupper() function
    
    		}
    
    		cout << endl << endl << endl;
    
    		cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    			 << endl
    			 << endl
    			 << letter
    			 << endl << endl << endl;
    
    		return 0;
    
    }
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    It would most certainly help you if you were to apply consistent and proper indentation. And observe the levels of indentation.

    For example, the first cout and the first for statement are at the same level of indentation, but you added extra indentation for the for. No, don't do that! They are at the same level of indentation so indent them the same!

    Then within the for loop you have an if statement whose body contains another if statement. The inner if is at a deeper level of indentation than the outer if, but despite that you indented them the same. Wrong! The inner if needs to be indented more than the outer if!

    If everything is indented properly according to its level of indentation, then the necessary placement of the braces will immediately become apparent. As will any missing braces.

    Plus it doesn't help when you hide open braces at the ends of lines, which will become the middle of the line of you comment that line. Yes, that's a question of style, but that K&R style that you're using depends absolutely on proper indenting, which you are not doing!

    Here is what your code looks like when properly indented according to its levels of indentation (plus the correction in red for a compile-time error):
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main()
    {
    
    // 3. Declare a character array, and initialize it to a suitable string. 
    // Use a loop to change 
    // every other character to uppercase.
    
    // Hint: In the ASCII character set, values for uppercase characters are 32 less than their 
    // lowercase counterparts.
    
    // Status: This is still only giving me the original unchanged form of the string
    // for the final output
    
        char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
        char letter = 'A';
    
        cout << "Here is the normal quote from Arthur C. Clarke: " << endl 
             << endl
             << character_array 
             << endl << endl << endl;
    
        for(int i = 0;  i < strlen(character_array); i++)
        {
            
            letter = character_array[i];
    
            // This will assign the current letter we want to examine to the current
            // element
    
            if(i > 1 && i < strlen(character_array))
            {
    
                if(strlen(character_array) / i != 0)
                {
                    letter = toupper(letter);
                }
    
            }
    
            cout << "This is the value of letter: ";
    
            cout << letter;
    
            cout << endl;
    
            cout << "This is the value of i: " << i << endl;
            
            // This utilizes putchar which will take in the letter, and force
            // it to become uppercase via the toupper() function
    
        }
    
        cout << endl << endl << endl;
    
        cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
             << endl
             << endl
             << letter
             << endl << endl << endl;
    
        return 0;
    }
    Here is what I got when I compiled your program:
    C:TEST>g++ -Wall haunt5.cpp
    haunt5.cpp: In function `int main()':
    haunt5.cpp:29: warning: comparison between signed and unsigned
    haunt5.cpp:38: warning: comparison between signed and unsigned
    haunt5.cpp:38: parse error before `{'
    haunt5.cpp:45: confused by earlier errors, bailing out

    C:TEST>
    You left out a close parenthesis in that if-statement and those mismatched parentheses caused that "parse error before '{'" on line 38. One reason why you couldn't see that was because you were hiding that open brace at the end of that line which in turn hid from you the fact of that missing parenthesis. When I placed that red close parenthesis there that error went away.

    BTW, your braces ( {} ) are not mismatched. I don't know what you were talking about.

    Now when I compile that program, I get this:
    C:TEST>g++ -Wall haunt5.cpp
    haunt5.cpp: In function `int main()':
    haunt5.cpp:29: warning: comparison between signed and unsigned
    haunt5.cpp:37: warning: comparison between signed and unsigned
    haunt5.cpp:42: implicit declaration of function `int toupper(...)'

    C:TEST>
    You're still not #include'ing the header file for toupper()? How many more times do you need to be told?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Oh okay, I re-included the <cctype> library so I can use the ToUpper() function later on in the program.

    I also changed the indents, I should have known better.

    Though, the issue at the moment is that from the 3rd character onward, it changes all remaining characters to uppercase.

    I tried implementing another character array called replacement_array that would be the same length as the character_array, but to include the current element that's being examined, whether uppercase or lowercase and just to add it to the accumulating replacement_array.

    I'm receiving an error about how I cannot convert a char* to char* [70]. I'm just a bit confused because I'm not specifying any character pointers to begin with.

    I'm also receiving an error about the first for loop about a type mismatch, but it was working before I implemented the replacement_array, so the error probably lies within how the code that is supposed to accumulate the current element (uppercase or lowercase) to the replacement_array.

    Here's my code thus far, thanks a lot for your help by the way:

    Another note too though, the indents are fine, I think its the website that forced another tab into some of the lines when I put the code brackets around it, I swear my tabs look the same as yours that you had above for the indentation corrections:

    Code:
    #include <iostream>
    #include <cctype>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(){
    
    // 3. Declare a character array, and initialize it to a suitable string. 
    // Use a loop to change 
    // every other character to uppercase.
    
    // Hint: In the ASCII character set, values for uppercase characters are 32 less than their 
    // lowercase counterparts.
    
    // Status: This is still only giving me the original unchanged form of the string
    // for the final output
    
    	char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
    	char replacement_array[70];
    	char letter = 'A';
    
    	cout << "Here is the normal quote from Arthur C. Clarke: " << endl 
    		 << endl
    		 << character_array 
    		 << endl << endl << endl;
    
    	for(int i = 0;  i < strlen(character_array); i++){
    
    		// Why is it giving an unsigned mismatch?
    		// The above statement was working fine before
    			
    		letter = character_array[i];
    		replacement_array = replacement_array + letter;
    		// Here I implemented a replacement_array so I can have it accumulate 
    		// the current letters, aka the lowercase or uppercase forms of the character
    		// array elements
    
    		// The problem is that it is not allowing the conversion of 'char **' to 
    		// 'char *[70]'
    
    		// This will assign the current letter we want to examine to the current
    		// element
    
    		if(i > 1 && i < strlen(character_array))
    		{
    
    				// This will make sure to start the below line of code ONLY when
    				// i is greater than 0, and less than the entire length of the 
    				// character_array (maybe a for loop is better in this instance)
    
    			if(strlen(character_array) / i != 0)
    			{
    
    				// This approach of only dividing odd numbers is okay, but flawed
    				// in the sense that, what will happen if i is equal to 0?
    				// AKA: DIVISION BY ZERO OCCURS --> Big problem, so fix it!
    
    				// I added another if loop above ^
    			
    				letter = toupper(letter);
    				replacement_array = replacement_array + letter;
    
    
    
    
    			}
    
    		}
    
    		cout << "This is the value of letter: ";
    
    		cout << letter;
    
    		cout << endl;
    
    		cout << "This is the value of i: " << i << endl;
    			
    			// This utilizes putchar which will take in the letter, and force
    			// it to become uppercase via the toupper() function
    
    	}
    
    	cout << endl << endl << endl;
    
    	cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    		 << endl
    		 << endl
    		 << replacement_array
    		 << endl << endl << endl;
    
    	return 0;
    
    }
    Last edited by HauntJemimah; October 8th, 2013 at 11:47 AM.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Why all the if statements in the loop? I thought you wanted to change every other char to uppercase? Can't you increment more than 1 in the loop increment section?


    Jim
  24. #13
  25. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    Originally Posted by HauntJemimah
    I tried implementing another character array called replacement_array that would be the same length as the character_array, but to include the current element that's being examined, whether uppercase or lowercase and just to add it to the accumulating replacement_array.

    I'm receiving an error about how I cannot convert a char* to char* [70]. I'm just a bit confused because I'm not specifying any character pointers to begin with.
    Just telling us that you get an error and paraphrasing approximately what it said doesn't help as much as you think it does. For one thing, the error/warning message tells you what line of your source code that it's talking about. You know which line that is, but we don't. You might want to share that kind of information with us.

    So since I was being kept in the dark, I compiled your code myself and here is what I got (please notice that I'm telling you exactly what messages I got, plus I'm going to show you what lines of code 30, 36, 47, and 64 are):
    C:TEST>g++ -Wall haunt6.cpp
    haunt6.cpp: In function `int main()':
    haunt6.cpp:30: warning: comparison between signed and unsigned
    haunt6.cpp:36: incompatible types in assignment of `char *' to `char[70]'
    haunt6.cpp:47: warning: comparison between signed and unsigned
    haunt6.cpp:64: incompatible types in assignment of `char *' to `char[70]'

    C:TEST>
    Here is part of your code abridged with the indicated lines in red:
    Code:
        char character_array[70] = "Any sufficiently advanced technology is indistinguishable from magic.";
        char replacement_array[70];
        char letter = 'A';
    
     . . . 
    
        for(int i = 0;  i < strlen(character_array); i++){
    
            // Why is it giving an unsigned mismatch?
            // The above statement was working fine before
                
            letter = character_array[i];
            replacement_array = replacement_array + letter;
    
     . . . 
    
            // This will assign the current letter we want to examine to the current
            // element
    
            if(i > 1 && i < strlen(character_array))
            {
    
    
     . . . 
    
                    // I added another if loop above ^
                
                    letter = toupper(letter);
                    replacement_array = replacement_array + letter;
    The warnings on lines 30 and 47 about comparing signed and unsigned are because of i < strlen(character_array). i is a signed int and strlen returns a size_t, which in my compiler is defined as a long unsigned int. Since you're never assigning a negative value to i, there's no problem.

    The other is a big problem:
    Code:
                    replacement_array = replacement_array + letter;
    What the hell is that? Just to remind you, this is the declaration for replacement_array:
    Code:
        char replacement_array[70];
    replacement_array is an array! When you use the name of an array, it acts as a pointer. Except that you cannot change the address of an array, which is what the error message is complaining about. And which is exactly what you are trying to do there: you are trying to change the address of replacement_array to one which is letter greater. That goes beyond completely bogus.

    Apparently, what you were thinking was that you are using C++'s new basic string class in which the plus sign is overloaded to perform contatenation. C-style strings don't do that, since in C you cannot overload anything. Instead, C has the strcat function with which you can concatenate one string onto the end of another. But to do that with letter, you would first need to convert letter from a char to a C-style string. Or you could find the end of the replacement_array string, write letter there using standard array notation, and add a null-terminator ('\0') after that.

    Originally Posted by HauntJemimah
    I'm also receiving an error about the first for loop about a type mismatch, but it was working before I implemented the replacement_array, so the error probably lies within how the code that is supposed to accumulate the current element (uppercase or lowercase) to the replacement_array.
    I have no idea what error you are talking about there. Insufficient information.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Okay, I will be more specific with the errors from now on by posting which line it's from.

    I'll look up the documentation on strcat() to see how to use it.

    In reference to the array itself, I should have known that the array acts as a pointer, so the address can't be changed.

    I think the main error in my approach is that I'm trying to just make an accumulating STRING when in reality, I tried to make an array that is storing the old results from character_array with the uppercase characters.

    Again, I'll look into strcat() to maybe see how I can possibly utilize that function to my existing code.

    I like your two approaches that you mentioned:

    But to do that with letter, you would first need to convert letter from a char to a C-style string. Or you could find the end of the replacement_array string, write letter there using standard array notation, and add a null-terminator ('\0')
    I feel like I probably should go with the first approach for that makes the most sense giving my beginner level, though I understand where you're coming from for the second approach.

    The only thing I could think of as a slight problem to this, is that I'm using supposedly "native C++" according to the author, and not a console CLI/CLR program. The documentation I found on the CPlusPlus is referring to CLI/CLR based programs, due to the .h based libraries that they include at the top of their code.

    (http://www.cplusplus.com/reference/cstring/strcat/)

    They're probably is a native C++ form of strcat(), so I'll look in my book for it, good idea.

    Either way, thanks a bunch for bearing with me! It was a bit frustrating just to get the logic down, but I'm starting to see where I've been going wrong with this program.

    Thanks!
    Last edited by HauntJemimah; October 9th, 2013 at 11:27 AM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    They're probably is a native C++ form of strcat(), so I'll look in my book for it, good idea.
    No there is not a native C++ form of the C function strcat(). However strcat() is available in a C++ program through the use of the <cstring> C++ standard header, although you shouldn't need any of the <cstring> functions in this program.

    Again why all the if statements? I thought you wanted to change every other character in your string. Why not just use some other value in your for loop increment section to skip every other char. Do you know you can use values other than ++controlVariable in the increment section of your for loop. Perhaps something like controlVariable += 2 (increment by two) might work.
    Code:
    for(size_t controlVarible = 0; controlVariable < youString.length(); controlVariable +=2)
    Maybe you need to review how strings work. Perhaps the following links will help: Character Sequences Character Sequences, Arrays.

    Jim
    Last edited by jimblumberg; October 9th, 2013 at 11:52 AM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo