(i = 0; i <=年; i ++)在此范围内未声明“ i”

时间:2018-09-27 03:03:04

标签: c++ loops

出于某种原因,myprogramminglab一直在说我没有声明'i',我也不明白为什么。

 #include <iostream>

     using namespace std;

     int main()
 {
     int years;
     double cost, inflaRate;

    cout<<"Enter the current price of pencils:";
    cin >> cost;
    cout<<"Enter the number of years in the future that 
        you will buy the pencil:";
    cin >> years;

    cout<<"Enter the inflation rate as a percentage." << 
endl;
    cin >> inflaRate;

   inflaRate /= 1;

    cout << "The price of pencils will be " << cost;

   for (i = 0; i <= years; i++) //Keeps telling me I have not declared 'i' here
 {
 cost += (cost*inflaRate);
 }
    cout << cost << "in" << years << "years." << endl;

    system("pause");
    return 0;
 }

1 个答案:

答案 0 :(得分:1)

  

由于某种原因...

那是因为您还没有实际声明了i。您可以通过对for循环进行简单的更改来解决此问题:

for (int i = 0; i <= years; i++) // No longer complains you have not declared 'i' :-)
//   ^^^
//  Declare it!