I have tried all the fixes I found posted online, but they are not working for me. I've tried changing my data types, and I have included <cmath> but I still don't see how to fix this. Other advice has been to not use the pow function, but my teacher told us to use it.
Here is my program, this error is on line 50. Thank you in advance. (I'm sorry if this is not posted in the proper format, but I'm trying.) My error looks like this:
|50|error: invalid operands of types 'int' and 'double' to binary 'operator%'|
1 #include <iostream>
2 #include <fstream>
3 #include <cmath>
4
5 using namespace std;
6
7 //Function prototype
8 int convertedNum(int v, int b);
9 //Declare variables
10 int v,b,k;
11 //File descriptors
12 ifstream inFile;
13 ofstream outFile;
14
15 //Main function
16 int main ()
17 {
18 //Introduction
19 cout<<"*******************************************" << endl
20 <<"* This is a program that will convert any *" << endl
21 <<"* base 10 number v to any base b. *" << endl
22 <<"*******************************************" << endl;
23
24 //Read in file
25 inFile.open("BaseFile.txt");
26 if(inFile.is_open())
27 {
28 cout<<"File did open."<<endl;
29 return 0;
30 }
31
32 outFile.open("Results.out");
33
34 inFile >> v >> b;
35
36 outFile << v <<" in base 10 is "<<endl
37 << convertedNum(v,b)<<endl;
38 outFile << " in base " << b << endl;
39 inFile.close();
40 outFile.close();
41 return 0;
42 }
43 //Function definition
44 int convertedNum(int v, int b)
45 {
46 int k = floor((log10(v)/log10(b)+1));
47 while (k>0)
48 {
49 outFile << v/(pow(b,k-1))<< endl;
50 v = v%(pow(b,k-1));
51 k--;
52 }
53 }
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace std;
//Function prototype
int convertedNum(int v, int b);
//Declare variables
int v,b,k;
//File descriptors
ifstream inFile;
ofstream outFile;
//Main function
int main ()
{
//Introduction
cout<<"*******************************************" << endl
<<"* This is a program that will convert any *" << endl
<<"* base 10 number v to any base b. *" << endl
<<"*******************************************" << endl;
//Read in file
inFile.open("BaseFile.txt");
if(inFile.is_open())
{
cout<<"File did open."<<endl;
return 0;
}
outFile.open("Results.out");
inFile >> v >> b;
outFile << v <<" in base 10 is "<<endl
<< convertedNum(v,b)<<endl;
outFile << " in base " << b << endl;
inFile.close();
outFile.close();
return 0;
}
//Function definition
int convertedNum(int v, int b)
{
int k = floor((log10(v)/log10(b)+1));
while (k>0)
{
outFile << v/(pow(b,k-1))<< endl;
v = v % static_cast<int>(pow(b,k-1));
k--;
}
}
Thank you very much. I had tried that, but I did it wrong. Whew! Now I can work on all the calculation errors and finish my loop. Much appreciated. Great site.