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;
    
}

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 same.so 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;
      }

Edited 6 Years Ago by johny_mplayer: and the rest is

@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.

@johny_mplayer:
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.

Edited 6 Years Ago by Fbody: n/a

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!

This article has been dead for over six months. Start a new discussion instead.