?
Solved

Functions Help!!

Posted on 2003-03-17
8
Medium Priority
?
231 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:Jaxxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 1

Expert Comment

by:bsnh99
ID: 8155918
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
 

Author Comment

by:Jaxxx
ID: 8155973
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
 

Author Comment

by:Jaxxx
ID: 8156016
FYI.....AnsiString Value is initialised...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Accepted Solution

by:
DarthNemesis earned 280 total points
ID: 8161457
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
 

Author Comment

by:Jaxxx
ID: 8163366
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
 

Author Comment

by:Jaxxx
ID: 8163389
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
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8163453
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
 

Author Comment

by:Jaxxx
ID: 8169913
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question