Hello,
I am new to this board and C++, so I do not even know what encase code in [] means, but I will try. I have an assignment I have been working on for a week. I have consulted my book and the web, but I have been unable to figure out the solution. I have most of the code completed, except I do not get the correct answer from my calculation.
Everything works except the amount due is incorrect. It is returning as $4e-039. I think it has something to do with the float and decimal precision, but I have tried everything I know, which is not much and cannot figure it out. Please help!

Recommended Answers

All 7 Replies

Member Avatar for iamthwee

One of the things which stands out is that you are using min without actually having initialised it to some arbitary value.

1. Practice indenting your code. You'll find it a lot easier to follow what is going on.
Eg.

//*****************************************************************************
//Name:         <Tisa Knight-Chandler>
//Assignment:   <Number 7>
//File name:    <assign7>
//Date:         <October 31, 2007>
//Description   This program shall obtain input from the keyboard and compute
//a customer's cellular phone bill based on minutes used. 
//*****************************************************************************

#include <iostream>
#include <iomanip>

using namespace std;

float overageR;
float overagePD;
float overagePN;
float overageP;

float dueR;
float due;
float dueP;

const float overMinR = 0.2;
const float overMinPD = 0.1;
const float overMinPN = 0.05;

int dayMin;
int nightMin;

int main()
{
    int acct;
    int min;
    char type;
    float dueR;

    float basePriceR = 10;
    float basePriceP = 25;

    //!! You need to input min before doing some calculations
    overageR = (min - 50) * (0.2) + 25;
    overagePD = (min - 75) * (overMinPD) + basePriceP;
    overagePN = (min - 100) * (overMinPN) + basePriceP;

    cout << setprecision(2);
    cout << "Enter account number: ";
    cin >> acct;

    cout <<
        "\nEnter service type : (r or R) for regular (p or P) for premium service: "
        << endl;
    cin >> type;

    switch (type) {
    case 'r':
    case 'R':
        cout << "\nEnter number of minutes service used: ";
        cin >> min;
        cout << "\nAccount Number: " << acct << endl;
        cout << "Service Type: Regular" << endl;
        cout << "Minutes Service Used: " << min;
        if (type == 'R' && min <= 50)
        {
            dueR = basePriceR;
        } else if (type == 'R' && min > 50) {
            dueR = overageR;
        }
        cout << setprecision(0);
        cout << "\nAmount Due: $" << dueR;
        break;
    case 'p':
    case 'P':
        cout << "\nEnter Day time minutes used: ";
        cin >> dayMin;
        cout << "\nEnter night time minutes used: ";
        cin >> nightMin;
        cout << "\nAccount Number: " << acct << endl;
        cout << "\nService Type: Premium" << endl;
        cout << "\nMinutes Service Used <Day>: " << dayMin;
        cout << "\nMinutes Service Used <Night>: " << nightMin;
        cout << "\nAmount Due: $" << due;
        break;
    default:
        cerr << "\nService Type: Invalid" << endl;
    }

    if (type == 'P' && dayMin <= 75) {
        due = basePriceP;
    } else if (type == 'P' && dayMin > 75) {
        due = overagePD;
    } else if (type == 'P' && nightMin <= 100) {
        due = basePriceP;
    } else if (type == 'P' && nightMin > 100) {
        due = overagePN;
    }

    cout << "\nEnter q to quit --> ";
    char dummy;
    cin >> dummy;               // Wait for input

    return 0;
    //main
}

Pay close attention to line 41.

2. Remove all those global variables. Delete the ones you don't use, and move the rest into main(). The 'const' values can remain where they are.

3. Whatever compiler you're using, enable as many warnings as possible.

$ g++ -W -Wall -Wshadow -ansi -pedantic -O2 foo.cpp
foo.cpp: In function `int main()':
foo.cpp:36: warning: declaration of 'dueR' shadows a global declaration
foo.cpp:20: warning: shadowed declaration is here
foo.cpp:36: warning: 'dueR' might be used uninitialized in this function

For example, dueR is declared twice in two different scopes, and may be uninitialised.

> I am new to this board and C++, so I do not even know what encase code in [] means

It means to wrap your code in [code] and [/code] tags so they are easily readable. For example:

UGLY =

if (type == 'P' && dayMin <= 75) { due = basePriceP; } else if (type == 'P' && dayMin > 75) { due = overagePD; } else if (type == 'P' && nightMin <= 100) { due = basePriceP; } else if (type == 'P' && nightMin > 100) { due = overagePN; }

PRETTY =

if (type == 'P' && dayMin <= 75) {
        due = basePriceP;
    } else if (type == 'P' && dayMin > 75) {
        due = overagePD;
    } else if (type == 'P' && nightMin <= 100) {
        due = basePriceP;
    } else if (type == 'P' && nightMin > 100) {
        due = overagePN;
    }

It means to wrap your code in [code] and [/code] tags so they are easily readable. For example:

Thanks for posting that -- I've been wondering how you posted that without using spaces between the brackets. Didn't know about the noparse tags.:)

Ok everyone,
Thanks for your input. I believe I cannot do calculations before I have input the minutes, so I need to change the place in my code for the calculations. I did not initialize the minutes to any number because I need to use what is input. I am attaching my revised code, but the outcome is still the same.

> case 'p': > case 'P': cout<<"\nEnter Day time minutes used: "; Here you handle both lower and upper case

> if (type=='P'&&dayMin<=75) And here you don't.
Type in lower case, and you get garbage printed.

But since you've already decided that type is P or p, the additional attempt is pointless, so just go with if ( dayMin <= 75 ) > I did not initialize the minutes to any number because I need to use what is input
But doing int var = 0; Then later on var = a + b; isn't going to break anything, but might tell you something if your result always keeps coming up as zero.

Good morning,
Thank you all for your help, I was able to get the program to run correctly. I appreciate the fact that I was given input and help but not the answer. I feel a great sense of achievement now!

commented: Excellent Job! +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.