简体   繁体   English

如何将用户输入存储在数组中?

[英]How do i store user input in an array?

Why isn't my function taking any input and outputing anything?为什么我的函数不接受任何输入和输出任何东西? I want to be able to recieve user input, store it in my array and then print out the value(s) stored.我希望能够接收用户输入,将其存储在我的数组中,然后打印出存储的值。

using namespace std;

void Memory(){
    int temp;
    int Mange[3] = {0, 0, 0};

    for(int i = 0; i < sizeof(Mange); i++){
    cin >> temp;
    temp = Mange[i];
    cout << Mange[i];
    }
}

int main() {

    Memory();

    return 0;
}

This is a great exercise to do if you are just starting to get familiar in working with arrays, good on you!如果您刚刚开始熟悉使用数组,这是一个很好的练习,对您有好处! This is how I would implement a program that would accept user input into an array, and then print each element in the array (be sure to read the comments!):这就是我将如何实现一个程序,该程序将接受用户输入到数组中,然后打印数组中的每个元素(请务必阅读注释!):

#include <iostream>

using namespace std;

const int MAXSIZE = 3;

void Memory(){
    //initialize array to MAXSIZE (3).
    int Mange[MAXSIZE]; 

    //iterate through array for MAXSIZE amount of times. Each iteration accepts user input.
    for(int i = 0; i < MAXSIZE; i++){
    std::cin >> Mange[i];
    }

    //iterate through array for MAXSIZE amount of times. Each iteration prints each element in the array to console.
    for(int j = 0; j < MAXSIZE; j++){
    std::cout << Mange[j] << std::endl;
    }
}

int main() {
    Memory();
    return 0;
}

I hope this helps!我希望这有帮助! A great way to learn about what this program is actually doing is to copy and paste this into an IDE and then run a debugger on the code and observe what happens line by line.了解该程序实际执行的操作的一个好方法是将其复制并粘贴到 IDE 中,然后对代码运行调试器并逐行观察发生的情况。

I myself at first did this when started arrays , then got the hang of it .我自己起初在启动数组时就这样做了,然后就掌握了窍门。 You did half of the program correct , that's good , the only mistakes u made are storing the input data in the appropriate variables and displaying them in a wrong format To fix this,你做了一半的程序正确,那很好,你犯的唯一错误是将输入数据存储在适当的变量中并以错误的格式显示它们要解决这个问题,

#include<iostream>

using namespace std;

const int size=3;

void Memory()
{
    // You don't need variable temp here
    int Mange[size] = {0, 0, 0};

    //It's not necessary to keep it zero but it's a good practice to intialize them when declared

    for(int i = 0; i < size; i++)

    //You can store maximum value of array in diff variable suppose const then use it in condition

    {
    cin >> Mange[i];
    cout << Mange[i];
   //You can print it later with a different loop
   }

}

int main() {
    Memory();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户输入存储为char *? - How do I store user input as a char*? 如何将用户输入存储到多维数组中? 我正在使用3D阵列 - How do I store user input into a multidimensional array? I'm working with a 3D array 如何接受用户输入,对该输入进行数学运算,并将结果存储在数组中? 在 C++ - how to take user input, do math to that input, and store the results in an array? in C++ c++ - 如何将用户输入存储到数组中 - How to store user input to array in c++ 如何将每个用户输入存储到数组中? - How to store each user input into array? 如何将用户输入存储在int数组中 - How to store user input in an int array 尝试将用户输入存储到数组中 - Attempting to store user input into array 如何一次将来自用户输入的多个值存储到向量中(在 C++ 中)? - How do I store multiple values from user input into a vector at once (in c++)? 如何修复我的 function 中的 fstream 文件输入以将正确的信息存储在我的结构数组中? - how do i fix my fstream file input in my function to store the proper info in my array of structs? 如何从控制台输入学生的全名(包括空格)并将其存储在c ++中的数组中? - How do I take full name of students(including space) as input from console and store it in an array in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM