RE: Help with Beginner Code 11-25-2012, 07:02 PM
#11
Yea, I was like... welll uhmm :/ kinda already fixed..
![Confused Confused](https://sinister.ly/images/smilies/set/confused.png)
[username], need some help?, PM me.
![[Image: kjKks6Y.png]](http://i.imgur.com/kjKks6Y.png)
![[Image: kjKks6Y.png]](http://i.imgur.com/kjKks6Y.png)
Help with Beginner Code filter_list | |
(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
(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]
(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.
(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
(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)
(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/
(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.
if (a == 'M' or 'm')
{
Multiply();
}
} while( Another() );