Help on boolean variable in my program

  • Comp Sci
  • Thread starter Mustard
  • Start date
  • #1
21
1
Homework Statement:
I can not seem to get the correct calculations for when the user enters no for touchscreen--it always adds 60 to final calculation no matter what. any help will be appreciated.
Relevant Equations:
Source code in the bottom.
C:
#include <iostream>
#include <string>
#include<iomanip>
//prototypes
double gettingInfo(double&, double&, double&, bool&, char&);
double errorChecking(double&);
double calculatingPrice(double&, double&, double&, bool&);
double printingReciept(double&, double&, double&,double&);
using namespace std;

int main()
{
    cout<<"This program calculates the cost of a customized computer--given specifications.\n\n";
    double speedOfProcessor;
    double storageCapacity;
    double ramAmount;
    double total;
    bool touchscreen;
    char touchscreenCheck;
    string yesOrNo;
   
    gettingInfo(speedOfProcessor, storageCapacity, ramAmount, touchscreen, touchscreenCheck);
    total=calculatingPrice(speedOfProcessor,storageCapacity, ramAmount,touchscreen);
    printingReciept(speedOfProcessor, storageCapacity, ramAmount, total);
    
    return 0;
}
double gettingInfo(double& processor, double& storage, double& ram, bool& touchscreen, char& touchscreenCheck)
{
    
    cout<<"Enter processor speed: ";
    cin>>processor;
    errorChecking(processor);
    cout<<"Enter storage amount: ";
    cin>>storage;
    errorChecking(storage);
    cout<<"Enter RAM amount: ";
    cin>>ram;
    errorChecking(ram);
    cout<<"Is your computer touchscreen(Y/N)? ";
    cin>>touchscreenCheck;
    if(touchscreenCheck=='Y'|| touchscreenCheck=='y')
        touchscreen=true;
    else
        touchscreen=false;
}
double errorChecking(double& input)
{
    while (input<=0){
        cout<<"Please enter a number greater than zero: ";
        cin>>input;
    }
}
double calculatingPrice(double& processor, double& storage, double& ram, bool& touchscreen){
    
    const double PC_BASE_COST=150;
    const double PROCESSOR_SPEED_MAX=2;
    const double XTRA_PROCESSOR_SPEED=50;
    const double STORAGE_MAX=500;
    const double XTRA_STORAGE=45;
    const double RAM_MAX=4;
    const double XTRA_RAM=5;
    const double MARKUP=0.75;
    const double TOUCHSCREEN_PRICE=60;
    double extraProc;
    double xtraRam;
    double processorCost;
    double storageCost;
    double ramCost;
    double cost;
    double total;
    double markupTotal;
    double touchscreenCost;
    
    
    if (processor>PROCESSOR_SPEED_MAX){
        extraProc=processor-PROCESSOR_SPEED_MAX;
        processorCost=extraProc*XTRA_PROCESSOR_SPEED;
    }
    else{
        processorCost=0;
    }
    
    if (storage>=STORAGE_MAX){
        storageCost=XTRA_STORAGE;
    }
    else{
        storageCost=0;
    }
    if (ram>RAM_MAX){
        xtraRam=ram-RAM_MAX;
        ramCost=xtraRam*XTRA_RAM;
    }
    else{
        ramCost=0;
    }

    if (touchscreen=1)
        touchscreenCost=TOUCHSCREEN_PRICE;
    else
        touchscreenCost=0;
        
    cout<<touchscreenCost;
        
    cost=processorCost+storageCost+ramCost+touchscreenCost+PC_BASE_COST;
    markupTotal=cost*MARKUP;
    return total=cost+markupTotal;
    
}
double printingReciept(double& processor, double& storage, double& ram,double& total)
{
    cout<<endl;
    cout<<"-------------------------------------------"<<endl;
    cout<<"Processor Speed:\t"<<processor<<" GHz"<<endl;
    cout<<"Storage Capacity:\t"<<storage<<" GB"<<endl;
    cout<<"RAM Amount:\t\t"<<ram<<" GB"<<endl;
    cout<<"Touchscreen:\t\t"<<endl;
    cout<<"Total Cost:\t\t$"<<fixed<<setprecision(2)<<total<<endl;
}
    cout<<"Touchscreen:\t\t"<<endl;
    cout<<"Total Cost:\t\t$"<<fixed<<setprecision(2)<<total<<endl;
}
 
Last edited by a moderator:

Answers and Replies

  • #2
2,051
317
if (touchscreen=1)
Turn compiler warnings on and don't ignore them.

Could you also use the </> insert code button, and then paste your code between the tags? that will keep the indentation.

[Mentor Note -- code tags added to the OP's post]
 
Last edited by a moderator:
  • Like
Likes FactChecker
  • #3
FactChecker
Science Advisor
Gold Member
6,637
2,686
Line 98: "if (touchscreen=1)" is at least one problem. This is one of the most common errors in all of computer programming. It sets the value of touchscreen to 1 and is always true. Use "==" instead of "=".
 
  • Like
Likes sysprog and berkeman

Related Threads on Help on boolean variable in my program

  • Last Post
Replies
9
Views
1K
  • Last Post
Replies
1
Views
5K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
1
Views
19K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
2
Views
1K
Replies
15
Views
640
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
1
Views
1K
Top