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

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Alright, after sitting down and reading about how to convert chars to strings on a similar example on Cplusplus.com, I tried out the following.

    I replaced that replacement_array with a string that will act as a string accumulator named total_string.

    I used the <sstream> library so that I can declare a stringstream variable named ss, to convert the current char letter into a string, and then add that string form to string_total.

    I'm receiving 8 errors (as usual unfortunately), but this time I will try to specify what line in my code I'm receiving them on.

    Here is the first error on line 39, which refers to my incorrect declaration of the stringstream variable:

    Code:
    c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(39) : error C2065: 'stringstream' : undeclared identifier
    Here is the second error on line 39 that refers to a missing semi-colon, but its probably referring to my incorrect way of initializing the stringstream variable:

    Code:
    c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(39) : error C2146: syntax error : missing ';' before identifier 'ss'
    Here is third error on line 39 about how ss is an incorrect name for the stringstream variable:

    Code:
    c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(39) : error C2065: 'ss' : undeclared identifier
    Here is the fourth error on line 51 of how there is a signed versus unsigned mismatch in data (it might be what you were talking about in terms of signed int versus an unsigned int, and I think its talking about the variable i:

    Code:
    c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(51) : warning C4018: '<' : signed/unsigned mismatch
    Here is the fifth error on line 58 about the stringstream variable again:

    Code:
    c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(58) : error C2065: 'ss' : undeclared identifier
    Here is the sixth error on line 60 about the the incorrect initialization of the stringstream variable again :

    Code:
     c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(60) : error C2065: 'ss' : undeclared identifier
    Here is the seventh error on line 79 about ss again:

    Code:
     c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(79) : error C2065: 'ss' : undeclared identifier
    Here is the eighth error on line 84 about ss again:

    Code:
     c:\documents and settings\owner\my documents\visual studio 2008\projects\chapt4_exercise3\chapt4_exercise3\chapt4_exercise4.cpp(84) : error C2065: 'ss' : undeclared identifier
    Here's my code thus far with the revisions:

    #include <iostream>
    #include <cctype>
    #include <sstream>
    #include <string>

    // This sstream library is used so I can ss a character to a string

    // The string library is used so that I can later declare a string

    Code:
    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.";
    	
    	string letter_string;
    	string total_string;
    
    	// These are two strings, one of which is named letter_string which will be
    	// basically the string form of the character variable, letter
    
    	// The other string named total_string will act as an accumulating string
    	// that will concatenate the string form of the character variable, letter_string,
    	// to itself with every succession of the loop
    
    	stringstream ss;
    
    	// This is a stringstream variable that will be used to ss the character
    	// variable named letter into a string form, which I will store inside letter_string
    
    	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];
    
    		ss << letter;
    
    		ss >> letter_string;
    
    		total_string = total_string + letter_string;
    
    		if(i > 1 && strlen(character_array) / i != 0)
    		{
    		// I simplified the if loop to just go past the value of 1
    		// to prevent a division by zero error, and to test if its an even number
    
    		// 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!
    
    			
    			letter = toupper(letter);
    
    			// This will ss the current odd element of the character_array into
    			// its uppercase form
    
    			ss << letter;
    
    			// This will use the stringstream variable ss, and take in the 
    			// character variable named letter as its argument
    
    			ss >> letter_string;
    
    			// This will assign the ssed string form of the character variable
    			// to the string variable named letter_string
    
    			total_string = total_string + letter_string;
    
    			// This statement acts as a string accumulator to constantly add the
    			// current string form of the character variable
    			
    		}
    	
    	// The following code is just to check what the program is actually doing and
    	// will most likely be commented out when the program runs successfully:
    
    	cout << "This is the value of letter: ";
    
    	cout << letter;
    
    	cout << endl;
    
    	cout << "This is the value of i: " << i << endl;
    
    	}
    
    	cout << endl << endl << endl;
    
    	// Output the final results, aka the string accumulator, total_string:
    
    	cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    		 << endl
    		 << endl
    		 << total_string
    		 << endl << endl << endl;
    
    	return 0;
    
    }
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    You need to properly scope the stringstream class.

    But you're still over thinking the problem, IMO.

    Do you really need a second array?

    Why?

    Your objectives state:
    Use a loop to change every other character to uppercase.
    So why don't you just loop through the string and change every other character of that string to upper case, no need for a second string, stringstreams, or anything else. Just change every other character?

    Do you know how to iterate through a C-string and print each character of the string?


    Jim
  4. #18
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I'm not sure why you would need stringstream. If I were to use iostreams, I would normally use that as a replacement for sprintf so that I could have it convert non-string values (eg, numbers) into character strings that could be inserted into a larger string that I'm creating. But you don't need to do that if you're appending a character to a string.

    A very useful concept and technique in C++ is overloading. That's where you create more than one function by the same name but with different parameter lists. That way, you could have "one function" that could take an integer argument or a string argument or an object argument (eg, a function that needs a point, so you write a version that takes a Point object or that takes the x and y coordinates so that it can create its own Point object). In reality, you're still writing different functions for the different parameter lists; the way that the compiler knows which version of the function to call is by the data types in the parameter lists. To implement that, the compiler has to "mangle" the function names by appending the data types in the parameter list -- in C++, the term "name-mangling" comes up often and this is what and why that is.

    You can also overload operators, which is very powerful! When you use the << operator with cout, you're using an overloaded operator. The datatype of the operand to the right of the << tells the compiler which version of << to use; eg, if the operand is an int, then the int version of << is used. You can also overload operators for your own classes; eg, for a matrix class, you can define what = , == , + , - , and * mean. Very powerful!

    The basic string class, basic_string, which string is based on, overloads several operators; eg (from MinGW gcc's bastring.h header file):
    Code:
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const basic_string <charT, traits, Allocator>& lhs,
    	   const basic_string <charT, traits, Allocator>& rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (charT lhs, const basic_string <charT, traits, Allocator>& rhs)
    {
      basic_string <charT, traits, Allocator> str (1, lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const basic_string <charT, traits, Allocator>& lhs, charT rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (1, rhs);
      return str;
    }
    I don't expect you to understand everything there, though one purpose is to expose you to the syntax for overloading an operator. basic_string, and hence also string, uses the plus sign for concatenation. Look at the datatypes of the rhs ("right-hand side") parameter: basic_string (ie, string), char* (ie, C-style string, a null-terminated char array), a single char. That means that you can use + to concatenate a string, char array, or single char to a string; ie, with one simple statement. BTW, if you look at the lhs ("left-hand side") parameter, you will see that if rhs is a string, then lhs could be a string, a char array, or a single char.

    Therefore, if total_string is a basic string object and letter is a single char, you can concatenate letter onto total_string with this statement:
    Code:
    		total_string = total_string + letter;
    Or even more simply with:
    Code:
    		total_string += letter;
    With absolutely no need for a stringstream object.

    To be honest, I still cannot understand this line:
    Code:
    		if(i > 1 && strlen(character_array) / i != 0)
    If you want to only convert the odd characters (ie, for i being odd), then why not just use the modulo operator? Eg:
    Code:
    		if(i % 2)
    If the remainder of i divided by 2 is 1, then i is odd. If it's zero, then i is even; eg:
    Code:
    		if( !(i % 2) )
    And what jimblumberg just asked is also what I've been asking myself: why not just iterate through the char array and convert every other character? And if you are not supposed to modify the original string, then just iterate through the array copying each character to a destination string, though modifying every other character in the destination.
    Last edited by dwise1_aol; October 9th, 2013 at 03:08 PM.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by dwise1_aol
    I'm not sure why you would need stringstream. If I were to use iostreams, I would normally use that as a replacement for sprintf so that I could have it convert non-string values (eg, numbers) into character strings that could be inserted into a larger string that I'm creating. But you don't need to do that if you're appending a character to a string.

    A very useful concept and technique in C++ is overloading. That's where you create more than one function by the same name but with different parameter lists. That way, you could have "one function" that could take an integer argument or a string argument or an object argument (eg, a function that needs a point, so you write a version that takes a Point object or that takes the x and y coordinates so that it can create its own Point object). In reality, you're still writing different functions for the different parameter lists; the way that the compiler knows which version of the function to call is by the data types in the parameter lists. To implement that, the compiler has to "mangle" the function names by appending the data types in the parameter list -- in C++, the term "name-mangling" comes up often and this is what and why that is.

    You can also overload operators, which is very powerful! When you use the << operator with cout, you're using an overloaded operator. The datatype of the operand to the right of the << tells the compiler which version of << to use; eg, if the operand is an int, then the int version of << is used. You can also overload operators for your own classes; eg, for a matrix class, you can define what = , == , + , - , and * mean. Very powerful!

    The basic string class, basic_string, which string is based on, overloads several operators; eg (from MinGW gcc's bastring.h header file):
    Code:
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const basic_string <charT, traits, Allocator>& lhs,
    	   const basic_string <charT, traits, Allocator>& rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (charT lhs, const basic_string <charT, traits, Allocator>& rhs)
    {
      basic_string <charT, traits, Allocator> str (1, lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (rhs);
      return str;
    }
    
    template <class charT, class traits, class Allocator>
    inline basic_string <charT, traits, Allocator>
    operator+ (const basic_string <charT, traits, Allocator>& lhs, charT rhs)
    {
      basic_string <charT, traits, Allocator> str (lhs);
      str.append (1, rhs);
      return str;
    }
    I don't expect you to understand everything there, though one purpose is to expose you to the syntax for overloading an operator. basic_string, and hence also string, uses the plus sign for concatenation. Look at the datatypes of the rhs ("right-hand side") parameter: basic_string (ie, string), char* (ie, C-style string, a null-terminated char array), a single char. That means that you can use + to concatenate a string, char array, or single char to a string; ie, with one simple statement. BTW, if you look at the lhs ("left-hand side") parameter, you will see that if rhs is a string, then lhs could be a string, a char array, or a single char.

    Therefore, if total_string is a basic string object and letter is a single char, you can concatenate letter onto total_string with this statement:
    Code:
    		total_string = total_string + letter;
    Or even more simply with:
    Code:
    		total_string += letter;
    With absolutely no need for a stringstream object.

    To be honest, I still cannot understand this line:
    Code:
    		if(i > 1 && strlen(character_array) / i != 0)
    If you want to only convert the odd characters (ie, for i being odd), then why not just use the modulo operator? Eg:
    Code:
    		if(i % 2)
    If the remainder of i divided by 2 is 1, then i is odd. If it's zero, then i is even; eg:
    Code:
    		if( !(i % 2) )
    And what jimblumberg just asked is also what I've been asking myself: why not just iterate through the char array and convert every other character? And if you are not supposed to modify the original string, then just iterate through the array copying each character to a destination string, though modifying every other character in the destination.
    Ah cool, I forgot about the mod operator in the context of finding odd numbers, I should have done that!

    The only thing is that, haven't I been going through the character array like you just said, and been converting every other character?

    I made revisions to remove all the stringstream variables and the like, and its actually looking to be promising, and actually works INSIDE THE LOOP, but when the string adds the current character, it doesn't care if its uppercase or lowercase and only adds the lowercase version of it.

    So the string outputs the quote like I wanted at the end, but it only added the lowercase forms of each of the characters even though when the current character WAS UPPERCASE, it chose to ignore it...

    I'm a bit confused on why this string is being so stubborn heh.

    I added a bunch of output statements so I can monitor what is actually going on in the course of the loop, and I think the only problem left is that the line where I actually add the letter to the loop is just ignoring the uppercase form of it aka: Line 73

    Anyway, here's my code, as always, thanks a bunch!

    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    
    // This sstream library is used so I can ss a character to a string
    
    // The string library is used so that I can later declare a string
    
    using namespace std;
    
    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.";
    	
    	string letter_string;
    	string total_string;
    
    	// These are two strings, one of which is named letter_string which will be
    	// basically the string form of the character variable, letter
    
    	// The other string named total_string will act as an accumulating string
    	// that will concatenate the string form of the character variable, letter_string,
    	// to itself with every succession of the loop
    
    
    
    	// This is a stringstream variable that will be used to ss the character
    	// variable named letter into a string form, which I will store inside letter_string
    
    	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];
    
    		total_string += letter;
    
    		cout << "Current value of total_string: " << total_string << endl << endl;
    
    		if(i % 2 == 0)
    		{
    		// I simplified the if loop to just go past the value of 1
    		// to prevent a division by zero error, and to test if its an even number
    
    		// 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!
    			
    			letter = toupper(letter);
    
    			// This will ss the current odd element of the character_array into
    			// its uppercase form
    
    			total_string += letter_string;
    
    			cout << "Current value of total_string: " << total_string << endl << endl;
    
    			// This statement acts as a string accumulator to constantly add the
    			// current string form of the character variable
    			
    		}
    	
    	// The following code is just to check what the program is actually doing and
    	// will most likely be commented out when the program runs successfully:
    
    	cout << "This is the value of letter: ";
    
    	cout << letter;
    
    	cout << endl;
    
    	cout << "This is the value of i: " << i << endl;
    
    	}
    
    	cout << endl << endl << endl;
    
    	// Output the final results, aka the string accumulator, total_string:
    
    	cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    		 << endl
    		 << endl
    		 << total_string
    		 << endl << endl << endl;
    
    	return 0;
    
    }
  8. #20
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Read your code as if you were the computer. For each line, ask yourself what value each variable has. That way, you learn to read for comprehension.

    I started college continuing what I had been best at in high school, foreign language (specifically, German, though in college I branched out to studying about a dozen languages to one degree or another). When I entered the service, I switched over to computer science. From the start, I viewed computer languages as being very similar to human languages and that influenced my approach. For one thing, I viewed a program as communicating to the computer what I wanted it to do, so I would use that approache whenever I would write or read a program, meaning that I would read a program for comprehension. One effect that had in the first classes was that I would zero right in on the problem while my classmates would go into a panic and try near-random changes hoping that something might work.

    As you read your code, pay especial attention to this part (my comments added in red):
    Code:
        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];
    
            total_string += letter;  
                // You just appended the letter to the string
                // before you made any change to it.  Why?
    
            cout << "Current value of total_string: " << total_string << endl << endl;
    
            if(i % 2 == 0)
            {
            // I simplified the if loop to just go past the value of 1
            // to prevent a division by zero error, and to test if its an even number
    
            // 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!
                
                letter = toupper(letter);
    
                // This will ss the current odd element of the character_array into
                // its uppercase form
    
                total_string += letter_string;   
                    // You changed letter and then did nothing with it.
                    // Instead, you are appending the string, letter_string.
                    //  What is the value of letter_string?  I don't see where
                    //       you have assigned it any value.
                    //  For that matter, I don't see where letter_string serves any purpose at all.
    
    
                cout << "Current value of total_string: " << total_string << endl << endl;
    
                // This statement acts as a string accumulator to constantly add the
                // current string form of the character variable
                
            }
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Code:
       for(int i = 0;  i < strlen(character_array); i++)
        {
    
            // Why is it giving an unsigned mismatch?
            // The above statement was working fine before
    Because strlen() returns a size_t. A size_t is an unsigned type, an int is a signed type.

    To fix the warning use a size_t for i.
    Code:
       for(size_t i = 0;  i < strlen(character_array); i++)
        {
    
            // Why is it giving an unsigned mismatch?
            // The above statement was working fine before

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

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by dwise1_aol
    Read your code as if you were the computer. For each line, ask yourself what value each variable has. That way, you learn to read for comprehension.

    I started college continuing what I had been best at in high school, foreign language (specifically, German, though in college I branched out to studying about a dozen languages to one degree or another). When I entered the service, I switched over to computer science. From the start, I viewed computer languages as being very similar to human languages and that influenced my approach. For one thing, I viewed a program as communicating to the computer what I wanted it to do, so I would use that approache whenever I would write or read a program, meaning that I would read a program for comprehension. One effect that had in the first classes was that I would zero right in on the problem while my classmates would go into a panic and try near-random changes hoping that something might work.

    As you read your code, pay especial attention to this part (my comments added in red):
    Code:
        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];
    
            total_string += letter;  
                // You just appended the letter to the string
                // before you made any change to it.  Why?
    
            cout << "Current value of total_string: " << total_string << endl << endl;
    
            if(i % 2 == 0)
            {
            // I simplified the if loop to just go past the value of 1
            // to prevent a division by zero error, and to test if its an even number
    
            // 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!
                
                letter = toupper(letter);
    
                // This will ss the current odd element of the character_array into
                // its uppercase form
    
                total_string += letter_string;   
                    // You changed letter and then did nothing with it.
                    // Instead, you are appending the string, letter_string.
                    //  What is the value of letter_string?  I don't see where
                    //       you have assigned it any value.
                    //  For that matter, I don't see where letter_string serves any purpose at all.
    
    
                cout << "Current value of total_string: " << total_string << endl << endl;
    
                // This statement acts as a string accumulator to constantly add the
                // current string form of the character variable
                
            }
    My jaw dropped when I saw that I only added letter.

    I was like, WHY DID I MISS THAT haha oh my God.

    Anyway, so I changed it to include an else clause that will include the option of even numbered characters in the array.


    This version of the code does exactly what I want it to, and changes every other character like I wanted it too, EVEN THOUGH it changes the very first character, I maybe could change i to just start at index number 2 no?

    Anyway, here's my code, and I think this is the final version :) :

    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    
    // This sstream library is used so I can ss a character to a string
    
    // The string library is used so that I can later declare a string
    
    using namespace std;
    
    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.";
    	
    	string letter_string;
    	string total_string;
    
    	// These are two strings, one of which is named letter_string which will be
    	// basically the string form of the character variable, letter
    
    	// The other string named total_string will act as an accumulating string
    	// that will concatenate the string form of the character variable, letter_string,
    	// to itself with every succession of the loop
    
    
    
    	// This is a stringstream variable that will be used to ss the character
    	// variable named letter into a string form, which I will store inside letter_string
    
    	char letter = 'A';
    
    	cout << "Here is the normal quote from Arthur C. Clarke: " << endl 
    		 << endl
    		 << character_array 
    		 << endl << endl << endl;
    
    	for(size_t i = 0;  i < strlen(character_array); i++)
    	{
    
    		// strlen(): returns a size_t, and a size_t is an UNSIGNED type. 
    		// Integers are signed types. USE SIZE_T for i!
    			
    		letter = character_array[i];
    
    		// total_string += letter;
    
    		cout << "Current value of total_string: " << total_string << endl << endl;
    
    		if(i % 2 == 0)
    		{
    		// I simplified the if loop to just go past the value of 1
    		// to prevent a division by zero error, and to test if its an even number
    
    		// 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!
    			
    			letter = toupper(letter);
    
    			// This will ss the current odd element of the character_array into
    			// its uppercase form
    
    			total_string += letter;
    
    			cout << "Current value of total_string: " << total_string << endl << endl;
    
    			// This statement acts as a string accumulator to constantly add the
    			// current string form of the character variable
    			
    		}
    
    		else
    		{
    			total_string += letter;
    
    			cout << "Current value of total_string: " << total_string << endl << endl;
    
    		}
    	
    	// The following code is just to check what the program is actually doing and
    	// will most likely be commented out when the program runs successfully:
    
    	cout << "This is the value of letter: ";
    
    	cout << letter;
    
    	cout << endl;
    
    	cout << "This is the value of i: " << i << endl;
    
    	}
    
    	cout << endl << endl << endl;
    
    	// Output the final results, aka the string accumulator, total_string:
    
    	cout << "Here is the new version of the character_array with every 3rd letter changed to its uppercase form: "
    		 << endl
    		 << endl
    		 << total_string
    		 << endl << endl << endl;
    
    	return 0;
    
    }
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by jimblumberg
    Code:
       for(int i = 0;  i < strlen(character_array); i++)
        {
    
            // Why is it giving an unsigned mismatch?
            // The above statement was working fine before
    Because strlen() returns a size_t. A size_t is an unsigned type, an int is a signed type.

    To fix the warning use a size_t for i.
    Code:
       for(size_t i = 0;  i < strlen(character_array); i++)
        {
    
            // Why is it giving an unsigned mismatch?
            // The above statement was working fine before

    Jim
    Thank you! I fixed that i variable to be size_t and not int to prevent a mismatch of signed versus unsigned.

    I forgot the difference between the two, so that was a good reminder too ;D
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo