| | |
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:
Solved Threads: 0
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?
Why won't it compile? What code is wrong?
C++ Syntax (Toggle Plain Text)
#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; }
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.
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.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
Here's the error code that I got when I tried to compile it into g++ :
C++ Syntax (Toggle Plain Text)
~/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. 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.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
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?
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.
C++ Syntax (Toggle Plain Text)
num3 = (num1 * num2) / 2.0; 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.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
Excellent. Thank you for that.
Now, I'm just having some problems getting the reversed number to print.
Now, I'm just having some problems getting the reversed number to print.
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 1
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;
}
#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;
}
You know using sstream is a lot easier :
C++ Syntax (Toggle Plain Text)
#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; }
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
![]() |
Similar Threads
- Mystery with getline, very simple program (C++)
- Porting simple program from linux to minix (Kernels and Modules)
- Help with Simple C program :) (C)
- why i cann't execute my simple program(an array of pointers to object) (C++)
- Can someone help me develop a simple program (Visual Basic 4 / 5 / 6)
- can anyone help me develop a simple program (Visual Basic 4 / 5 / 6)
- Can't get program to compile (C++)
- Need advice & help with a very simple program (IT Professionals' Lounge)
- Erroneus outputs in string manipulation simple program (C++)
- I am a girl doing my 1st simple program (C)
Other Threads in the C++ Forum
- Previous Thread: Output capital letters
- Next Thread: user inputs only a #
Views: 435 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






