I am working to assign a pointer to a double within C++. When I take the pointer out it runs fine however as soon as I run it with the pointer I get an error saying "cannot convert 'double*' to 'double' in assignment". I cannot figure out why this is giving me this error. Any help is greatly appreciated.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double ROP, HW, OT,oSal;
int choice;
char choice2= (toupper(choice2));
double * pbsal[2];
double bsal[2];
cout<<setprecision (2)<<fixed;
cout<<"Welcome to the salary calculator!!!"<< endl;
cout<<""<<endl;
cout<<"Please enter your hourly rate: "<<endl;
cin>>ROP;
while(choice != 3){
cout<<"Would you like to:"<<endl;
cout<<"1. Enter the number of hours you worked?"<<endl;
cout<<"2. Enter the number of overtime hours you worked?"<<endl;
cout<<"3. Exit the program"<< endl;
cin>>choice;
switch(choice){
case 1:
cout<<"Please enter the number of hours you worked(excluding
overtime):"<<endl;
cin>>HW;
cout<<"Would you like to see your salary based on the number of
hours you worked?"<<endl;
cin>> choice2;
break;
case 2:
cout<<"Please enter the number of hours of overtime you worked:"<<
endl;
cin>>OT;
oSal = (ROP*1.5)*OT;
cout<<"Your overtime salary is: $"<<oSal<<endl;
break;
}
switch(choice2){
case 'Y':
for(int i =0; i<=2; i++){
bsal[i]=ROP*HW;
*pbsal[i]= &bsal[i];
}
cout<<"Your salary based on the hours you provided is: $"
<<bsal<<endl;
cout<<"test"<<*pbsal[0]<<endl;
cout<<""<<endl;
break;
case 'N':
cout<<"Thank you!"<<endl;
cout<<""<<endl;
break;
}
}
}
This is the error that I am receiving:
15:45:42 **** Incremental Build of configuration Debug for project Module4 ****
make all
Building file: ../src/Module4.cpp
Invoking: Cygwin C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Module4.d" -MT"src/Module4.o" -o "src/Module4.o" "../src/Module4.cpp"
../src/Module4.cpp: In function 'int main()':
../src/Module4.cpp:64:25: error: cannot convert 'double*' to 'double' in assignment
*pbsal[i]= &bsal[i];
^
make: *** [src/subdir.mk:20: src/Module4.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.
I want to be able to assign the pointer as a double.
Pointers in C++ are a little bit different than their C counterpart. It should be invoked using the keyword new
for efficient memory management. double *pbsal = new double[2];
Now pbsal[0]
and pbsal[1]
are just double values. And the loop should be i<2
not i<=2
.
bsal[i] = ROP*HW;
pbsal[i] = bsal[i];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.