please help, at a complete loss and about to quit :(
Hello all, I'm new here and I have a problem.
I'm currently doing a c++ course and I am totally and completely stuck.
Not helped by the fact that due to unforseen personal circumstances I'm now weeks behind.
Its basic stuff, I've to write a program that makes use of 2 (provided) functions
Its borland builder thats used for it and I am having so much problems its unreal.
I've been working on this one question for around 8 hours and I can't get it to work. And builder really isn't any help. (it keeps running debugs, denies access if I try stop it, then crashes, also when trying to run programs often it'll run a program that I'm not even working with at the time no matter what code is present)
All I have to do is get the program to take a string ie "this is a test"
and give the output
this
is
a
test
the first function (cut) removes any extra spaces at the start of the string and the second one (trim) starts the line from a given index
I've manated to get this result:
t
h
i
s
....
and this:
t
th
thi
this
...
the section of code I had for the second output was this:
AnsiString Line;
Line = ReadStrPr("Enter a Line: ");
Cut(Line);
Int Index;
AnsiString Word;
for (Index = 1 ; Index <=Length(Line) ; Index = Index + 1)
{
if (Line[Index] != ' ')
{
Word = Word + Line[Index];
WriteStringCr(Word);
}
else
{
Trim(Line, Index);
}
I think i need to reset the Index to 1 but I don't know where to do it because every time I put it in I get either no output or it gets stuck in an infinite loop
Please help I really am at the end of my tether with this
Many thanks
x
# 1 Re: please help, at a complete loss and about to quit :(
so, what is your approach to this problem? Are there any characteristics you can use to split your string into substrings which are separated by whitespace? And then add these substrings into an array of strings, and then output the elements of that array?
nspils at 2007-11-11 21:00:54 >

# 2 Re: please help, at a complete loss and about to quit :(
thanks for the reply, I'm just going to submit it as is. The thing is its very course specific so i can't use anything that I didn't learn in the course so far, not even if I got it from reading further on in the course.
This happened with the last assignment, my program wouldn't give the desired output yet I still scored 100% for the code so it may be a builder or course team library problem.
The second question was to remove the function files from the program and re-write the functions yourself, which I did and got the exact same results.
# 3 Re: please help, at a complete loss and about to quit :(
I understand the danger which lurks for "helpers" who provide too advanced help for the person asking for the help. My question was my attempt to get you to explain what kind of approach you were taking on the problem, which would give me insight into what suggestions I could make which would be the most beneficial for you.
If you post in the future, try providing some information about your level of skill, etc. Don't be put off by the "this is not the place for getting people to do your homework" attitude: this is reserved for people who post a homework assignment and ask for someone to provide a solution, rather than help in modifying the student's solution which has hit a snag or which is in need of some kernel of understanding to break through the veil of less experience or understanding.
nspils at 2007-11-11 21:03:04 >

# 4 Re: please help, at a complete loss and about to quit :(
My level of skill is absolute basic! I'm just starting out, I've been doing the course since febuary. With this particular problem I have all the code and it was so close to doing exactly what it was supposed to which was very frustrating. I tried modifying it in every single way i could think of without success :(
the section of code I posted was my solution to the problem.
assuming _ is whitespace (because I don't know how to get a little square)
I need the program to take a string input from the keyboard eg _ _this_is_a_test
and output
this
is
a
test
(one word per line, no whitespace)
I was given to functions to do this with
'cut' which removes whitespace from the start of a string and
'trim' which trims off the start of a string so it starts from a given index
so what I want it to do is
'cut' away the first spaces
write out the first word
'trim' the string so it now begins at the space after the first word
'cut' away that space
write out the next word
and so on
Any time I tried to use a 'while' loop or a post conditioned 'for' loop it ended up caught in an infinite loop
so i chose a pre conditioned 'for' loop with an 'if'
but I can only get it to output either
t
h
i
s
...(one letter per line, no whitespace)
or
t
th
thi
this
thisi
...(writes out each letter, adds on the next letter on the next line, no whitespace)
So what I need to do it have it collect the letters until it reaches a space, and then write them out. I tried a bit of code that at least in my theory would reset the index each time a space was reached but this led to either no output at all or being caught in an infinite loop again!
I think I am pretty close with it and I had a friend look at it who has done a bit of c++ and he agreed about resetting the index but as I say, I can't seem to get it right!
But as I said, when I re-wrote the functions for myself I continued to get the same results so i don't know if this is a course flaw or what as I encountered a very similair problem with another project (code was outputting incorrect results, but when my code was returned to me from my tutor there was no flaw at all in it!)
# 5 Re: please help, at a complete loss and about to quit :(
Just a couple of points, I'd like to mention;
1> when u accept the string, make sure you get the entire string, because if you use cin or something like that,then; if a space appears in the middle of string it is treated as a terminating character. So make sure by printing the input string first.
if thats the problem use : getline(cin,str)
2> Another point is, you are appending the characters to form a word untill a space is found. Do not print them everytime, wait till you have the whole word.
Hope this helps
Best Regards
Tapajyoti
tdas at 2007-11-11 21:05:03 >

# 6 Re: please help, at a complete loss and about to quit :(
Just a couple of points, I'd like to mention;
if thats the problem use : getline(cin,str)
This isn't something thats been covered in the course, so i can't
2> Another point is, you are appending the characters to form a word untill a space is found. Do not print them everytime, wait till you have the whole word.
I tried to do that but nothing I tried worked!
I don't know where to change the code to do this, I was told that putting the write out bit after the tail function would do that but when I ran the program it didn't work.
# 7 Re: please help, at a complete loss and about to quit :(
There is not much I can help you with since it seems you are working with a provided library of string type of routines.
Your approach is:
1. take the input, make it an AnsiString object [which seems to be an indexed string of characters, like a cstring in C++]
2. while you have not reached the end of your AnsiString object (is it null (\0) terminated, like cstrings?)
a. cut() to delete any leading white space
b. while the "current" character (indexed position of the AnsiString, starting at 0) is not whitespace,
(1) append the current character to your building Word
(2) increment your index
DO NOT PRINT HERE - wait until the current character is whitespace
c. Print Word to the console, then make word empty
d. trim all part of your AnsiString up to the current index [remember this will re-index the AnsiString, shortening it as well]
and this will bring you back to step 2 ...
nspils at 2007-11-11 21:07:01 >
