Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

c++ - program is misinterpreting input intended to populate entries in an array

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

        //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! 
";
        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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...