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.
JaxxxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bsnh99Commented:
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);
0
JaxxxAuthor Commented:
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]
0
JaxxxAuthor Commented:
FYI.....AnsiString Value is initialised...
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

DarthNemesisCommented:
First off, I've never heard of this ReadStringPr() function so I had to write my own.

You didn't actually define your function bodies, so
that would be the main problem. You want to move all of the code that checks for a valid string into the ValidName function, and all of the code for ReplaceSpace into its own separate function as well, so main does not have to perform any tests that the functions are designed to do.

#include <iostream>
#include <string> // needed for my ReadStringPr()
#include <conio.h> // needed for getch()
#include <vcl.h>
#pragma hdrstop
// ---------------------------------------
AnsiString ReadStringPr(AnsiString &Prompt) {
        cout << Prompt;
        string input;
        getline(cin, input);
        return AnsiString(input.c_str());
}
// ---------------------------------------
bool ValidName(AnsiString Line) {
        if (Line.Length() >= 1 && Line.Length() <= 8 && Line[1] >= 'A' && Line[1] <='Z' && !Line.Pos(" ") )
                return true;
        return false;
}
// ---------------------------------------
void ReplaceSpace(AnsiString &Line, char NewChar) {
        for (int i=1; i <= Line.Length(); i++) {
                if (Line[i] == ' ')
                        Line[i] = NewChar;
        }
}
// ---------------------------------------
#pragma argsused                  // FYI, if you aren't accepting command
int main(int argc, char* argv[])  // line input then you don't need this;
{                                 // you can just use int main() {

        AnsiString Value = ReadStringPr("Enter string:  ");

        if  (ValidName(Value))
                cout << "String accepted" << endl;
        else
        {
                ReplaceSpace(Value, '*');
                if (ValidName(Value))
                        cout << "String contains space characters..." << endl;
                else
                        cout << "Illegal string" << endl;
        }
        getch();
        return 0;
}

Another point of interest, the Borland compiler uses the directive #argsused before an int main (int argc, char* argv[]), but you would only use that kind of main if you wanted your program to be run from the DOS prompt. Since your program doesn't need any command-line parameters, you can change it to int main(), or to void main() if you remove the return 0; at the end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JaxxxAuthor Commented:
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;
          }
0
JaxxxAuthor Commented:
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.

0
DarthNemesisCommented:
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?
0
JaxxxAuthor Commented:
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)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.