首页 文章

C赋值简介:并行数组

提问于
浏览
-1

我是一名初学者,正在为我教授指定的课程寻求帮助 . 任务是使用2个并行循环来询问用户5种不同的莎莎酱及其成本 . 然后打印出名称,总销售量,平均值,畅销书和最差销售量 . 问题是,我可以计算最好和最差的销售,但这些是整数 . 我不明白我怎么能拉字符串而不是int?任何帮助都会受到欢迎!#include

#include <string>
#include <cmath>
#include <cstring>
using namespace std;

int main() {
    string name[5];
    int total[5];
    int i, final, counter, high, low;

    for(i=0; i<=4; i++){
        cout << "Salsa name: ";
        cin >> name[i];
        cout << "How many of " << name[i] << " were sold? ";
        cin >> total[i];
        final += total[i];

        if(i < 0) {
            "Sorry, you cannot have negative sales!";
            return 0;
        } else {
            if(counter == 0) {
                low = total[i];
            } else if (total[i] < low) {
                low = total[i];
            } else if (total[i] > high) {
                high = total[i];
            } counter++;
        }
    }

        cout << "Name          Amount Sold\n"
             << "-------------------------\n";
        for(int i = 0; i <= 4; i++){
            cout << name[i] << "        " << total[i] << endl;
        }


    cout << "Total sold: " << final << endl
         << "Most Sold: " << high << endl
         << "Least Sold: " << low;
    return 0;
}

输出:

Running /home/ubuntu/workspace/Ch6_Ex3.cpp
Salsa name: salsa1
How many of salsa1 were sold? 10
Salsa name: salsa2
How many of salsa2 were sold? 20
Salsa name: salsa3
How many of salsa3 were sold? 30
Salsa name: salsa4
How many of salsa4 were sold? 40
Salsa name: salsa5
How many of salsa5 were sold? 50
Name          Amount Sold
-------------------------
salsa1        10
salsa2        20
salsa3        30
salsa4        40
salsa5        50
Total sold: 32862
Most Sold: 50
Least Sold: -547659664

Process exited with code: 0

1 回答

  • 1

    你的代码有一些小问题:

    • 变量 counter 中没有任何意义,因为你的意思是它与 i 具有相同的值,所以使用 i ;你也没有初始化它 . 比较 counter == 0 是未定义的行为 .

    • 而不是跟踪 total 中的最高/最低值,而应该跟踪这些特定值的索引,因为 names 中的相应值是每个莎莎品牌的名称......

    请注意以下更改:

    #include <iostream>
    #include <string>
    #include <cmath>
    #include <cstring>
    using namespace std;
    
    int main() {
        string name[5];
        int total[5];
    
        int i;
        int final = 0;
    
        int high, low;
        high = 0, low = 0;
    
        for(i=0; i<=4; i++){
            cout << "Salsa name: ";
            cin >> name[i];
            cout << "How many of " << name[i] << " were sold? ";
            cin >> total[i];
            final += total[i];
    
            // if(i < 0) {
            //     "Sorry, you cannot have negative sales!";
            //     return 0;
            // }
            // i is never going to be less than 0.
    
            if (total[high] < total[i])
                high = i;
            if (total[low] > total[i])
                low = i;
        }
    
        // ...
    
        cout << "Most-sold salsa: " << name[high]
             << "\nLeast-sold: " << name[low] << "\n";
    
        return 0;
    }
    

    其他说明:

    • 我强烈建议反对 using namespace std; See why

    • 你最初忘了 #include <iostream>

相关问题