程序误解了旨在填充数组中条目的输入 [英] program is misinterpreting input intended to populate entries in an array

查看:46
本文介绍了程序误解了旨在填充数组中条目的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在命令提示符下从用户一次获得3个数字,以便可以对现有数组执行点积运算?例如:

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:

我事先定义

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

现在,用户以"i,j,k"格式输入自然数.该程序退出

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]

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

推荐答案

使用for循环将用户输入存储在数组中.用户完成操作后,即可进行操作.像这样:

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;
}

我建议您像上面的代码一样使用 std :: vector .

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

这篇关于程序误解了旨在填充数组中条目的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆