-2

EDIT: First time I did this I asked too many questions. Now I am not providing enough detail. Simply c-outing an array is not helping because I am trying to do arithmetic with the array and it is misinterpreting data. It might help in figuring out what the problem is, so I am marking it as the correct answer and making a new post. I don't know what else to do, I am clearly not getting the big picture through to readers.

I need a user-defined string to be interpreted by the program as an array. I tried to implement advice from user463035818 but I don't think it's coming out correctly.

The array I am talking about is initialized as arrnStoreInput01A at the top of this portion of the program (which I have cropped so it's easier to read). I instruct the user to enter three numbers in the format "i, j, k" (I will change to "i j k" if that is better--I have tested both). The way things are now, when I ask the program to spit the result back at me (4th line from the bottom), it spits out a hexidecimal instead of {i,j,k} whether or not I use commas (result is different hex, though).

What happened? How can I fix this so I have a 1x3 array of integers that I can perform arithmetic operations on, i.e. 3*arrnStoreInput01A? Thank you.

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <limits>
#include <tuple>

int main()
{
    int nNumCup = 0, nNumLem = 0, nNumSug = 0, nNumIce = 0;
    float fCoH = 20.00, fCostCup25 = 1.99, fCostCup50 = 2.49, fCostCup100 = 2.99;

        int arrnStoreInput01A[3];
        std::cout << "Go to Cups \n \n";
        std::cout << "Cups are availible in packs of 25, 50 and 100. \n"
            "Please enter three numbers in \"i,j,k\" format for the \n"
            "respective amounts of each of the following three products \n"
            "you want to buy: \n \n"
            "A) 25-pack of cups for " << fCostCup25 << "\n"
            "B) 50-pack of cups for " << fCostCup50 << "\n"
            "C) 100-pack of cups for " << fCostCup100 << "\n \n"
            "For example, type \"0,4,0\" to purchase 4 packages of 50 cups or \n"
            "type \"3,2,1\" to buy 3 packages of 25 cups, 2 packages of 50 cups \n"
            "and 1 package of 100 cups. \n \n";

        std::cin >> arrnStoreInput01A[0] >> arrnStoreInput01A[1] >> arrnStoreInput01A[2];

        float arrnCostCup[3] = { fCostCup25,fCostCup50,fCostCup100 };
        int arrnQuantCup[3] = { 25,50,100 };
        std::cout << arrnStoreInput01A;

    return 0;
}
user391838
  • 61
  • 6
  • 2
    Didn't I see a similar question a few hours ago? – DeiDei Jun 20 '18 at 09:33
  • people complained it wasn't clear so I deleted it. This question is clear...and a different question. – user391838 Jun 20 '18 at 09:36
  • Possible duplicate of [Cout a whole array in c++](https://stackoverflow.com/questions/33439064/cout-a-whole-array-in-c) – PhilMasteG Jun 20 '18 at 10:43
  • 1
    Possible duplicate of [program is misinterpreting input intended to populate entries in an array](https://stackoverflow.com/questions/50947733/program-is-misinterpreting-input-intended-to-populate-entries-in-an-array) – Capie Jun 20 '18 at 12:37

1 Answers1

3

The variable arrnStoreInput01A is an array. There is no operator<< overload which can be used to print an array.

Instead what is happening is that arrnStoreInput01A decays to a pointer to its first element, and that pointer is printed.

So when you do

std::cout << arrnStoreInput01A;

what is really happening is

std::cout << &arrnStoreInput01A[0];

If you want to print all elements of an array, you have to explicitly iterate over it, printing each element.

For example

for (auto const value : arrnStoreInput01A)
{
    std::cout << value << ' ';
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    ... or wrap it in an object constructible from an array which _does_ have an `operator<<` defined. – einpoklum Jun 20 '18 at 09:36
  • There is a problem. WhenI input "3,2,1" and use your code to spit out the array, I get 3 -858993460 -858993460 Something is wrong with the way I am indexing, can any of you see it? – user391838 Jun 20 '18 at 11:11