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).