-1

This is my code in it's entirety when I enter "A", then "3,2,1".

How can I get 3 numbers at once from a user in the command prompt so that I can perform a dot-product operation on it with an existing array? For example:

suppose in advance, I define

int myArray[3] = {1,2,3};

Now a user enters natural numbers in the format "i,j,k". The program spits out

myArray[0]*userArray[0] + myArray[1]*userArray[1] + myArray[2]*userArray[2]

that is,

1*a + 2*b + 3*c

I was able to do exactly this with predefined arrays, easily. However, something is going terribly wrong when I try to identify the user input as pieces of an array. The program thinks the numbers are different or in a different place, or of a different structure, resulting in negative or humongous answers.

[Part of] My program is below. The goal is the same as the example:

#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";

        //This is where the user inputs "i,j,k". I entered "3,2,1" in the command prompt.

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

        float arrnCostCup[3] = { fCostCup25,fCostCup50,fCostCup100 };
        float fStoreInput01AdotfCoH = arrnStoreInput01A[0] * arrnCostCup[0]
            + arrnStoreInput01A[1] * arrnCostCup[1]
            + arrnStoreInput01A[2] * arrnCostCup[2];
        int arrnQuantCup[3] = { 25,50,100 };

        if (fStoreInput01AdotfCoH <= fCoH){
            fCoH = fCoH - fStoreInput01AdotfCoH;
            nNumCup = nNumCup + arrnStoreInput01A[0] * arrnQuantCup[0]
                + arrnStoreInput01A[1] * arrnQuantCup[1]
                + arrnStoreInput01A[2] * arrnQuantCup[2];
        }
        else
            std::cout << "Not enough cash on hand.";

        std::cout << "you have " << nNumCup << " cups! \n";
        std::cout << "you have " << fCoH << " left in cash!";

        //Inspecting what the program thinks the user-inputed array is
        //(next lines) reveals that it is interpreting "3,2,1"
        //erroneously as 3 -858993460 -858993460

        for (auto const value : arrnStoreInput01A)
        {
            std::cout << value << ' ';
        }

    return 0;
}

I am also attaching a picture of the command prompt output because that is very illustrative and arguably easier to interpret (see top of post).

user391838
  • 61
  • 6
  • 2
    You best bet at this point are [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Jun 20 '18 at 12:01
  • Hope not. This has to be a logic error because the method works with predefined arrays (that is, if I create two arrays, pick them apart, and multiply their components as if I were doing dot-product). The program is almost certainly misinterpreting arrnStoreInput01A[0], arrnStoreInput01A[1], arrnStoreInput01A[2]. – user391838 Jun 20 '18 at 12:09
  • The program is not misinterpreting anything. You're trying to read a comma into an `int`, which puts the input stream into the failure state and then it doesn't read any more, leaving some array elements uninitialised. (You should definitely check out those C++ books.) – molbdnilo Jun 20 '18 at 12:56

2 Answers2

2

use a for loop to store the user input on the array. When the user finishes, then you do the operation. Something like this:

#include <iostream>
#include <vector>

int main()
{

    std::vector<int> userInput ;
    std::vector<int> predefinedArray{1,2,3};
    for(int i=0;i<3;i++)
    {
        int tmp;
        std::cout<< "Introduce input numer " <<i<<": " ;
        std::cin >> tmp; 
        userInput.push_back(tmp);
    }

    std::cout<<userInput[0]*predefinedArray[0]+
               userInput[1]*predefinedArray[1]+
               userInput[2]*predefinedArray[2]<<std::endl;

    return 0;
}

I would recomend you to use std::vector as I did on the above code.

Capie
  • 976
  • 1
  • 8
  • 20
  • 1
    BTW, i tried your code and it works fine. I introduced 0(press enter) 4(press enter) and 0 (press enter) and i got this output: `you have 200 cups! you have 10.04 left in cash!0 4 0 ` I think your issue is how you introduce the inputs. You should press enter after each number you input. Just typing "0,4,0" and pressing enter wont work. – Capie Jun 20 '18 at 12:20
  • I didn't realize right away that I needed to enter the amounts separately (which should have been obvious...this is a for loop). The math works for me now, too. Only problem is that the context is wrong and could be confusing because now the user is entering the amount for items indexed by numbers. How do you think we could increment alphabetical characters for each of the prompts? Still best answer. Thank you. – user391838 Jun 20 '18 at 12:54
  • It's not because of the loop. `std::cin` will only read one value at a time, even if you put `std::cin< – Capie Jun 20 '18 at 13:11
  • I see, that kind of highlights why the commas were so problematic, too. I found a good option for incrementing characters, but I will put that concern aside for now (I had each of the three items listed as A, B, and C so the user can say they want "3 of A, 2 of B and 1 of C", etc). – user391838 Jun 20 '18 at 13:33
2

Have a look at how operator>> works. It reads integers up to the comma (or anything not an integer). You will have to remove the comma from the input stream to get the next number.

You could use ignore:

std::cin >> arrnStoreInput01A[0];
std::cin.ignore(1,',');
std::cin >> arrnStoreInput01A[1];
std::cin.ignore(1,',');
std::cin >> arrnStoreInput01A[2];
Loebl
  • 1,381
  • 11
  • 21
  • This worked perfectly but I just picked the other one before you posted yours! Both good solutions, this one easiest to implement – user391838 Jun 20 '18 at 13:08