你好! C ++编程和初学者我需要帮助

时间:2011-03-31 12:24:19

标签: c++

When I build the solution

好!我真的需要你的帮助或一些提示 我需要一个程序,我会收到我前几年薪水的加薪。我需要计算并显示未来三年的年度加薪量。我想使用3%4%5%和6%的费率。

这是我到目前为止所做的,但它不起作用

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

int main()
{
    int beginSalary  = 0;
    double newSalary = 0.0;
    double raise     = 0.0;
    double theRate = 0.0;

    cout << "Beginning Salary (negative number or 0 to end): ";
    cin >> beginSalary;

 do 
 {

        // 3 percent
        newSalary = (beginSalary+(beginSalary*3/100)); 
        raise = newSalary-beginSalary; 
        cout << raise << endl; 
        cout << endl;


        // 4 percent
        newSalary = (beginSalary+(beginSalary*4/100)); 
        raise = newSalary-beginSalary; 
        cout << raise << endl;


        // 5 percent
        newSalary = (beginSalary+(beginSalary*5/100)); 
        raise = newSalary-beginSalary;  
        cout << raise << endl; 
        cout << endl;

        // 6 percent
        newSalary = (beginSalary+(beginSalary*6/100)); 
        raise = newSalary-beginSalary; 
        cout << raise << endl; 
        cout << endl;

    } while ( newSalary != 0);

    return 0;
}   //end of main function

3 个答案:

答案 0 :(得分:0)

cin&gt;&gt; ... 应该在循环内。或者你有一个无限循环 或者不同的循环条件会更好

答案 1 :(得分:0)

问题出在输入请求中:

你必须在循环中移动CIN指令。 循环只重复块{}内的部分。

while条件也需要改变,因为!= 0 你不会再问负值(如cout句中所述)

   cout << "Beginning Salary (negative number or 0 to end): ";

 do 
 {
    cin >> beginSalary;

//A LOT OF CODE
} while ( newSalary <= 0);

答案 2 :(得分:0)

你想要做的是:

int beginSalary;
do {
    cout << "Beginning Salary (negative number or 0 to end): ";
    cin >> beginSalary;
    if (beginSalary <= 0) break;

    for (int percent = 3; percent <= 6; percent++)
    {
        cout << endl << "Raise for " << percent << "% for the next three years: " << endl;
        double salary = beginSalary;
        for (int year = 1; year <= 3; year++)
        {
            double raise = salary * percent / 100.0;
            salary += raise;
            cout << "Year " << year << ": " << raise << ". ";
        }
    }
} while (1);