-
Some help plz!
Hello.. can anybody helps me? i'm trying to write a c++ program.. that does like this... enter a number like 123 and the program should read it in reverse... like 321!
i did it.. but there z something wrong... i cant guess where z it...
Code:
#include <iostream.h>
void main(){
int num,n,x;
cout<<"Enter a positive number please\n";
cin>>num;
for(int j=1; num>=j; j*=10){
for(int i=10; x>=n ; i*=10){
n= num%i;
x= n/j;
cout<<x;
}
}
if i put 12.. it would give me 212121..................... and if i give it 123 it wuld give me infinity and wrong numbers.. like 3231321123..... !!!! :confused: ... :(
-
I do not understand the logic of your program. But I do understand that you use uninitialized variables ( x and n ).
So your inner loop might or might not run at all.
Kurt
-
Code:
#include <iostream.h>
That should be just iostream (without the .h extension) and you'll likely need to put a using namespace std; line under that for now until you understand about namespaces:
Code:
#include <iostream>
using namespace std;
The function main should always return an int.
Code:
for(int i=10; x>=n ; i*=10){
One problem is that n and x are not initialized at this point in the program, they could have almost any value in it.
These types of problems are often better handled by reading the input in as a text string instead of an integer. It is then easy to use text manipulation to reverse the characters making up the string.
-
thnx alot for helpin' me! :) but about the ogic of my program!! dont ask me! ask the programmer who wrote it.. cuz actually i was studyin' and i found this q in the homework i had to do!! so i tired to do it!!!
and about the <iostream> without .h... i dont think that it goes with my Borland c++ 4.52!
-
I'm no expert myself, but what are the errors you get if you use :
Code:
#include <iostream>
You sure it's Iostream that's causing what ever problem it is ? They should both be compatible. (Compiler and Iostream :D )
-
well yes.. i'm so sure... <iostream.h> has nothing to do with the errors i have :(.. and if i tried to use
Code:
# include <iostream>
using namespace std;
it would tell me
Code:
Compiling NONAME00.CPP:
Error NONAME00.CPP 1: Unable to open include file 'IOSTREAM'
Error NONAME00.CPP 2: Declaration syntax error
Error NONAME00.CPP 4: Undefined symbol 'cout' in function main()
.... !
-
well yes.. i'm so sure... <iostream.h> has nothing to do with the errors i have :(.. and if i tried to use
Code:
# include <iostream>
using namespace std;
it would tell me
Code:
Compiling NONAME00.CPP:
Error NONAME00.CPP 1: Unable to open include file 'IOSTREAM'
Error NONAME00.CPP 2: Declaration syntax error
Error NONAME00.CPP 4: Undefined symbol 'cout' in function main()
.... !
-
Borland 4 is stone age, but you're right, it doesn't have the versions without .h.
Unless you really still use MS-DOS, I highly advise that you get a proper compiler. You can get Bloodshed Dev-C++, or various free offers from MS.