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

In summary, a boolean variable is a type of variable in programming that can only have two values: true or false. It is commonly used in decision-making statements and can be declared using the keyword "boolean." Some common operations that can be performed on boolean variables include logical operators like AND, OR, and NOT. It is important to note that a boolean variable can only have the values of true or false, and any other value will result in an error.
  • #1
Mustard
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:
Physics news on Phys.org
  • #2
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
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

1. What is a boolean variable?

A boolean variable is a data type in programming that can only have two possible values: true or false. It is often used to represent logical values in a program.

2. How do I declare a boolean variable in my program?

To declare a boolean variable in your program, you can use the keyword "boolean" followed by the variable name and an assignment operator. For example: boolean isRaining = true;

3. How do I use a boolean variable in an if statement?

In an if statement, you can use a boolean variable as the condition. If the variable is true, the code inside the if statement will be executed. If the variable is false, the code will be skipped. For example: if (isRaining) { // do something }

4. Can a boolean variable be used in mathematical operations?

No, a boolean variable cannot be used in mathematical operations because it only has two possible values: true or false. It is used to represent logical values, not numerical values.

5. How do I change the value of a boolean variable in my program?

To change the value of a boolean variable, you can use the assignment operator (=) to assign a new value. For example: isRaining = false; This will change the value of the variable from true to false.

Back
Top