Login Register






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


C++ program filter_list
Author
Message
RE: C++ program #11
(11-28-2011, 06:31 PM)esque Wrote: yAAA got it thanks Diabolic666 and Drayk for help

The two programs I ever used to write C++ was Notepad++ and Code::Blocks, I presonally like Code::Blocks more Smile
Everything you knew of, redefined.

Reply

RE: C++ program #12
Microsoft Visual Studio 2010 for the win.

Reply

RE: C++ program #13
It doesn't really matter that much what you use to write the code, as the language is always the same, so use whatever is most comfortable. Some offer extensions though, but it's not good to use them if you're learning the language, as they're deviations from the standard.

Some pointers though.

Code:
int i, choice;
    char c;
    while (choice!=4)

This while is a big error, you're using variable "choice" without initializing it - giving it some value first, so it contains some gibberish (leftovers in the memory). It will usually work, but it can also cause problems with your program, such instability or crashes, especially if the compiler attempts to optimize your code.

Never use variable without initializing it (giving it some value)

You're using it to control the loop, which is the while loop. While loop is a loop that is used when it doesn't have to run even once (can be completely skipped), if you need the loop to be executed at least once, use the do-while loop. This loop has condition at the end, so it's okay to use it there, as the variable choice gets initialized at the end. So you can write it as following:

Code:
void ascii()
{
    int i, choice;
    char c;
    do
    {
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        cout << "Choose an operation: \n\n";
        cout << "1. Number to char\n";
        cout << "2. Char to number\n";
        cout << "3. ASCII List\n";
        cout << "4. Exit\n\n";
        cout << "Selection: ";
        cin >> choice;
        cin.get();
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        switch (choice)
        {
            case 1:
                cout << "Number: ";
                cin >> i;
                cin.get();
                cout << "\n\nChar: " << (char)i;
                cout << "\n\n\nPress enter to go back...";
                cin.get();
                break;
            case 2:
                cout << "Character: ";
                cin >> c;
                cin.get();
                i=c;
                cout << "\n\nChar: " << i;
                cout << "\n\n\nPress enter to go back...";
                cin.get();
                break;
            case 3:
                for (int x = 1; x < 255; x++)
                {
                    cout << x << ": " << (char) x << "\n";
                }
                cout << "\n\n\nPress enter to go back...";
                cin.get();
                break;
            case 4:
                break;
        }
    } while (choice!=4)
}

Another thing that I don't like much is this line:

Code:
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

If you want to do something repeatedly like this, it may be better (cleaner) to use a loop, instead of writing so many "\n". For example:

Code:
for(int i = 0; i < 40; ++i)
   cout << "\n";

Technically, it's a bit slower, but in this case, it doesn't matter at all and it's much more cleaner. If you're going to use it repeatedly, you can wrap it in a function and use like this:

Code:
void WriteLine(unsigned int number)
{
for(int i = 0; i < number; ++i)
   cout << "\n";   // alternatively also cout << endl;
}

...

WriteLine(40);

Another thing that I find weird is that you have tons of "cout <<" for something. You can chain output for cout like this:

Code:
cout << "Choose an operation: \n\n
            << "1. Number to char\n"
            << "2. Char to number\n"
          << "3. ASCII List\n"
          << "4. Exit\n\n"
          << "Selection: ";

Also in the switch you have
Code:
case 4:
                break;
Which doesn't really do anything, so you can just leave it out.

Now to the main() function, there are a few problems too. Again, using the variable choice without initializing it, like I explained already, so I ignore that now. Then. You don't need to choice and choice2 variables, one is good enough, because characters (char) are basically numbers too (just 8 bit) so if you want to save space, you can save the input to character too, like this:

Code:
choice = 0;  // make it zero first, to erase upper bytes that won't be used
cin.get((char &)choice); // load character into the choice variable
cin.get(); // get rid of it newline in the input stream

Also the portion:
Code:
if (choice2=='Y' || choice2=='y')
Can be done like this, with function to convert it to lower case, so you don't have to check both
Code:
if( tolower(choice) == 'y' )

Also if you have only one command, you don't need the curly braces, so

Code:
if (choice2=='Y' || choice2=='y')
                {
                    choice=4;
                }
                else
                {
                    choice=3;
                }
Can be written like
Code:
if (choice2=='Y' || choice2=='y')
                    choice=4;
                else
                    choice=3;

Also, last thing: you declared the function main() as "int main()", meaning, it returns a value of type "integer". However, you do not return anything from inside the main, so you have to add at the end:
"return 0;" otherwise problems may arise. Or, declare main() as "void main()" if your compiler supports it, though main() should always return something, zero usually means that the program exited correctly.

If you have any questions, feel free to ask, I probably omitted some stuff, though I pointed out the most significant mistakes.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: C++ program #14
(11-29-2011, 06:17 PM)Frooxius Wrote: It doesn't really matter that much what you use to write the code, as the language is always the same, so use whatever is most comfortable. Some offer extensions though, but it's not good to use them if you're learning the language, as they're deviations from the standard.

Some pointers though.

Code:
int i, choice;
    char c;
    while (choice!=4)

This while is a big error, you're using variable "choice" without initializing it - giving it some value first, so it contains some gibberish (leftovers in the memory). It will usually work, but it can also cause problems with your program, such instability or crashes, especially if the compiler attempts to optimize your code.

Never use variable without initializing it (giving it some value)

You're using it to control the loop, which is the while loop. While loop is a loop that is used when it doesn't have to run even once (can be completely skipped), if you need the loop to be executed at least once, use the do-while loop. This loop has condition at the end, so it's okay to use it there, as the variable choice gets initialized at the end. So you can write it as following:

Code:
void ascii()
{
    int i, choice;
    char c;
    do
    {
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        cout << "Choose an operation: \n\n";
        cout << "1. Number to char\n";
        cout << "2. Char to number\n";
        cout << "3. ASCII List\n";
        cout << "4. Exit\n\n";
        cout << "Selection: ";
        cin >> choice;
        cin.get();
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        switch (choice)
        {
            case 1:
                cout << "Number: ";
                cin >> i;
                cin.get();
                cout << "\n\nChar: " << (char)i;
                cout << "\n\n\nPress enter to go back...";
                cin.get();
                break;
            case 2:
                cout << "Character: ";
                cin >> c;
                cin.get();
                i=c;
                cout << "\n\nChar: " << i;
                cout << "\n\n\nPress enter to go back...";
                cin.get();
                break;
            case 3:
                for (int x = 1; x < 255; x++)
                {
                    cout << x << ": " << (char) x << "\n";
                }
                cout << "\n\n\nPress enter to go back...";
                cin.get();
                break;
            case 4:
                break;
        }
    } while (choice!=4)
}

Another thing that I don't like much is this line:

Code:
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

If you want to do something repeatedly like this, it may be better (cleaner) to use a loop, instead of writing so many "\n". For example:

Code:
for(int i = 0; i < 40; ++i)
   cout << "\n";

Technically, it's a bit slower, but in this case, it doesn't matter at all and it's much more cleaner. If you're going to use it repeatedly, you can wrap it in a function and use like this:

Code:
void WriteLine(unsigned int number)
{
for(int i = 0; i < number; ++i)
   cout << "\n";   // alternatively also cout << endl;
}

...

WriteLine(40);

Another thing that I find weird is that you have tons of "cout <<" for something. You can chain output for cout like this:

Code:
cout << "Choose an operation: \n\n
            << "1. Number to char\n"
            << "2. Char to number\n"
          << "3. ASCII List\n"
          << "4. Exit\n\n"
          << "Selection: ";

Also in the switch you have
Code:
case 4:
                break;
Which doesn't really do anything, so you can just leave it out.

Now to the main() function, there are a few problems too. Again, using the variable choice without initializing it, like I explained already, so I ignore that now. Then. You don't need to choice and choice2 variables, one is good enough, because characters (char) are basically numbers too (just 8 bit) so if you want to save space, you can save the input to character too, like this:

Code:
choice = 0;  // make it zero first, to erase upper bytes that won't be used
cin.get((char &)choice); // load character into the choice variable
cin.get(); // get rid of it newline in the input stream

Also the portion:
Code:
if (choice2=='Y' || choice2=='y')
Can be done like this, with function to convert it to lower case, so you don't have to check both
Code:
if( tolower(choice) == 'y' )

Also if you have only one command, you don't need the curly braces, so

Code:
if (choice2=='Y' || choice2=='y')
                {
                    choice=4;
                }
                else
                {
                    choice=3;
                }
Can be written like
Code:
if (choice2=='Y' || choice2=='y')
                    choice=4;
                else
                    choice=3;

Also, last thing: you declared the function main() as "int main()", meaning, it returns a value of type "integer". However, you do not return anything from inside the main, so you have to add at the end:
"return 0;" otherwise problems may arise. Or, declare main() as "void main()" if your compiler supports it, though main() should always return something, zero usually means that the program exited correctly.

If you have any questions, feel free to ask, I probably omitted some stuff, though I pointed out the most significant mistakes.

First of all, thank you very much for your time and patience.

Since I'm somewhat new to this, I don't know much of the rules and I apreciate all that you pointed out and have corrected my application.

I just have a question about the choice and choice2 variables.

When you say I could do:

Code:
choice = 0;  // make it zero first, to erase upper bytes that won't be used
cin.get((char &)choice); // load character into the choice variable
cin.get(); // get rid of it newline in the input stream


I dont understand what the & does. I do understand that Im taking the input char and converting it to integer, right?

And also, if I'm converting it to integer, must I first check what the integer value of 'y' is and compare it to that value?

Once again, thank you very much for your help Smile
Everything you knew of, redefined.

Reply

RE: C++ program #15
The (char &) is a cast statement, it's used to explicitly tell the compiler to interpret that integer as a reference to datatype char. Now, why is that?

When you pass arguments to a function when calling it, there are two basic ways to do that: pass by value and pass by reference. When the variable is passed by value, then what happens under the hood is, that a copy of the variable is made for inside of the function. Like this for example:

Code:
void function(int b)
{
    b = 10;
}

int a = 2;
function(a);
cout << a;  // prints 2

In this case, the variable a with value 2 was passed to the function as b and the function changed the b to 10, however once the function exited, a is back to 2 again. In fact, it never changed. When the function was called like function(a), a copy of a was made and stored in b, so when it was changed it didn't affect the original a.

Passing by reference works differently though - it gives the function the original value, so it can alter it. You tell the compiler that you want it to be a reference by putting the & in front of it.

Code:
void function(int &b)
{
    b = 10;
}

int a = 2;
function(a);
cout << a;  // prints 10

And because function get() loads a character into some variable, it needs to be able to store the read character into that variable. So if you passed choice normally (by value) a copy would be made, get would read the character into it, but then it would get discarded, as it was just a copy. Therefore it must be passed as a reference, thus we need the &

What's also important is that get() requires datatype char, not int, but if you wrote just get(choice) it wouldn't compile, as compiler can't squeeze the int into char. However, if you put the cast in frnot of it, you tell it to take only portion of int and consider it a char. Furthermore, it needs to be a reference, thus it's (char &).

int is usually 4 bytes, while char is 1 byte. So if you put (char &) in front of it, it will basically give the get() function one byte of the 4 bytes of the int as a reference, so it can store the character into it. The other 3 bytes are zero, so they don't affect the loaded character anyhow.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: C++ program #16
So I'm converting the integer values to char, and can I compare it in the switch/case?
Everything you knew of, redefined.

Reply

RE: C++ program #17
Not really. There's no converting really. All data in computer are basically ones and zeroes and how they are interpreted depends on the code. It can interpret them as characters, or as an image or sound or even an instruction (code). Characters are nothing but numbers, the char datatype is usually 1 byte, while int is 4 bytes. You can easily store the character in the integer, but function get() can't work with whole int, so by placing (char &) you only give it an address of the int, but it will think that it's a char, so it will see only one byte and store the character there. However, outside of this function, it's still the int type, you just "tricked" it into thinking that it's char and to store the character from the input in it.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: C++ program #18
(12-01-2011, 01:04 PM)Frooxius Wrote: Not really. There's no converting really. All data in computer are basically ones and zeroes and how they are interpreted depends on the code. It can interpret them as characters, or as an image or sound or even an instruction (code). Characters are nothing but numbers, the char datatype is usually 1 byte, while int is 4 bytes. You can easily store the character in the integer, but function get() can't work with whole int, so by placing (char &) you only give it an address of the int, but it will think that it's a char, so it will see only one byte and store the character there. However, outside of this function, it's still the int type, you just "tricked" it into thinking that it's char and to store the character from the input in it.

I see now, thank you very much, you've been very helpfull! Kudos Smile
Everything you knew of, redefined.

Reply







Users browsing this thread: 1 Guest(s)