String woes

This is a discussion on String woes within the C++ Programming forums, part of the General Programming Boards category; Hi, I am working on a program that deals with strings. Basically, I want to check the integer value of ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    52

    String woes

    Hi, I am working on a program that deals with strings. Basically, I want to check the integer value of each string and if the char's value is null, I want to make it one. Checking the value of the string is not hard, it is the part that deals with changing it's value. Here is my code:
    Code:
    for (int i=0; i < 6; i++)
    	{
    		if ((int)string[i] == 0)
    		{
    			(int)string[i] = 1;
    		}
    	}
    I have a feeling it is something I don't know/understand with strings but any help will be appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I'm not sure I follow what you are doing. Are you trying to do something like:

    Code:
    string str = "0001101";
    
    if( str[0] == '0' ) // Something..
    {
    }
    I couldn't really tell much from the code snippet you supplied. Also if it is a C++ style string why do you name it string?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    yea
    if str[0] == 0
    str[0] = 1
    something like that, sorry for all the pseudocode.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Ripper1
    yea
    if str[0] == 0
    str[0] = 1
    something like that, sorry for all the pseudocode.
    So... did you look at my code? It pretty much tells you how to do it. You should just be checking characters like '0' not 0. so..

    Code:
    if( str[i] == '0' )
    {
      str[i] = '1';
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    I've had code similar to that all night and all that happens when I run it is that Windows crashes. I just copied yours to make sure, Windows crashed as well. I have no idea why, that's why I posted here, I thought it was an insane string error but it compiles. I've had variations of that code for hours trying to figure this out. Blah, I probably need to restart and all my troubles will be solved.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Post the entire code and I'll take a look.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    Ok, I feel bad because I am trying to get better at contest programs and this was crashing windows but here goes, certain members might recognize it.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string comet;
    	string group;
    	int cometproduct = 1;
    	int groupproduct = 1;
    
    	cin >> comet;
    	cin >> group;
    	for (int i=0; i < 6; i++)
    	{
    		if ((int)comet[i] == 0)
    		{
    			comet[i] = 'A';
    		}
    	}
    	cout << comet;
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Don't feel bad about trying to learn. Your probably is when you are looping through your string. You have it hard coded to do 6 indices. What if the string is shorter? This could be a problem. Try doing something like:

    Code:
    int CometLength = comet.length(); // or comet.size()
    for( int i = 0; i < CometLength; i++ )
    {
      // ...
    }
    Hope that helps a little. Also remember, if you are looking for the character 0 then place single quotes around it in the if statement. Good luck and post back if you need more help.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    thank you, I need to read up more before I attempt harder problems on this string of contests, I generally know a lot about core language C++ coding but I lack a lot of little things such as that. thanks for the help!

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Basically, I want to check the integer value of each string and if the char's value is null...
    Null is different than 0. You could use this code to loop and test whether you have reached the end of an array:
    Code:
    for (int i=0;array[i]!='\0';i++)
       ...
    Since arrays should be null-terminated, as long as you havent tried to put any values in the very last element of the array this should work
    Last edited by JaWiB; 08-22-2003 at 04:14 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    All the char (letters, digits, punctuation marks) and many we don't routinely use are identified numerically in a character set. Thus, using the ASCII character set the char a has a numerical value of 97 and the char A has a value of 65, etc. The ASCII char set uses numerical values 0-127, the extended ASCII char set uses numerical values 0-255, and the unicode char set is identical to the extended ASCII character set for numerical values 0-255 but goes beyond there as well. The point is, the numerical value for the null character is zero. therefore, this:

    char array[4];
    array[0] = 0;

    assigns the numerical value of zero, that is the null character, to the first element of array, and

    array[1] = 65;

    assigns the numerical value of 65, that is 'A', to the second element of array. At this point, array is both a valid char array and a valid string. However, as a string, it is an empty string, as the index for 'A' is after the index for the null char. In fact, strings can only have a single null char, and it is the last char of the string, even if there are other char after the null char in the char array. You can also have more than one null char in a char array, of course:

    array[0] = 'A';
    array[1] = 0;
    array[2] = 'a';
    array[3] = '\0';//just another way to express the null char.

    now if you did this:

    cout << array;

    the output would be: A

    However, if you did this:
    Code:
    for(int i = 0; i < 4; ++i)
    {
        if(array[i] == 0)
           cout << '*';
        else
           cout << array[i];
    }
    the output would be:
    A*a*

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    Thanks for the extra help, but I finished it last night, I still appreciate it though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 04:58 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21