error in my code!!

hey all :)))
im getting error on my code :(

int main()
{

string inp;
int dec=0; //store
char base;
cout << "input number: ";
cin >> inp;
cout << "input base: ";
cin >> base;
L17 for (int i = inp.length ()-1; j = 0; ++j) dec += (inp[i]-48) * pow ((float) base-48,j);
cout << "your number in base 10 is: " << dec << endl;
L19 system ("pause");
return 0;

LINE 17 ERROR:
-ERROR: 'j' was not declared in this scope
-ERROR: 'pow' cannot be used as a function

LINE 19:
-ERROR: 'system' was not declared in this scope

*using codeblocks

tyy!! :)))
I think you need to include the math library as well as declare variable j. System ("pause") is not necessary. Whatever book is telling you to use it, throw it out.

I edited it and it now runs fine on my machine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	
	string inp;
	int dec=0; //store
	char base;
	cout << "input number: ";
	cin >> inp;
	cout << "input base: ";
	cin >> base;
	
	for (int i = inp.length ()-1; int j = 0; ++j) dec += (inp[i]-48) * pow ((float) base-48,j);
	cout << "your number in base 10 is: " << dec << endl;

	return 0;
}


This needed to be added:

#include <iostream>
#include <cmath>
using namespace std;

for (int i = inp.length ()-1; int j = 0; ++j) dec += (inp[i]-48) * pow ((float) base-48,j);
Last edited on
-ERROR: 'j' was not declared in this scope

Well, here you have for (int i = inp.length ()-1; .... Did you mean int j = inp.length()-1? Also, this j = 0; will make it so that your loop will never execute (it will evaluate to zero which is false). I don't know exactly what you're trying to do so I can't help you here, though you will have to change this terminating condition.

-ERROR: 'pow' cannot be used as a function

Yes it can :s Did you #include <cmath> ?

-ERROR: 'system' was not declared in this scope

You need to #include <cstdlib> to use the system function (but don't)
theres no way I can use this logic as a string?? (its for class and we havent leared cmath)
pow is declared in <cmath> or <math.h>

You need to re-examine your for loop though. Right now it doesn't execute. You should start i at inp.length()-1 and j at zero. You should terminate when i becomes less than zero, and you should be incrementing j and decrementing i.
Im trying to
input number
input base
print DECIMAL

not working!!
Can we see the new code?

Another error I see is this pow ((float) base-48,j). If base is eight (octal), for example, then you'll be adding the current digit times -40j.
#include <iostream>
#include <cmath>

using namespace std;


int main()
{

string inp;
int j;
int dec=0; //store
char base;
cout << "input number: ";
cin >> inp;
cout << "input base: ";
cin >> base;
for (int i = inp.length ()-1; j == 0; ++j) dec += (inp[i]-48) * pow ((float) base-48,j);
cout << "your number in base 10 is: " << dec << endl;

return 0;
}





this when I input..prints 0
j == 0;

At this point, j is uninitialized. If j just happens to be zero, then this will be an infinite loop. Your loop shouldn't terminate when j != 0 (which is what it currently says). I think what you want to do is start i at inp.length ()-1 (last character) and move i to (first character). As such, you should terminate the loop when i becomes less than zero.

pow ((float) base-48,j)

I went over this in my last post but I was wrong. base-48 works because base is a char, though why don't you just declare base as an int so you don't have to subtract 48.

(inp[i]-48)

On the subject of 48, it's a magic number. You should do inp[i] - '0' instead. This goes the same for above, pow ((float) base - '0',j) though like I said you should just make base an int

Your main issue is with your for loop header. This is what you should be doing:

shacktar wrote:
You should start i at inp.length()-1 and j at zero. You should terminate when i becomes less than zero, and you should be incrementing j and decrementing i.

If you use a for loop, then you will be initializing i and j as well as incrementing j and decrementing i. This wouldn't follow the basic for(T i = 0; (some condition); i++) structure. You may want to implement the above using a while loop instead if you can wrap your head around it better.
Topic archived. No new replies allowed.