在基于数组的程序中获取最后一个字符串或最后一个价格后程序 crashes/stops

Program crashes/stops after getting the last string or the last price in array based program

该程序允许您通过订单获取产品名称和价格,数据存储在数组中,我使用for循环输入和输出数组元素,但是当用户输入最后一个产品名称或价格时程序停止工作。是编译器限制还是代码方面的问题,我正在等待您的回答。我在 Code::Blocks 和 DevC++ IDE 中对其进行了测试。 在下面的 link 中,您有代码本身和程序的图像 运行。 [https://drive.google.com/file/d/17vWo0KY5xVqAPK3tsgbo0X-wOTc_FjQr/view?usp=sharing]

The image

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int i , n  , j;
    cout<<"Input the number of the products: "<<endl;
    cin>>n;
    string Product [n];
    double Price[n];
    for(i=1;i<=n;i++){

cout<< "Input the name of product"<<i<<" :"<<endl;
        cin>>Product[i];
        cout<<"Input the price of product "<<i<<" :"<<endl;
        cin>>Price[i];

    }

    for(j=1;j<=n;j++){
        cout<< "Product "<<Product[i]<<" costs: "<< Price[i]<<" $."<< endl;



}
return 0;
}

数组索引从0开始到n-1。在这里,当您进行输入时,您试图将价格和产品的值存储在第 n 个索引处,这是不可能的。因此,而不是从 0 开始 for 循环,而不是存储在 i-1 索引中的第 ith 个索引中。

初始循环:

`for(i=1;i<=n;i++){ /* some code*/ }

for(j=1;j<=n;j++){ /* some code*/ }`

已更新 for 循环从 0 开始:

for(i=0; i<n; i++){ /* some code*/ }

for(j=0; j<n; j++){ cout<< "Product "<<Product[j]<<" costs: "<< Price[j]<<" $."<< endl; }

还有其他用途:

for(i=1;i<=n;i++){<br> cout<< "Input the name of product"<<i<<" :"<<endl; cin>>Product[i-1]; cout<<"Input the price of product "<<i<<" :"<<endl; cin>>Price[i-1]; }

for(j=1;j<=n;j++){ cout<< "Product "<<Product[j-1]<<" costs: "<< Price[j-1]<<" $."<< endl; }