-2

this is the code:

#include <iomanip>
#include <iostream>
using namespace std;

int main() {
  const double PERC19 = 0.2;
  const double PERC49 = 0.3;
  const double PERC99 = 0.4;
  const double PERC100 = 0.5;
  const double Price = 99.00;
  double totalCost, originalAmount, discountAmount, dRate;
  int numberofSold;

  cout << "Enter the number of units sold\n";
  cin >> numberofSold;

  // Input Validation
  if (numberofSold < 0) {
    cout << "Input should be a positive number\n";
    exit(0);
  }

  originalAmount = numberofSold * Price;
  
  if(numberofSold >= 10 && numberofSold <= 19){
    cout << "test";
    discountAmount = originalAmount * PERC19;
    totalCost = originalAmount - discountAmount;
  } 

  if(numberofSold >= 20 && numberofSold <= 49){
    cout << "test";
    discountAmount = originalAmount * PERC49;
    totalCost = originalAmount - discountAmount;

    
  } 

  if(numberofSold >= 50 && numberofSold <= 99){
    cout << "test";
    discountAmount = originalAmount * PERC99;
    totalCost = originalAmount - discountAmount;

    
  } 

  if(numberofSold >= 100){
    cout << "test";
    discountAmount = originalAmount * PERC100;
    totalCost = originalAmount - discountAmount;

    
  } 

  cout << totalCost;

    }

this is it's output:

PS C:\C_programs\first assing> .\asign.exe
Enter the number of units sold
5
3.95253e-323
PS C:\C_programs\first assing>

when given an input of 5, why would this be outputted? 5 does not qualify for any of the if statements. totalCost should be null, but instead it does 3.95253e-323 instead when run on my computer.

on replit.com, it outputs 395 instead, which is the calculation for the first if statement (not the input val one), dispite not actually running the statement.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • None of your if statements get entered and you end up outputting an uninitialised value. – Mike Vine Sep 09 '22 at 08:16
  • because you writing out totalCost in anyway at the end. So when it didnt change it assign a random number and writing it to console – Yunus Temurlenk Sep 09 '22 at 08:16
  • `null` is not a number and so `totalCost` cannot have that value. If you want `totalCost` to have a particular value then you must give it that value (in all cases). If you want there to be no output when the input is `5` then you must arrange your code so that you don't output anything when the input is `5`. – john Sep 09 '22 at 08:21
  • When you write `double totalCost`, you are asking the machine to reserve some space in memory to store a double. When writing `double totalCost = 0`, you reserve the space AND store 0 in this location. With the first, nothing specific is stored in `totalCost`. The value stored is thus _uninitialized_, and plotting it is `undefined behavior` (often abbreviated UB). For more info about UB: https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior. In you specific case, depending on your compile options `totalCost` could be 0, MIN_DBL, random value... – GabrielGodefroy Sep 09 '22 at 08:31

1 Answers1

0

You have not initialized the variable totalCost to anything. Reading an uninitialized variable is undefined behavior (anything can happen). In your case it so happens to return the value 3.95253e-323 which you then output in the last line. The fix is to initialize it with:

double totalCost = 0;

You can also ensure you set it by ensuring one of your if statements apply.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • sorry, what does it do when the value is undefined? I don't know what you mean by " it int he last line" – Ameer Khan Sep 09 '22 at 08:18
  • It meas it can have any (random) value, and read such a variable is undefined behavior (anything can happen). – Allan Wind Sep 09 '22 at 08:18
  • @AmeerKhan Undefined behavior means anything can happen including but not limited to the program giving your expected output. But never rely on the output of a program that has undefined behavior. The program may just crash. – Jason Sep 09 '22 at 08:19
  • Thanks. I knew it was because it was undefined. I just didn't know why it would do what it did instead of throwing an error. – Ameer Khan Sep 09 '22 at 08:23