Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


made a program filter_list
Author
Message
RE: made a program #23
(09-02-2011, 05:00 PM)Frooxius Wrote: What do I think? Well the code is... weird and wrong and won't work properly for many reasons, I recommend trying some simpler things for start, since you don't seem to understand basic constructs of the C/C++ language, so you can understand them better and then have less trouble.

Code:
while(int i = texts.length()) //
    {
        texts[i+1]=' ';
        texts[i+2]=' ';
        texts[i+3]=' ';
        break;
    }
The "condition" doesn't make any sense, you're basically assigning to the variable i length of the texts string, so the assigned value becomes value of the whole expression, that gets evaluated. So assuming the length wouldn't change, such loop would run indefinitely, unless it would be ended from the inside.

The code inside (the assignments) are also incorrect: i contains the length of the string and because array-like indexing is usually from zero, then the index of the last character would be i-1, yet you are assigning characters beyond maximum range. This can sometimes work, because STL string allocates memory dynamically - however not when using array-like indexing, but the actual allocated memory can be more than is the actual length (this can be determined by the texts.capacity() method), but it also could be not. In any case, the code will most probably produce run-time error and thus it's wrong.

Also... the last break; statement killed it... What's the point of starting a loop and then unconditionally breaking out of it? Even if the condition made sense, the "loop" would execute only once, because you force it to end right away, so it doesn't make any sense. If you want to test some condition and execute code based on it, use
Code:
if(condition)
   code
else
   optional-else-clause code
"code" can be either single statement or a code block encompassed in curly brackets - { code }

Code:
for (int i=0;i<=texts.length();i++) // Removing Spaces
    {
        if (texts[i]==' ')
        {
            for (int g = i;g<=texts.length();g++)
            texts[g]=texts[g+1];
        }
    }
This is weird way to remove spaces and not very efficient (and again, highly dangerous, because you go beyond the maximum index). You can do it in a single run by creating another string and copying only non-whitespace characters, then discarding the old one.
Code:
// it will be good to either put this in a function or a code block so the temp will be discarded after the operation
string temp;
temp.reserve(texts.length()); // reserve enough capacity to improve performance (eliminate need for reallocation when expanding capacity internally)
for(int i = 0; i < texts.length(); ++i)
   if(!texts[i] != ' ')
      temp.append(texts[i]);
texts = temp;
There are more ways to solve this, for example by use of methods to determine index of space and then appending a whole substring, but this is simpler and more effective. Also, it might be good to use isspace(int _C) to check for whitespaces, because it detects also other ones, like tabs and such: if(!isspace(texts[i]))

Code:
for (int i=0;i<=(texts.length());i+=3) // Convertings
    {
        output[i]=texts[i+2];
        output[i+1]=texts[i+1];
        output[i+2]=texts[i];
    }
Again, you're accessing index that's higher than maximum safe index. You need to ensure in the condition that it never exceeds the maximum possible index. So it would be

Code:
for (int i=0;i < texts.length()-2;i+=3) // Convertings
    {
        output[i]=texts[i+2];
        output[i+1]=texts[i+1];
        output[i+2]=texts[i];
    }

Code:
cout << endl;
cout << "Output text: ";
Not erroneous, but here's a little trick to make it shorter:
Code:
cout << endl << "Output text: ";
Or even shorter (and faster)
Code:
cout << "\nOutput text: ";
'\n' is a newline character.

Code:
for (int i=0;i<=texts.length();i+=3) // Output the converted texts with a space every 3 characters
    {
        cout<<output[i];
        cout<<output[i+1];
        cout<<output[i+2];
        cout<<" ";
    }

Again, exceeding the index. Plus you can chain it too:
Code:
for (int i=0;i < texts.length()-2; i+=3) // Output the converted texts with a space every 3 characters
        cout<<output[i] << output[i+1] <<output[i+2] << " ";

Code:
cout<< endl;
cout<< endl;
cout<<"Press any key";
cin.get();

You can chain this again and there will be newline character left in the input stream, so you have to use cin.get() twice to get rid of it now, or use cin.ignore() to get rid of it right after it's created (when you read input).

Code:
cout << "\n\nPress any key";
cin.get(); // if you didn't get rid of newline first
cin.get();

***************


(09-02-2011, 01:02 PM)chipp Wrote:
Code:
while ((int i = 0), i <= text.length)
which is the same as:
Code:
int i = 0;
while (i <= text.length())

is read as: "while i is less or same with length of variable text"

hope that helps

Wrong. Little advice: if you're trying to help somebody, test the code you write first, because this is a nonsense (which also means that you should not try to help if you don't understand the subject properly, as you might only confuse them more and tell them nonsenses).

First, it's erroneous to use variable declaration in the comma statement according to the standard, so it will result in a compile-time error. Secondly if that was possible, which you can emulate like this:
Code:
int i;
while (i = 0, i < text.length())
It's still an error and it's NOT the same as the second piece code you wrote. That's because comma statement
Code:
expression1, expression2, ... , expressionN
works like this: it evaluates ALL expressions and then the value of the last expression becomes value of the whole statement.

That means, each time the while statement evaluates the condition, it first sets i to zero and only then performs the comparison. The value of the whole statement is the result of the "i < text.length()", but because the i is always reset to zero, it will result in an infinite loop, unless text.length() becomes zero, because the i is always reset to zero before the test!

Also, you omitted the () operator and wrote just text.length. You need to actually call the function, this is not C#, where this would be correct. Standard C++ doesn't support properties.


Also regarding the OS/compiler discussion:

If you use standard C/C++ and standard libraries, then it should compile with any compiler that complies with the standard and under any platform where is given compiler available. It should also work the same everywhere, unless it depends on something platform-specific in cases where the standard is somewhat loose (like the range of datatypes, int might be 16-bit on some platforms for example, while on PC's it's usually 32-bit).

Simply put, if you write standard C/C++ code with standard libraries and remain in the PC platform, then OS and compiler doesn't matter at all - so there's error in your code.
Oh thank you Frooxius, now i go to finish my C++ learning first. Frooxius you rocks :Laie:
[Image: dd.png]

Reply





Messages In This Thread
made a program - by 13295 - 09-01-2011, 11:57 AM



Users browsing this thread: 1 Guest(s)