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?

#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;
}

Recommended Answers

All 12 Replies

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.

Here's the error code that I got when I tried to compile it into g++ :

~/assn/a2$ g++ -o test reverseNum.cc
reverseNum.cc: In function âint main()â:
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)â
/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> >&)
reverseNum.cc:13: error: expected `;' before â{â token

try using more parentheses, like this: cout << "Their average is " << (num3 = (num1 * num2) / 2.0); or splitting it into two separate lines.

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?

num3 = (num1 * num2) / 2.0;
cout <<  "Their average is " << num3  << "\n";

Excellent. Thank you for that.

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

#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;
   num3 = (num1 + num2) /2;
   cout << "Their average is " << (num1 + num2 / 2.0) << endl;
   int rev1;
   {
      num3 += rev1%10; // I'm fairly sure this is where I'm getting it wrong
      rev1 /= 10; // Are these three lines wrong?
      num3 *= 10;
   }
   cout << "The average reversed is " << rev1;
}

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;
}

Thanks for the reply,

I still can't figure out how to print the number in reverse. Please advise.

You know using sstream is a lot easier :

#include<iostream>
#include<sstream>

using namespace std;

int main()
{ 
	
	
	int NumberToReverse = 123456;
	
	string str = "";

	stringstream sstrm;

	sstrm << NumberToReverse;

	sstrm >> str; //str = "123456";

	int i = str.length() - 1;
	
	cout<<NumberToReverse <<" reversed is : ";
	
	for( i; i  >= 0; --i)
		cout<<str[i];
	
	cout<<endl;


	return 0;

}
rev1 = 0;
{
num3 *= 10;
num3 += rev1%10;
rev1 /= 10;
}

Looks like you are trying to loop some statements. But the syntax of the loop is not correct. Read http://www.cplusplus.com/doc/tutorial/control/ (Loops) to find out the correct format.

Ah, I've figured it out... Thank you for the help, everybody.

How do I give out Rep on this site? I can't figure it out.

>How do I give out Rep on this site?
What is 'Rep'.
If you mean reputation, can just click on the "Add to <username> Reputation" on the post for which you want to repute the user. Choose "I Approve" and click "Add to Reputation"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.