0

I have a question regarding making a calculator.

So i was given a assignment to make a calculator which accepts two doubles and makes either of 4(+,-,/,*) operations, everything seems to be working except one thing, when entered variable isn't double i want code to show an error and while loop to start again,but i can't pull that off. Any advises will be welcome. Thanks in advance. https://code.sololearn.com/cXuAotBrOR2d/?ref=app

19th Mar 2021, 10:49 AM
Giorgi Kekelidze
Giorgi Kekelidze - avatar
6 Antworten
+ 2
Quoted from your question: "everything seems to be working fine except one thing". I think you maybe made some changes to the code because no, everything is not working fine currently. Here are the reasons why: 1) Line 10: this line does nothing in its current form. Maybe you wanted to do ``` num1 = itsnum1; num2 = itsnum2; ``` 2) Line 30 & 91: you cannot pass more than one argument to the delete keyword by separating with commas. The commas have no effect and only the first variable of the comma-separated list is actually deleted. This means that on line 30, only `x` is deleted while on line 91, only `num1` is deleted. This is why the compiler gives the warning 'warning: right operand of comma operator has no effect ' You need to delete each variable separately, like so ``` delete num1; delete num2; .... ``` 3) Line 78: `yesorno == false;` this line makes no sense. What were you trying to do? It is safe to remove it (to be continued in next answer)
19th Mar 2021, 12:36 PM
XXX
XXX - avatar
+ 2
4) On line 69, you are deallocating the memory pointed by variables num1, num2, and op. But after that line, you are again assigning to these variables. So technical,y you are using memory that is not allocated to you. This can be very unsafe. The above point is the direct reason for the final problem: Even though there are no warnings now, the code will run into a segmentation fault. This is because on line 69, you are deallocating the memory pointed to by `num1` and `num2`. But, as you are not allocating memory again, when the loop executes one more time, `num1` and `num2` will again be deallocated. This results in a segmentation fault. The best way to address this problem is to not call Calc.del() at all as there is not really any need to deallocate and reallocate memory in every iteration of the loop. Here is a fixed version of your code https://code.sololearn.com/cBXfgUJgN6pG/?ref=app
19th Mar 2021, 12:47 PM
XXX
XXX - avatar
+ 2
Lastly, this is a suggestion. I know this is probably for learning purposes only, but playing so much with memory for such a small task is generally not useful. Playing with memory is always unsafe and can result in errors that are very hard to locate (as you saw in your code). So in a small code like yours which mostly only uses primitive types, there isn’t much need of allocating memory on the heap. Rather, stick with using normal variables that are not pointers.
19th Mar 2021, 12:47 PM
XXX
XXX - avatar
0
Thanks for the help i have noted everything you said but i still have one more question, is it possible for code to start while loop from the beginning if entered variables are wrong?
19th Mar 2021, 2:12 PM
Giorgi Kekelidze
Giorgi Kekelidze - avatar
0
Giorgi Kekelidze sorry for replying late. Yes it is possible. You could do so by simple removing the `break` statement on line 93 (in the code I mentioned). But...... it's not as simple as that. When an error occurs while taking input, a flag is set on `cin` (or any object of std::basic_istream or std::istringstream) which tells that the input has failed. cin.fail() returns true when the flag is set to true. You will need to unset that flag to take furthur input. Then, you will also have to ignore the bad input or else the input will fail again. Keeping these 2 things in mind, I have made a code that works. Main changes I have made: 1. I have moved the `else` part of the condition `if (!cin.fail())` into a separate function. 2. Instead of checking if the input failed after all the inputs are taken, I am checking each time a numerical input is taken (just because it makes more sense to notify the user when they enter an invalid number instead of making them give all the inputs and then notifying)
20th Mar 2021, 2:08 PM
XXX
XXX - avatar
0
Giorgi Kekelidze Please make sure go through the comments I have included in the code. Try giving the following input: 10 20 + yes hello yes 10 bye yes 4 5 * no (Or anything else, this is just an example) https://code.sololearn.com/cco4SyZcd2yi/?ref=app
20th Mar 2021, 2:09 PM
XXX
XXX - avatar