I am supposed to be writing a program that will calculate sales in a week. Every week 200.00 dollars is earned, then sales is calculated on top of the 200.00 (i.e., 5000.00 in sales, would be 200 + .09 * 5000.00). My code is below, but I am having problems with the calculations. When I run the program it continues to give me 200.00 dollars for the week. Not sure how to approach this.

// this program will calculate the input of each salesperson's gross sales 
// from the last week and calculate and display that salesperson's earnings
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double ttlsales, weeklyearnings;
    
    ttlsales = 0;
    weeklyearnings = 0;
    
    cout << "Enter the total sales for the week, (-1 will end program): ";
    cin >> ttlsales;
    
    while (ttlsales >= 1)
    {
          ttlsales = (.09 * weeklyearnings) + 200;
          
          cout << "Total earnings are: " << setprecision (2)
               << fixed << ttlsales << endl;
          
          cout << "Enter the next sales person's total sales for the week,\n"
          "(-1 will end program): ";
          cin >> ttlsales;
    }
    
    cout << "The program will now end." << endl;
    
    return 0;
    
}

Dani AI

Generated

As and pointed out, the root cause in situations like this is almost always a mixup between the value you read from input and the value you compute. The loop must read a fresh sales amount each iteration and then compute earnings from that fresh value. Trying to “reset” a variable with a decrement or reusing the computed-value slot for input will produce the same stale result repeatedly.

Quick, practical checklist to debug this kind of loop:

  • Give distinct names to the two concepts: one variable for the raw input (sales) and a separate one for the computed pay.
  • After reading input, immediately check for input failure (bad type or EOF) and for the sentinel value that ends the loop.
  • Compute earnings only from the input variable; then print the result. Do not read the next input into the computed-value variable.
  • Use simple prints or an IDE/watchpoint to observe the input and computed values each iteration — stepping through one loop is often all it takes to spot the bug.

A compact, robust loop pattern (conceptual) is:

read input into sales_variable
while (sentinel not received and input is valid) {
  compute earnings = base + rate * sales_variable
  print earnings
  read sales_variable for next iteration
}

Extra notes: avoid using decrement/increment operators to “reset” values — they change the number but don’t correct a logic error about where input is stored. Check and clear stream state with cin.clear()/cin.ignore() on bad input. Prefer a sentinel that can’t be confused with valid data (e.g., a negative number if sales are never negative). For reference on reading input and loop semantics see the C++ reference pages for input extraction and while loops: std::basic_istream::operator>> and while statement.

Recommended Answers

All 4 Replies

the fault comes from line 13.the weeklyearnings are 0.so line 20 will always give 200.
change in line 16 ttlsales to weeklyearnings and in line 18 the far it works.

// this program will calculate the input of each salesperson's gross sales
      // from the last week and calculate and display that salesperson's earnings
      #include <iostream>
      #include <iomanip>
      using namespace std;
      int main()
      {
      double ttlsales, weeklyearnings;
      ttlsales = 0;
      weeklyearnings = 0;
      cout << "Enter the total sales for the week, (-1 will end program): ";
      cin >> weeklyearnings;
      while (weeklyearnings >= 1)
      {
      ttlsales = (.09 * weeklyearnings) + 200;
      cout << "Total earnings are: " << setprecision (2)
      << fixed << ttlsales << endl;
      cout << "Enter the next sales person's total sales for the week,\n"
      "(-1 will end program): ";
      cin >> weeklyearnings;
      }
      cout << "The program will now end." << endl;
      return 0;
      }

@OP:
As johny mentioned, you are using weeklyearnings when you haven't placed any useful value in it yet. As a result, line 20: ttlsales = (.09 * weeklyearnings) + 200; always generates (0.0 + 200). You need to correct your variable use.

:
Please read this. It's okay to post bits of code and make recommendations, but giving someone something to cut/paste is generally frowned upon. When you do this, you're doing their work for them and hindering their learning process.

Okay, so I finally fixed those problems. I know have a new issue. The program will run, but when I try and run it in the loop, it will not recalculate for the weeklyearnings variable. It will keep the old answer stored, and continually output the same answer. I have tried to put in, --weeklyearnings, to reset that variable, but I'm not having any luck with that. Any suggestions?

// this program will calculate the input of each salesperson's gross sales 
// from the last week and calculate and display that salesperson's earnings
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double ttlsales = 0, weeklyearnings;
    
    cout << "Enter the total sales for the week, (-1 will end program): ";
    cin >> ttlsales;
    
    while (ttlsales >= 1)
    {
          weeklyearnings = (.09 * ttlsales) + 200;
          
          cout << "Total earnings are: " << setprecision (2)
               << fixed << weeklyearnings << endl;
          
          cout << "Enter the next sales person's total sales for the week,\n"
          "(-1 will end program): ";
          cin >> weeklyearnings;
          
    }
    
    cout << "The program will now end." << endl;
    
    system ("pause");
    
    return 0;
    
}

Okay, I finally fixed all the problems. I made a few errors in my code. Thanks for all of your help!

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.