Need help for writing C++ code?

Need help for writing C++ code? Topic: Need help for writing C++ code?
June 18, 2019 / By Amice
Question: Having trouble with writing this any help would be appreciated thanks. Write a program that inputs two fractions (a numerator and a denominator for each). The program should work properly for all fractions, except those that have a zero in either denominator. If the user enters 0 in a denominator, give an error message and exit the program. You will then use these two fractions to do the following: Add the two fractions together and give the resulting fraction and the decimal equivalent. Remember that you can only add fractions that have the same denominator and for this lab you may just multiple the two denominator together (make sure you appropriately multiply the numerators), to give a common denominator, without worrying about the least common denominator. If the two fractions that were entered were 9/13 and 7/12 the output would be shown as both 199/156 and 1.2756. Multiply the two fractions together and give the resulting fraction and the decimal equivalent. If the two fractions that were entered were 2/9 and 6/3 the output would be 12/27 and 0.4444
Advertisement
Best Answer

Best Answers: Need help for writing C++ code?

Warwick Warwick | 7 days ago
So what issues are you having in terms of completing this assignment? You wrote down the homework description, but didnt ask a real question What part of this assignment dont you understand? We arent here to do your homework, but if you tell us what you are having trouble with, then we can help
👍 102 | 👎 7
Did you like the answer? Need help for writing C++ code? Share with your friends
Warwick Originally Answered: Writing code vs Writing?
Programming and writing an essay are two entirely different things. Programming is giving a machine detailed instructions on how to do something or how to react to user input. It doesn't even compare to writing a novel. In game design, if one guy on the team prefers to write the story while another prefers to code the game's engine, it's just because people usually prefer to do things they're good at and that they like doing. Also, the two activities require two completely separate skill sets. It's like driving a car versus fixing a car. Or painting a fence versus painting a picture.
Warwick Originally Answered: Writing code vs Writing?
They are vastly different. Writing computer code is more like building a machine than it is like writing a novel. Asking why people write programs instead of writing novels is like asking why mechanics build engines instead of painting artwork.

Rylan Rylan
i'm going to assum which you have a terminal window, or you're sitting in front of a linux computing gadget. when you have logged in you will the two be dealing with a terminal window or a private computing gadget. in case you spot a private computing gadget discover the thank you to start a terminal. interior the terminal you would be able to desire to apply a textual content cloth editor. for some years human beings have argued it extremely is greater constructive vi or emacs, yet now there are others. interior the terminal on the command line type "vi helloworld.cpp" or "emacs helloworld.cpp" or "xemacs helloworld.cpp" or "vim helloworld.cpp" or "gedit hellowrold.cpp" or "nano helloworld.cpp". All of those are occasion of textual content cloth editor in unix. Gedit is like homestead windows notepad, and there are menus to maintain documents. In vi and vim it extremely is not any longer as obtrusive, it extremely is the single that i exploit the main in view which you do no longer would desire to waste time touching your mouse. Whe vi starts you will see a sparkling demonstrate press i to be in interactive mode. type your code, and hist esc : w q this escapes you from interactive mode, and then writes and extremely vi or vim. In xemacs and emacs you should use the mouse like notepad. In nano the instructions are on the bottome of the demonstrate. i desire this helps.
👍 30 | 👎 0

Murdie Murdie
1. In your examples given, THE SECOND ONE, i.e. the last example you gave, IS WRONG. Here 's the program you need. Remember, separate numerator and denominator by a space when you give input. #include using namespace std; int main() {         int n1,d1, n2,d2;         cout<<"Enter the first fraction num and den, separated by spaces\n";         cin>>n1>>d1;         if(d1 == 0){                 cout<<"Invalid fraction, div by zero\n";                 return 0;         }         cout<<"Enter the second fraction\n";         cin>>n2>>d2;         if(d2 == 0){                 cout<<"Invalid fraction, div by zero\n";                 return 0;         }                  cout<<"Adding two fractions...\n";         int num,den;         if(d1 == d2){                 den = d2;                 num = n1 + n2;         } else{                 den = d1 * d2;                 num = n1*d2 + n2*d1;         }         cout<<"Result = "<
👍 24 | 👎 -7

Murdie Originally Answered: Evaluate my simple source code?
1) using global variables is a bad thing. You should try not to do that especially if its not really needed. so moove the variables into main. if (nChoice <= 0, nChoice >5) { cout << "Please make a valid selection"< #include i think you can skip one of these two, not sure though try removing them one by one and see if it compiles without errors last suggestion, put the whole thing into a loop so program does not close after every single equation e.g. int main() { do{ {your code} }while(nChoice!=5); system("pause"); return 0; } edit: and something that i didnt noticed but someone did, if (nChoice <= 0, nChoice >5) dont use comma in this ... just use the logical OR operator ('||') like this... if (nChoice <= 0 || nChoice >5) now that i check again .. that guy suggested you to use the and operator ... and well... this is not going to work right :P, || is the way to go ^_^. EDIT2: No:/ comma operator does not work like this it is going to work like this: (left,right)------>result true,true//this will return true false,false//this returns false false,true//this returns true true,false //THIS WILL WORK BAD!!! IT WILL RETURN FALSE, when you want this to return true!! EDIT3: just noticed... begining: thats a label, it only gets used by goto command e.g. goto begining; will get you on the beginning of your code. Using goto is a bad programming style, just don't do it! using just the label will not do any harm (maybe take up some more space on disk after compile but still not sure.. we are talking just for a couple of bytes in here..) but its not really needed to be put in there.
Advertisement

If you have your own answer to the question Need help for writing C++ code?, then you can write your own version, using the form below for an extended answer.