Login Register






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


Help with Beginner Code filter_list
Author
Message
RE: Help with Beginner Code #11
Yea, I was like... welll uhmm :/ kinda already fixed.. Confused
[username], need some help?, PM me.
[Image: kjKks6Y.png]


RE: Help with Beginner Code #12
(11-25-2012, 06:15 PM)Frooxius Wrote: First, it's great that you experiment with what you've already learned, instead strictly following the book that's the best way to learn something very well!

Secondly, in this case, you'll need to go a bit further in the book though, to learn about something called loops.

Loops essentially allow you to repeat a portion of the same code how many times you want, which is exactly what you want to do in this scenario.

------------------------

First this I would do is to move the code that checks if the user wants to end outside of the Multiply function. Remember that in good code, function should always do only what its name says, which in case of Multiply is multiplying. It doesn't care about anything else other than multiplying. Otherwise, as the code grows in complexity, it gets into a mess very quickly.

To keep it simple, make another function that will check if the user wants to end or not and return two value - true or false (these values are called boolean or bool for short), based on user's choice:


Code:
bool Another()
{
cout << "Another ('Y' or 'N')? "; //attempting to redirect 'Y' to Multiply() and 'N' to cout
     char d;
     cin >> d;
     if (d == 'N' or 'n')
        return false;
     else
        return true;
}

Now wrap the main program in a loop called do-while, which fill first execute a piece of your program and then checks a condition in the while statement - basically the function Another, which will ask the user whether to exit and return true or false based on his choice. If his choice is yes, then function returns true, which tells the do-while cycle to repeat the block again and it will keep doing so, until user chooses No.

Code:
/***************MAIN BLOCK***************/

int main()
{  
    do
    {
        cout << "Choose one: (M)ultiply, (A)dd, (S)ubtract, (D)ivide: \n";
        cout << "Letter of choice: ";
        char a;
        cin >> a;
        cin.get();

        if (a == 'M' or 'm')
        {
              Multiply();
        }
    } while( Another() );

    cout << "Have a nice day!";
    cin.get();

    return 0;
}

It should work now. There are other things that could be done better in this code, but I didn't want to change it too much, so it's not too much confusing to you at this point.

(11-21-2012, 06:35 AM)ArkPhaze Wrote: You're using System() which is already bad.

Using system() is not the best way to do it, but neither is the way you said it. He's just beginning with learning this and you slap him in the face with this and walk away. Now that's just rude. Try to explain why is it bad and point to better alternative if you want to be really helpful.

(Now back to HackerExecute): As you can see, I swapped it in the code for cin.get(), which is a better way to wait till user presses enter and also you can use it to filter the new-line character (the '\n') too instead of the cin.ignore(...) function.

You see, the input characters are handled by the operating system that runs your program. All the keys that are pressed are put into some sort of pipe, called stream. On the input of that pipe is the OS, sending all the keys that user pressed into it and on the other end is your program, which can read from it when it needs to.

When user presses a letter and then enter, the system sends into the pipe both the letter and the "Enter symbol". In your program, you read and remove from the pipe only the letter, so the pipe still contains the "Enter key".

So next time you try to read another letter from the input stream, the Enter key is still waiting there, so it will be read instead, which is what you don't want.

If you add cin.get() after the bit of code ("cin >> d" for example) that reads the letter, it will remove the Enter key from the stream, so it's clean again and another letter can be put into it. Simply put, the "New-line character, corresponding to the Enter key" won't be clogging the input pipe anymore.

It also works as waiting. If there's no enter key in the pipe, then cin.get() will wait until there's one, meaning until the user presses the Enter key, then it will continue. If it doesn't wait (meaning there already was an "Enter key" in the pipe, put two cin.get(); in a row. One to clean the pipe up, the other one to wait for the Enter.

------------------------------------------

I hope this helps you and your learning. If you have any questions, feel free to ask :3

I didn't say "don't use system() you idiot", if your interpretation of what I wrote seems to portray my statement as some kind of insult then that's your (mis)interpretation of it, which is not my fault.

Furthermore, it wasn't intended to be rude at all. That's simply just the way I write things. If he would have heard me say it, he wouldn't have taken it the wrong way. It wasn't sarcastic, and it wasn't in a strict tonality. With how much of a sentence that is though in itself, you can only assume things as there isn't much there.

Then again I'm not a journalist, so if you'll back him up on the fact that he's just starting out in C++, someone should be able to back me up as well. English was not my first language. I learned actually through forums for the most part. (Progressed by posting on forums the most anyways.)

HERE is why System() is bad:
http://www.cplusplus.com/forum/articles/11153/
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]


RE: Help with Beginner Code #13
Oh god, here comes the drama. o.O
[username], need some help?, PM me.
[Image: kjKks6Y.png]


RE: Help with Beginner Code #14
(11-25-2012, 06:15 PM)Frooxius Wrote: Now wrap the main program in a loop called do-while, which fill first execute a piece of your program and then checks a condition in the while statement - basically the function Another, which will ask the user whether to exit and return true or false based on his choice. If his choice is yes, then function returns true, which tells the do-while cycle to repeat the block again and it will keep doing so, until user chooses No.[/color]

This is much better than what I had in mind. I'm barely at a small part where they mention true and false.


(11-25-2012, 06:15 PM)Frooxius Wrote: As you can see, I swapped it in the code for cin.get(), which is a better way to wait till user presses enter and also you can use it to filter the new-line character (the '\n') too instead of the cin.ignore(...) function.

You see, the input characters are handled by the operating system that runs your program. All the keys that are pressed are put into some sort of pipe, called stream. On the input of that pipe is the OS, sending all the keys that user pressed into it and on the other end is your program, which can read from it when it needs to.

When user presses a letter and then enter, the system sends into the pipe both the letter and the "Enter symbol". In your program, you read and remove from the pipe only the letter, so the pipe still contains the "Enter key".

So next time you try to read another letter from the input stream, the Enter key is still waiting there, so it will be read instead, which is what you don't want.

If you add cin.get() after the bit of code ("cin >> d" for example) that reads the letter, it will remove the Enter key from the stream, so it's clean again and another letter can be put into it. Simply put, the "New-line character, corresponding to the Enter key" won't be clogging the input pipe anymore.

It also works as waiting. If there's no enter key in the pipe, then cin.get() will wait until there's one, meaning until the user presses the Enter key, then it will continue. If it doesn't wait (meaning there already was an "Enter key" in the pipe, put two cin.get(); in a row. One to clean the pipe up, the other one to wait for the Enter.

This was GREAT information and really helped me out a lot. I appreciate the explanation for cin.get() as I didn't know what it was used for yet, but I did know it waited for an enter input.
The stream explanation was helpful also, I wasn't aware of ANY of that so this gave me a greater mental picture.
Thanks for all of the help and code revision, Froox Smile

(11-25-2012, 06:57 PM)RA1N Wrote: I fixed his problem a few days ago via PM. Showed him how to use a switch statement with Char values

There was still a problem. Break statements were missing.

(11-25-2012, 07:00 PM)Deque Wrote: He should have told so. Now Frooxius made a really great effort to help him and it's all for nothing. (But I hope people with similar problems might learn from it)

I didn't tell so because like I said above, there was still a problem until a wile ago. Froox did do a good effort and I learned an incredibly large amount from it. He cleared up a lot of confusion in my head as well as help my code be much cleaner and fixed a thing or two.

I also wanted to see what other replies I could receive, other ways to edit the code or use all information together to fix it a way I wanted. There's nothing wrong with letting others post so I can learn much more form them. Smile
[Image: 20t2nn7.jpg]
︻┳═一
HackerExecute
Pwned | Twitter


RE: Help with Beginner Code #15
HackerExecute: Glad to help! ^^ Also for this task you shouldn't need any break statements at all.

Also that's the reason why I say that things should be solved here in public, not somewhere in PM's, because nobody else can learn from it if it's in PM's, plus when it's public it's possible that other people notice some misinformation and help to correct it.


(11-25-2012, 07:03 PM)ArkPhaze Wrote: I didn't say "don't use system() you idiot", if your interpretation of what I wrote seems to portray my statement as some kind of insult then that's your (mis)interpretation of it, which is not my fault.

Furthermore, it wasn't intended to be rude at all. That's simply just the way I write things. If he would have heard me say it, he wouldn't have taken it the wrong way. It wasn't sarcastic, and it wasn't in a strict tonality. With how much of a sentence that is though in itself, you can only assume things as there isn't much there.

Then again I'm not a journalist, so if you'll back him up on the fact that he's just starting out in C++, someone should be able to back me up as well. English was not my first language. I learned actually through forums for the most part. (Progressed by posting on forums the most anyways.)

HERE is why System() is bad:
http://www.cplusplus.com/forum/articles/11153/

Blah blah blah, same excuses and irrelevant talks (I never said you did say that he's an idiot nor that it's an insult, therefore your argument is invalid, because it's based on "information" you made up), but you're totally missing (or intentionally ignoring) the point:

It's not very helpful to just say "that's a bad thing to do" and go away. This is a hacking forums, where we should value reason and intelligence and help people, meaning explain things to them, not tell them something is bad and leave them to rot.

If you're here just to argue, instead of helping others, I suggest you to stop now. That or actually learn something yourself and be helpful next time, instead of pointless arguing (with made up arguments) and all will be good.

It's perfectly fine to make mistakes and do things wrong if you learn from them. I do mistakes all the time, everyone does and the best thing is not to pointlessly argue about them, but try to avoid them next time.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.


RE: Help with Beginner Code #16
(11-25-2012, 08:55 PM)Frooxius Wrote: Also that's the reason why I say that things should be solved here in public, not somewhere in PM's, because nobody else can learn from it if it's in PM's, plus when it's public it's possible that other people notice some misinformation and help to correct it.

And another question if you don't mind... How would I continue applying if choices from the menu to the other functions?

I saw:
Code:
if (a == 'M' or 'm')
        {
              Multiply();
        }
    } while( Another() );

The brace before while is to close out for do, right? Would I begin a new statement under while, repeating the same code written above only this time replacing Multiply() with the next function in the block?
[Image: 20t2nn7.jpg]
︻┳═一
HackerExecute
Pwned | Twitter


RE: Help with Beginner Code #17
can someone teach me???


RE: Help with Beginner Code #18
Don't dig graves! Old thread and Closed!
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG








Users browsing this thread: 1 Guest(s)