Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序误解了用于填充数组中的条目的输入_C++_Arrays - Fatal编程技术网

C++ 程序误解了用于填充数组中的条目的输入

C++ 程序误解了用于填充数组中的条目的输入,c++,arrays,C++,Arrays,如何在命令提示符下一次从用户处获取3个数字,以便使用现有阵列对其执行点积操作?例如: 预先假设,我定义 int myArray[3] = {1,2,3}; 现在,用户以“i,j,k”格式输入自然数。 节目播出了 myArray[0]*userArray[0] + myArray[1]*userArray[1] + myArray[2]*userArray[2] 就是 1*a + 2*b + 3*c 我能够用预定义的数组轻松地做到这一点。然而,当我试图将用户输入识别为数组的一部分时,出现了严

如何在命令提示符下一次从用户处获取3个数字,以便使用现有阵列对其执行点积操作?例如:

预先假设,我定义

int myArray[3] = {1,2,3};
现在,用户以“i,j,k”格式输入自然数。 节目播出了

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

1*a + 2*b + 3*c
我能够用预定义的数组轻松地做到这一点。然而,当我试图将用户输入识别为数组的一部分时,出现了严重的错误。该程序认为数字不同,或者在不同的地方,或者结构不同,结果是否定的或冗长的答案

下面是我节目的一部分。目标与示例相同:

#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;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
int nNumCup=0,nNumLem=0,nNumSug=0,nNumIce=0;
浮子fCoH=20.00,fCostCup25=1.99,fCostCup50=2.49,fCostCup100=2.99;
int arrnStoreInput01A[3];

std::cout使用for循环将用户输入存储在数组中。当用户完成后,您执行该操作。类似如下:

#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;
}
#包括
#包括
int main()
{
向量用户输入;
std::向量预定义数组{1,2,3};

对于(int i=0;i请看一下
运算符>>
的工作原理。它可以读取整数到逗号(或任何非整数的内容)。您必须从输入流中删除逗号才能获得下一个数字

您可以使用:


在这一点上,您最好的选择是。希望不是。这一定是一个逻辑错误,因为该方法适用于预定义的数组(即,如果我创建两个数组,将它们分开,然后像做点积一样将它们的组件相乘)。该程序几乎肯定会误解arrnStoreInput01A[0],arrnStoreInput01A[1],arrnStoreInput01A[2]这个程序不会误解任何东西。你试图把一个逗号读入一个<代码> int /Cube >,它将输入流放入失败状态,然后它不再读取,留下一些数组元素未初始化。(你应该绝对地检查那些C++书籍。)BTW,我尝试了你的代码,它工作得很好。我介绍了0(按下回车)。4(按enter键)和0(按enter键)我得到了这个输出:
你有200个杯子!你还有10.04个现金!0 4 0
我想你的问题是如何引入输入。你应该在输入的每个数字后按enter键输入。只需输入“0,4,0”并按enter键不起作用。我没有立即意识到我需要单独输入金额(这应该很明显……这是一个for循环)。数学现在也适用于我。唯一的问题是上下文是错误的,可能会令人困惑,因为现在用户正在输入按数字索引的项目的数量。您认为我们如何为每个提示增加字母字符数?仍然是最好的答案。谢谢。这不是因为循环。
std::cin
每次读取一个值,即使你把
std::cinI看,这也突出说明了逗号为什么会有如此大的问题。我找到了一个增加字符的好方法,但我现在将把这个问题放在一边(我把三个项目分别列为a、B和C,这样用户就可以说他们想要“a中的3、B中的2和C中的1”,等等).这个很好用,但我只是在你发布你的之前选择了另一个!这两个解决方案都很好,这一个最容易实现
std::cin >> arrnStoreInput01A[0];
std::cin.ignore(1,',');
std::cin >> arrnStoreInput01A[1];
std::cin.ignore(1,',');
std::cin >> arrnStoreInput01A[2];