From java to C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 50
Reputation: volscolts16 is an unknown quantity at this point 
Solved Threads: 0
volscolts16's Avatar
volscolts16 volscolts16 is offline Offline
Junior Poster in Training

From java to C++

 
0
  #1
Jan 21st, 2008
Well another semester has started almost done with all java classes, and now C++ has started. I like it so far it is shorter and somewhat easier code. I have a homework assignment which I have started on, but with just starting C++ not sure where to go. Class meets tomorrow and assignment is due Thursday. A fairly simple enough homework, but I keeping getting stumped, several errors are coming up, and we are just started learning debugging. I am using Visual Studio 2005, its what we're using in class.

Assignment:
Allow a user to input 2 fractions and compute their sum.
such as first fraction: 1/2
second fracton: 2/5
Sum: 9/10

My code:
  1. 1. # include<iostream>
  2.  
  3. 2. using namespace std;
  4. 3. /* This program will prompt the user for two fractions and then it will figure their sum in a fractional form.*/
  5.  
  6. 4. int main ()
  7. 5. {
  8. 6. int a;
  9. 7. int b;
  10. 8. int c;
  11. 9. int d;
  12. 10. int fraction first = a/b;
  13. 11. int fraction second = c/d;
  14.  
  15.  
  16.  
  17. 12. cout<< "Enter first fraction;"<<endl;
  18. 13. cin>> a/b;
  19. 14. cout<< "Enter second fraction;"<< endl;
  20. 15. cin>> c/d;
  21. 16. cout>> "Sum ="<< ((a*d)+(b*c))/(b*d)<<endl;
Last edited by Ancient Dragon; Jan 21st, 2008 at 1:07 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,520
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: From java to C++

 
0
  #2
Jan 21st, 2008
variables a, b, c and d can not be input like that. Do it like this:
cin >> a >> b; You can't type the division symbol, but only the two numbers

you don't need lines 10 and 11, so you might as well delete them because they are wrong anyway.


BTW: Don't manually add line numbers when you post code because it makes it difficult for other to copy/paste into their own compiler for testing. Use code tags, which will add line numbers like this
[code=c++]
// your code goes here
[/code]
Last edited by Ancient Dragon; Jan 21st, 2008 at 1:13 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 35
Reputation: brk235 is an unknown quantity at this point 
Solved Threads: 1
brk235 brk235 is offline Offline
Light Poster

Re: From java to C++

 
0
  #3
Jan 23rd, 2008
Hi Here iam pasting code for your question....when u compile it u will get what you want....please reply me if you want more than this..
  1. # include<iostream>
  2.  
  3. using namespace std;
  4. /* This program will prompt the user for two fractions and then it will figure their sum in a fractional form.*/
  5.  
  6. int main ()
  7. {
  8. int a;
  9. int b;
  10. int c;
  11. int d;
  12.  
  13. cout<< "Enter values for the first fraction;"<<endl;
  14. cin>>a>>b;
  15. cout<< "Enter values for the second fraction;"<< endl;
  16. cin>> c>>d;
  17.  
  18. cout<<"First fraction ="<<a<<"/"<<b<<endl;
  19. cout<<"Second fraction ="<<c<<"/"<<d<<endl;
  20.  
  21. cout<< "Sum ="<< ((a*d)+(b*c))<<"/"<<(b*d)<<endl;
  22.  
  23. return 0;
  24. }
Last edited by Ancient Dragon; Jan 23rd, 2008 at 8:06 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC