Why won't this simple program compile?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

Why won't this simple program compile?

 
0
  #1
Sep 29th, 2009
I'm trying to compile a program that I created that takes a number and finds its average and reverses it. It's a very simple program, but when I try to compile it with g++, it gives me an error.
Why won't it compile? What code is wrong?

  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5. int num1;
  6. int num2;
  7. int num3;
  8. cout << "Please enter two positive integers with at most four digits each: ";
  9. cin >> num1 >> num2;
  10. cout << "Their average is " << num3 = (num1 * num2) / 2.0;
  11. int rev1;
  12. rev1 = 0
  13. {
  14. num3 * = 10;
  15. num3 += rev1%10;
  16. rev1 /= 10;
  17. }
  18. cout << "The whole part of the average reversed is " << rev1;
  19. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
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: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why won't this simple program compile?

 
-7
  #2
Sep 29th, 2009
Post the errors. My guess is that it doesn't like line 10.

lines 15 and 16: The value of revl is 0 (line 12) so the results of those calculations will also be 0.
Last edited by Ancient Dragon; Sep 29th, 2009 at 9:00 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: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

Re: Why won't this simple program compile?

 
0
  #3
Sep 29th, 2009
Here's the error code that I got when I tried to compile it into g++ :

  1. ~/assn/a2$ g++ -o test reverseNum.cc
  2. reverseNum.cc: In function âint main()â:
  3. reverseNum.cc:10: error: no match for âoperator=â in â((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Their average is ")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](num3) = (#âfloat_exprâ not supported by dump_expr#<expression error> / 2.0e+0)â
  4. /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:64: note: candidates are: std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::operator=(const std::basic_ostream<char, std::char_traits<char> >&)
  5. reverseNum.cc:13: error: expected `;' before â{â token
  6.  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
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: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why won't this simple program compile?

 
-7
  #4
Sep 29th, 2009
try using more parentheses, like this: cout << "Their average is " << (num3 = (num1 * num2) / 2.0); or splitting it into two separate lines.
Last edited by Ancient Dragon; Sep 29th, 2009 at 9:21 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: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

Re: Why won't this simple program compile?

 
0
  #5
Sep 29th, 2009
I tried adding the parenthases, and it still wouldn't compile.

I pulled the whole "num3 =" part out of that line and it compiled, so you're right, that's what's wrong with it.. I just can't figure out how to properly have that equation equal num3.

Any idea on how I can get num3 to equal the answer of that equation? How do you split the line into two, like you mentioned?
Last edited by Towely; Sep 29th, 2009 at 9:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
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: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why won't this simple program compile?

 
-7
  #6
Sep 29th, 2009
  1. num3 = (num1 * num2) / 2.0;
  2. cout << "Their average is " << num3 << "\n";
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: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

Re: Why won't this simple program compile?

 
0
  #7
Sep 30th, 2009
Excellent. Thank you for that.

Now, I'm just having some problems getting the reversed number to print.

  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5. int num1;
  6. int num2;
  7. int num3;
  8. cout << "Please enter two positive integers with at most four digits each: ";
  9. cin >> num1 >> num2;
  10. num3 = (num1 + num2) /2;
  11. cout << "Their average is " << (num1 + num2 / 2.0) << endl;
  12. int rev1;
  13. {
  14. num3 += rev1%10; // I'm fairly sure this is where I'm getting it wrong
  15. rev1 /= 10; // Are these three lines wrong?
  16. num3 *= 10;
  17. }
  18. cout << "The average reversed is " << rev1;
  19. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: taphofyle is an unknown quantity at this point 
Solved Threads: 1
taphofyle taphofyle is offline Offline
Newbie Poster

Re: Why won't this simple program compile?

 
0
  #8
Sep 30th, 2009
This will compile but i still don't get a good result.

#include <iostream>
using namespace std;
int main ()
{
int num1;
int num2;
int num3;
cout << "Please enter two positive integers with at most four digits each: ";
cin >> num1 >> num2;
cout << "Their average is " << (num3 = (num1 * num2) / 2.0);
int rev1;
rev1 = 0;
{
num3 *= 10;
num3 += rev1%10;
rev1 /= 10;
}
cout << "The whole part of the average reversed is " << rev1;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

Re: Why won't this simple program compile?

 
0
  #9
Sep 30th, 2009
Thanks for the reply,

I still can't figure out how to print the number in reverse. Please advise.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,458
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Why won't this simple program compile?

 
0
  #10
Sep 30th, 2009
You know using sstream is a lot easier :

  1. #include<iostream>
  2. #include<sstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9.  
  10. int NumberToReverse = 123456;
  11.  
  12. string str = "";
  13.  
  14. stringstream sstrm;
  15.  
  16. sstrm << NumberToReverse;
  17.  
  18. sstrm >> str; //str = "123456";
  19.  
  20. int i = str.length() - 1;
  21.  
  22. cout<<NumberToReverse <<" reversed is : ";
  23.  
  24. for( i; i >= 0; --i)
  25. cout<<str[i];
  26.  
  27. cout<<endl;
  28.  
  29.  
  30. return 0;
  31.  
  32. }
Last edited by firstPerson; Sep 30th, 2009 at 1:18 am.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Reply

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




Views: 435 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC