Comp Sci How Does a Boolean Variable Impact Program Functionality in C++?

  • Thread starter Thread starter Mustard
  • Start date Start date
  • Tags Tags
    Program Variable
AI Thread Summary
The discussion highlights the significance of a Boolean variable in C++ programming, particularly in the context of a computer cost calculation program. A critical error identified is the misuse of the assignment operator '=' instead of the equality operator '==' in the conditional statement checking the touchscreen variable, which leads to incorrect functionality. The program structure includes functions for gathering user input, error checking, calculating the total cost, and printing the receipt. Proper handling of Boolean values is emphasized to ensure accurate program behavior. The conversation underscores the importance of understanding operator usage to avoid common programming mistakes.
Mustard
Messages
21
Reaction score
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:
Physics news on Phys.org
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
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

Similar threads

Replies
23
Views
3K
Replies
3
Views
2K
Replies
3
Views
1K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
11
Views
3K
Replies
1
Views
28K
Back
Top