简体   繁体   中英

Weird Results in C++

I have good experience in Python and very little in C++. I'm trying to write a program to print each element of the 'sales' array:

#include <iostream>
#include <iomanip>

using namespace std;
void printArray(int, int);

int main()
{
    char chips[5][50] = {"mild", "medium", "sweet", "hot", "zesty"};
    int sales[5][6] = {0};

    int tempSales, counter;
    const int i = 5;

    for (counter = 0; counter < i; counter++)
    {
        cout << "Please enter in the sales for " << chips[counter] << ": ";
        cin >> tempSales;

        tempSales >> sales[counter][5];
    }

    cout << "{";
    for (int counter = 0; counter < i; counter++)
    {
        cout << chips[counter] << ", ";
    }
    cout << "}" << endl;

    cout << "{";
    for (int counter = 0; counter < i; counter++)
    {
        cout << sales[counter] << ", ";
    }
    cout << "}" << endl;

    return 0;
}

To solve this problem, I need to have the same commands and keywords I still have, and it can't be any advanced or weird syntax. What's going on? I'm getting results like:

{mild, medium, sweet, hot, zesty, }
{010FF6F4, 010FF70C, 010FF724, 010FF73C, 010FF754, }

Whereas I just want to see 1, 2, 3, 4, and 5 for the second array. Please help!

You are using a two dimensional array with sales[][] but then try to read from it like from a one dimensional one. Instead just declare it with int sales[5]{} and save your input with

tempSales >> sales[counter];

Also the assignment into chips and sales won't repeat the initialization pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM