【问题标题】:My array takes random values despite user input尽管用户输入,我的数组仍采用随机值
【发布时间】:2013-11-05 05:03:30
【问题描述】:

再次打扰,我不是编程天才。

首先,总结一下:数组输入。两个 3D 向量……哈哈,让我们用向量来计算 MORE 向量。无论如何:点积是荒谬的(小数点前九个数字;我的意思是,说真的,我从没想过 1x8+7x5+4x2 可以有九个数字)。交叉乘积……甚至更糟。

我做了……嗯……我怎么称呼它?好吧,我们称之为“traza”。为了便于理解,我将翻译一个定义:代码的“traza”告诉我们其执行的指令顺序,以及在每一行代码之后变量如何变化。你知道,带有变量和数字标记的表格指的是代码行,如果代码做了一些意想不到的事情,我们会在这些行中查看。切入正题:就我所见,一切都很好。

然后,我使用打印命令和向量中的每个值制作了一个意外的“pseudotraza”。就在输入之后和点积之前(在函数中)。你猜怎么着: 1)这不是我的输入,在他们中的任何一个中 2)它们甚至不是相同的值 3) 第一个值与我的输入相距甚远,但以下值至少更具逻辑性(与输入的差异较小)。

我在 12 小时前的今天早上学会了使用数组/向量/任何东西。我从来不需要在输入之前将任何值设置为 0 作为默认值。但这是我之前知道的唯一一件事情发生在我身上的时候。 (总有一天你们中的任何一个人都会成为我的编程老师,你们比他自己学得更多……请原谅我糟糕的语法,西班牙的英语教学只是“在上高中一年学习一些语法规则和不超过 50 个练习。 ..这就是你通过大学入学考试所需要的一切!”)

#include <iostream>
using namespace std;
#include <stdlib.h>

const int N = 3;
typedef int Vector[N];

void introduirVector (Vector);
float producteEscalar (const Vector, const Vector); /*Input vector and dot product*/

int main (void)
{
 Vector v1, v2;
 float p_esc;

 cout << "Introduim les dades del vector A: "; /* Input */
 introduirVector (v1);

 cout << "Introduim les dades del vector B: "; /* 2x Input combo */
 introduirVector (v2);

 cout << v1[0] << "\n" << v1[1] << "\n" << v1[2] << "\n" << v2[0] << "\n" << v2[1] << "\n" << v2[2] << endl; /* "Puseudotraza*/

 p_esc= producteEscalar (v1, v2); /*Dot product*/

 cout << "El producte escalar: " << p_esc; /*Dot product is...*/

 system ("PAUSE");
 return 0;
}

/*3x Input combo*/
void introduirVector (Vector)
{
    int i;
    Vector v;

    for (i = 0; i < N; i++)
    {
     cin >> v[i];
    }

    return;
}

 /* Dot product (why all the Vectors are set as constants but another after this is not set? It's the hint given by the teacher, my (not) beloved teacher...they gave us the main function and the other function's prototypes, but that's all) */
float producteEscalar (const Vector, const Vector)
{
   float escalar;
   Vector v1, v2;
 /* Pseudotrazas for all*/
   cout << v1[0] << "\n" << v1[1] << "\n" << v1[2] << "\n" << v2[0] << "\n" << v2[1] << "\n" << v2[2] << endl;

/* Dot product and all that */
   escalar = (v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]);

   return escalar;
}

【问题讨论】:

  • 如果您可以将闲聊减少到最低限度,这个问题的可读性会更高。只是事实。
  • 另外,这里没有我能看到的实际问题。
  • 在 introduirVector() 和 producteEscalar() 中,您使用的是局部变量而不是传递给函数的数据。但是你会遇到问题,你传递的是值而不是引用,所以你的 introduirVector 无论如何都无法更改传递给它的向量。

标签: c++ arrays input random vector


【解决方案1】:

问题是这样的:

/*3x Input combo*/
void introduirVector (Vector)
{
    int i;
    Vector v;

这个函数接受Vector作为参数,但是Vector没有名字,所以不能在函数内部使用。

然后你声明一个新的本地Vector v。您将用户的输入读入此向量。

函数然后结束,此时您读取用户输入的向量消失。

首先,您应该使用调用时使用的参数,而不是局部变量。但是您的第二个问题是您使用的是pass by value。当您调用此函数时,introduirVector (v1); 不是您在 introduirVector 中使用的 main 中您熟悉和喜爱的“v1”,而是 本地副本当你的函数返回时,它就不再存在了。

你需要做的是让你的函数接受一个指针或对它被调用的 Vector 的引用:

void introduirVector(Vector& v)
{
    for (size_t i = 0; i < 3; ++i) {
        cin >> v[i];
    }
}

这并不能完全解决您的代码的所有问题,但它应该让您继续前进。

【讨论】:

    【解决方案2】:

    我没有看到任何输入,这就是您没有收到任何输入的原因。

    尝试使用:

    string inpt;
    int a, b, c;
    cin >> inpt;
    a = atoi( inpt.c_str());
    inpt = "";
    cin >> inpt;
    b = atoi( inpt.c_str());
    // etc...
    
    // Make your vector in this fashion, grabbing each integer separately then loading them
    // into a vector
    

    【讨论】:

    • 输入在 introduirVector() 中。参见“cin >> v[i];”
    • 哦,我的错,所以问题是他传递的是一个向量,而不是一个指向向量的指针。这会导致编辑范围不断变化的变量。渲染无效
    • 这是其中的问题。 cin 仅适用于局部变量...不使用传递给函数的数据。传递的数据是按值传递而不是引用...等
    • 所以这只是一个可怕的、难以理解的混乱,应该被忽略吗?
    • 我的课上还没有解释字符串。我不能使用它们。
    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    相关资源
    最近更新 更多