Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

'==' : 'char *' differs in levels of indirection from 'int'

TonyProgramsTonyPrograms Posts: 10Member
Here is my code

#include
#include
#include
using namespace std;
void getTime (int *);
void getDuration (int *);
void getDay (char *, char *);
void outputCost (int *,int *,int *,char *,char *, char *);
const double mid_day = 0.3;
const double night = 0.15;
const double weekend = .1;

int main ()
{
int cost;
int yourTime;
char first, second, choice('Y');
getDay (&first, &second);
getTime (&yourTime);
int yourDuration;
getDuration (&yourDuration);
outputCost (&cost,&yourTime,&yourDuration,&first,&second,&choice) ;
system ("pause");
return 0;
}

void getDay (char *first, char *second)
{
cout<<"Please enter the day of the call in a two "<<endl;
cout<<"character format, for example Monday would be Mo. "<<endl;
cout<<"Please input the first two letters of the day now. "<<endl;
cin>> *first >> *second;
}

void getTime (int *yourTime)
{
cout<<"Please enter the time in military format." <<endl;
cin >> *yourTime;
}

void getDuration (int *yourDuration)
{
cout<<"Please enter the duration of the call using the number minutes." <<endl;
cin >> *yourDuration;
}

void outputCost (int *cost,int *yourTime,int *yourDuration, char *first, char *second, char *choice)
{
cout<<"Now I am going to calculate the total cost of "<<endl;
cout<<"your phone call."<<endl;

do
{

if (*first=='M'||*first=='m'&& *second=='o'||*second=='O')
{
((*yourTime>=700)||(*yourTime<=2100));

*cost=(int)(*yourDuration * mid_day);
}
else
{
*cost=(int)(*yourDuration * night);
}
if (*first=='T'||*first=='t'&&*second=='U'||*second=='u')
{
((*yourTime>=700)||(*yourTime<=2100));

*cost=(int)(*yourDuration * mid_day);
}
else
{
*cost=(int)(*yourDuration * night);
}
if (*first=='W'||*first=='w'&&*second=='E'||*second=='e')
{
((*yourTime>=700)||(*yourTime<=2100));

*cost=(int)(*yourDuration * mid_day);
}
else
{
*cost=(int)(*yourDuration * night);
}
if (*first=='T'||*first=='t'&&*second=='H'||*second=='h')
{
((*yourTime>=700)||(*yourTime<=2100));

*cost=(int)(*yourDuration * mid_day);
}
else
{
*cost=(int)(*yourDuration * night);
}
if (*first=='F'||*first=='f'&&*second=='R'||*second=='r')
{
((*yourTime>=700)||(*yourTime<=2100));

*cost=(int)(*yourDuration * mid_day);
}
else
{
*cost=(int)(*yourDuration * night);
}
if (*first=='S'||*first=='s'&&*second=='A'||*second=='a')
{
*cost=(int)(*yourDuration * weekend);
}
if (*first=='S'||*first=='s'&&*second=='U'||*second=='u')
{
*cost=(int)(*yourDuration * weekend);
}


cout<<"The phone call cost $ ";
cout<<*cost<<endl;
cout<<"Do you wish to calculate another cost? Press Y or N" <<endl;
cin>> choice;
}
while ((choice=='Y')||(choice=='y'));


}



and when i try to compile thats what the error says : error C2040: '==' : 'char *' differs in levels of indirection from 'int'
_____and im really unsure why this is occuring

Comments

  • pseudocoderpseudocoder Posts: 673Member ✭✭


    The incompatible types error is within the outputCost function; you just forgot to dereference choice.

    cin >> choice; should be
    cin >> *choice;

    while(choice == 'Y' || choice == 'y'); should be
    while(*choice == 'Y' || *choice == 'y');


    If you intend to enter values again, the do-while loop isn't going to work in the way you're wanting it to. If your user chooses yes to calculate another cost, the loop will continuely compute the existing values for each variable in the parameter list.

    What you might do is remove the do-while from outputCost and put it in main, initialize your variables, and then call your functions. For example,

    [code]
    int main(void) {
    ... // declare your variables and any other bits of code

    do {

    cost = 0; yourTime = 0; yourDuration = 0; // init values to 0
    first = 'n'; second = 'n'; // init to value other than Y

    getDay(&first, &second);
    getTime(&yourTime);
    getDuration(&yourDuration);
    outputCost(&cost, &yourTime, &yourDuration, &first, &second, &choice);

    } while(choice == 'Y' || choice == 'y');

    ... // any other bits of code

    return 0;
    }[/code]

    Other things:

    in getDay, don't leave the function unless first and second are valid. The way it is now, a user could enter ZX, for example, and the program would try and calculate a cost.

    in getTime, don't leave the function unless time is 0-23 (assuming its hourly only).

    in getDuration, make sure the value is >= 0.

    That way, you ensure that your data is valid before trying to calculate a result. Not sure about the calculation formulas; if you're getting the expected output for the input, then ok, otherwise, it may be worth your while to take another look.

    HTH

  • TonyProgramsTonyPrograms Posts: 10Member
    o great thanks it worked, very helpful
  • TonyProgramsTonyPrograms Posts: 10Member
    This post has been deleted.
  • TonyProgramsTonyPrograms Posts: 10Member
    ok now that i fixed it, it says choice is undefined, do i need to undeclare it
  • pseudocoderpseudocoder Posts: 673Member ✭✭
    Where is choice defined? if you declared it inside of the do-while loop, then its scope is valid within the body of loop code block and not main.

    [code]
    do {
    char choice;
    ...
    } while(choice == 'Y'); <-- 'choice' will be undefined here.
    [/code]

    To fix that, just move its declaration above the do-while. If it's somewhere else, check your function parameter lists for matching names.

    HTH
Sign In or Register to comment.