Link to home
Start Free TrialLog in
Avatar of Jaxxx
Jaxxx

asked on

Functions Help!!

Hello All

Wondered if you could help me again, this time with my functions


[code]
// Prototypes for the two functions
bool ValidName(AnsiString InString);
void ReplaceSpace(AnsiString &Line, char NewChar);
//-------------------------------------------------------------------------

  char NewChar = '_';
  int Index;
  int Changed

  AnsiStiring &InString = ReadStringPr("Enter string: ");


      if ((InString[Index] >='A') && (InString[Index] 'Z') && InString[Index] <= 8))
          cout<<"String accepted";

      else

      for (Index = 0, Changed = 0; Index < Length(InString); Index++)
         {
           if (InString[Index] == ' ')
         {
             InString[Index] = NewChar;
             Changed = 1;
        }
    }
       if (Changed == 1)
           cout<<"String contains space characters..");
       else
           cout<<"Nothing was changed");


     getchar();
       return 0; //am sure I should be omitting this, because this has   no return value.
}
[/code]

InString is said to be valid if it begins with a capital letter, contains no space characters and has length not exceeding 8.

String &Line, Char NewChar changes the input string by replacing all occurences of space characters (if any) by NewChar...No return value.

I have been trying to write this code for days now and for the life of me cannot see why it dosen't work. Probably because I have not used ReplaceSpace but cannot see where it should go.  Also am not sure I actually need Changed to check whether a character has been changed?  Also do I need to write a function definition?

Any help would belive me be appreciated, you have saved me in the past, please help again.
Avatar of bsnh99
bsnh99

Hi Jaxxx,

I'm not sure where to start here.

You don't show the code that you've given as part of a function, so I can't verify that you should get rid of the "return 0;". But if you say that it has no return value, your compiler should be giving you an error if you try to return a value from a function declared void. (Some compilers might actually let this pass without warning, though.)

AnsiString is misspelled in your example, possibly because you retyped this code instead of cutting and pasting.

Your comparison (InString[Index] 'Z') is missing a "<=", again, probably because you retyped instead of cut&paste.

In your tests (InString[Index] >= 'A'), Index does not appear to be initialized. The results of the comparison are therefore undefined. This could be your problem.

You are comparing InString[Index] (a char) to 8 (an int). I'm not sure why you're doing this. Maybe you wanted to check the length of InString instead?

If I had to guess where you should use ReplaceSpace(), I'm guessing that you would replace your for loop with a call to ReplaceSpace(InString, NewChar);
Avatar of Jaxxx

ASKER

My apologies for the errors in typing, please be assured (InString[Index]<='Z') that it is written correctly.

Once again apologies, this is my other piece of code (working) I did not think you needed to see this:

[code]
// Prototypes for the two functions
bool ValidName(AnsiString Name);
void ReplaceSpace(AnsiString &InString, char NewChar);
// ---------------------------------------
int main(int argc, char* argv[])
{

Value = ReadStringPr("Enter string:  ");

        if  (ValidName(Value)== true)
          cout<<"String accepted";

        else
          {
                ReplaceSpace(Value,'*');

                if (ValidName(Value)==true)
              {
                cout<<"String contains space characters..";

              }
                cout<<"Illegal string";
          }

[/code]
Avatar of Jaxxx

ASKER

FYI.....AnsiString Value is initialised...
ASKER CERTIFIED SOLUTION
Avatar of DarthNemesis
DarthNemesis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jaxxx

ASKER

I have made some changes to the code, it now compiles but is giving me the previous messages, eg illegal string, string has spaces etc....instead of replacing each space character with an underscore character.

Help please (I've been on this for days and days and days)

// Prototypes for the two functions
bool ValidName(AnsiString Name);
void ReplaceSpace(AnsiString &InString, char NewChar);
// ---------------------------------------
int main(int argc, char* argv[])
{

    AnsiString Value;

    Value = ReadStringPr("Enter string:  ");

          if  (ValidName(Value)== true)
             cout<<"String accepted"<<endl;

         else
          {
                  ReplaceSpace(Value,'*');

                  if (ValidName(Value)==true)
          {
                     cout<<"String contains space characters.."<<endl;

              }
                cout<<"Illegal string";
          }


     getchar();
     return 0;
}
// Working Code!!
//--------------------------------------------------------------------------


    char NewChar= '_';
    int Index;



    bool ValidName(AnsiString InString)
       {
         if ((InString>= 1) && (InString<= 8) && (InString >= 'A') && (InString<='Z')
                  && (InString!= ' '))
              return true;
              else
              return false;
       }


    void ReplaceSpace(AnsiString &Line, char NewChar)
         {
            int Index = 1;
            for (Index =1;Index<=Length(Line);Index++)

            if (Line[Index]==' ')
               Line[Index]=NewChar;
          }
Avatar of Jaxxx

ASKER

Thanks for replying DarthNemesis

I tried to instigate some of what you said but it didn't work.  Probably toooooo tired working on this code, excuse the stupidity, I am new to functions.

In the version I wrote:
1) The string will be counted as valid if it is between 1 and 8 characters long, the first letter is capitalized, and there are no spaces.
2) If the string is not valid, all spaces will be replaced by '*'. (Change the ReplaceSpace call to this to make it replace spaces with underscores: ReplaceSpace(Value, '_'); )
3) If the string is still not valid, display "Illegal string"

In your changed version:
1) The string will be counted as valid if it is between 1 and 8 characters long, the first letter is capitalized, and there are no spaces.
2) If the string is not valid, all spaces will be replaced by '*'. Display "Illegal string" whether or not the string is now valid.

What is it that you intended the code to do, if not the above?
Avatar of Jaxxx

ASKER

Thanks DarthNemesis, I hadn't added a lib file to builder and got false readings.

Thank you very much for all your help, I can finally get some sleep :o)