wrong output

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

wrong output

 
0
  #1
May 3rd, 2008
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main( )
  5. {
  6. float rand, cent, pounds, shillings;
  7.  
  8. cout << "Enter Pounds " << endl;
  9. cin >> pounds;
  10. cout << "Enter Shillings " << endl;
  11. cin >> shillings;
  12.  
  13. pounds =1;
  14. shillings =1;
  15.  
  16. // enter amount while both pounds and shillings is not equal to 0
  17. while
  18. (pounds != 0 && shillings != 0)
  19.  
  20. {
  21. cout << "Enter Pounds " << endl;
  22. cin >> pounds;
  23. cout << "Enter Shillings " << endl;
  24. cin >> shillings;
  25. // Calculate & convert pounds to rands
  26. rand = (pounds * 2 );
  27. // Calculate & convert shillings to cent
  28. cent = (shillings * 10);
  29. cout << "The pound is equal to R "<<rand << endl;
  30. cout <<"Shillings equells to "<< cent << " C "<< endl;
  31. }
  32. return 0;
  33. }
Last edited by Ancient Dragon; May 3rd, 2008 at 3:41 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
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: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: wrong output

 
0
  #2
May 3rd, 2008
It would be nice to know what this program is intended to do. It is trying to convert pounds and shillings to something, but I don't know what.
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: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

Re: wrong output

 
0
  #3
May 3rd, 2008
Ok Ancient Dragon

The program has to convert pounds to rands, and also convert shillings to cents also display the results. Asuming that there are 20 shillings in a pound and that 1 shilling to 10 cents.

The program to run in each line the first entry is the number of pounds and the second entry is the number of shillings.

please help
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: wrong output

 
0
  #4
May 4th, 2008
Originally Posted by loeto View Post
Ok Ancient Dragon

The program has to convert pounds to rands, and also convert shillings to cents also display the results. Asuming that there are 20 shillings in a pound and that 1 shilling to 10 cents.

The program to run in each line the first entry is the number of pounds and the second entry is the number of shillings.

please help
Lines 8 - 11 are unnecessary. You are already making sure that you go through the loop at least once with lines 13 and 14. If you change the loop from a while loop to a do-while loop, you can also delete lines 13 and 14. In lines 6, 26, and 29, be aware that rand is a C++ command:
http://www.cplusplus.com/reference/c...dlib/rand.html

You haven't included cstdlib, but you have said that you are using the standard namespace in line 2. If you get some error regarding "rand", that may be why. You can change it to something different.

You have pounds and shillings declared as floats. It can be dangerous to compare floats to an exact number as you have in line 18. However, since you are comparing them to 0, it's probably all right.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

Re: wrong output

 
0
  #5
May 4th, 2008
VernonDozier
I have made the changes but the program still gives wrong output

please anyone help
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: wrong output

 
0
  #6
May 4th, 2008
Originally Posted by loeto View Post
VernonDozier
I have made the changes but the program still gives wrong output

please anyone help
Originally Posted by loeto View Post
VernonDozier
I have made the changes but the program still gives wrong output

please anyone help
You're going to get the wrong output because the calculations never take place on lines 25 and 27. My suggestions only had to do with the loop control code. Work out mathematically on paper what the formulas are, test them out (on paper with a calculator) to make sure those formulas are correct, then when you are sure that they are correct, try to convert those formulas to C++ code and put them on lines 25 and 27 (each formula may take more than one line of code). If it doesn't work, post your revised program here, along with the input you give it, the output you get when you give it that input, and, if the output is different, what the correct output should be.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

Re: wrong output

 
0
  #7
May 4th, 2008
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int Rand, Cent, Pounds, Shillings; // pound = 20 shillings
  8.  
  9. do
  10. {
  11. cout << "Enter Pounds: " << endl;
  12. cin >> Pounds;
  13. cout << "Enter Shillings: " << endl;
  14. cin >> Shillings;
  15. }
  16. while (Pounds != 0 && Shillings != 0);
  17. {
  18. // Calculate & convert pounds to rands
  19. Rand = (Pounds * 2 );
  20.  
  21. // Calculate & convert shillings to cent
  22. Cent = (Shillings * 10);
  23.  
  24. cout << "The pound is equells to R "<<Rand << endl;
  25. cout <<"Shilling equells to "<< Cent<< " C "<< endl;
  26.  
  27. }
  28. return 0;#include <iostream>
  29. #include <cstdlib>
  30. using namespace std;
  31.  
  32. int main()
  33. {
  34. int Rand, Cent, Pounds, Shillings; // pound = 20 shillings
  35.  
  36. do
  37. {
  38. cout << "Enter Pounds: " << endl;
  39. cin >> Pounds;
  40. cout << "Enter Shillings: " << endl;
  41. cin >> Shillings;
  42. }
  43. while (Pounds != 0 && Shillings != 0);
  44. {
  45. // Calculate & convert pounds to rands
  46. Rand = (Pounds * 2 );
  47.  
  48. // Calculate & convert shillings to cent
  49. Cent = (Shillings * 10);
  50.  
  51. cout << "The pound is equells to R "<<Rand << endl;
  52. cout <<"Shilling equells to "<< Cent<< " C "<< endl;
  53.  
  54. }
  55. return 0;


Can anyone help please
This program only input the first input and stops, which is 12 pounds and 0 shillings
can you assist me so that program can input the other inputs
pounds & shillings
1 & 19
0 & 11
22 & 18
9 & 8
0 & 0 (ending)
Last edited by Ancient Dragon; May 4th, 2008 at 6:50 pm. Reason: replaced quote tags with code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: wrong output

 
0
  #8
May 4th, 2008
Originally Posted by loeto View Post
Can anyone help please
This program only input the first input and stops, which is 12 pounds and 0 shillings
can you assist me so that program can input the other inputs
pounds & shillings
1 & 19
0 & 11
22 & 18
9 & 8
0 & 0 (ending)
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int Rand, Cent, Pounds, Shillings; // pound = 20 shillings
  8.  
  9. do
  10. {
  11. cout << "Enter Pounds: " << endl;
  12. cin >> Pounds;
  13. cout << "Enter Shillings: " << endl;
  14. cin >> Shillings;
  15. }
  16. while (Pounds != 0 && Shillings != 0);
  17. {
  18. // Calculate & convert pounds to rands
  19. Rand = (Pounds * 2 );
  20.  
  21. // Calculate & convert shillings to cent
  22. Cent = (Shillings * 10);
  23.  
  24. cout << "The pound is equells to R "<<Rand << endl;
  25. cout <<"Shilling equells to "<< Cent<< " C "<< endl;
  26.  
  27. }
  28. return 0;
Looks like your brackets are off a little bit. There are no brackets ending the main function, so add a closing bracket after return 0; to end the main function. The brackets on lines 17 and 27 don't hurt, but they don't do anything either so you can delete them. Your do-while loop is from lines 9 - 16. It reads in data repeatedly but doesn't do any of the calculations. It will continually ask for input until it gets two zeroes. Then and only then will execute the calculation code. This calculation code will only execute once since it is not in a loop. Thus, if you want the calculation code to execute each time the user enters data, you are ending the do-while loop too soon. End it right above return 0; . So delete lines 17 and 27, move lines 15 and 16 to right after line 25, and add a closing bracket after return 0; .
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
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: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: wrong output

 
0
  #9
May 4th, 2008
it would help if you would learn how to format your code better.
  1. int main()
  2. {
  3. int Rand, Cent, Pounds, Shillings; // pound = 20 shillings
  4.  
  5. do
  6. {
  7. cout << "Enter Pounds: " << endl;
  8. cin >> Pounds;
  9. cout << "Enter Shillings: " << endl;
  10. cin >> Shillings;
  11. }while (Pounds != 0 && Shillings != 0);
  12.  
  13. // Calculate & convert pounds to rands
  14. Rand = (Pounds * 2 );
  15.  
  16. // Calculate & convert shillings to cent
  17. Cent = (Shillings * 10);
  18.  
  19. cout << "The pound is equells to R "<<Rand << endl;
  20. cout <<"Shilling equells to "<< Cent<< " C "<< endl;
  21.  
  22. return 0;
  23. }

As you can now see, the loop continuously asks for the #Pounds and Shillings until one or the other is 0. Each time you enter the amounts the previous amounts are destroyed. You probably should move lines 16-20 up inside the loop, after line 10.
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: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

Re: wrong output

 
0
  #10
May 5th, 2008
Thanx guys

So these means that the is no other way where by the program can only terminate when both input are 0's.

If the is please help me with that because is my only problem now.
i tried this:
while ((Pounds && Shillings )!= 0.0);
But if one (0) is entered the program ends
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC