我正在学习C ++,我想问用户输入4个数字的功能,然后只需打印阵列。
int getFourNums();
int main(int argc, char** argv){
int getNums;
getNums = getFourNums();
cout << "The array is: " getNums << endl;
}
int getFourNums(){
int i;
int myArray[4];
cout << "Enter 4 nums: ";
for(i = 0; i < 4; i++){
cin >> myArray[i];
}
return myArray[i];
截至目前,它让我得到了四个数字,但这是打印的结果是“该数组是:0”。我不明白为什么阵列看似不填充。
你的根本问题是int getFourNums()
只能返回一个整数,而不是他们的数组。接下来的问题是,功能不能因为历史原因返回原始阵列。可以选择对返回std::array
,含有该阵列的struct
,通过引用传递数组到函数,或返回一个std::vector
。我对这个应用程序的偏好是一个std::vector
- 它是灵活的,虽然不太一样有效,std::array
,你应该默认为std::vector
除非你有一个很好的理由,否则。然后,您getNums
代码是这样:
std::vector<int> getFourNums() {
std::vector<int> result;
cout << "Enter 4 nums: ";
for(int i = 0; i < 4; i++){
int v;
cin >> v;
result.push_back(v);
}
return result;
}
要打印载体,见this question。我个人的喜好而定了范围为基础的循环在向量;您的口味可能会有所不同。
在代码中的一个问题是,像循环
for(i = 0; i < 4; i++){
cin >> myArray[i];
}
将结束与i==4
。因此,return myArray[i]
将超过数组边界和/或然后访问一个未初始化的值,并产生未定义的行为。
主要的问题,然而,就是在C ++中,你会遵循一个非常不同的方法,并使用集合类型,如std::vector
不是纯阵列。参见下面的代码示出这一点。希望能帮助到你。
#include <vector>
#include <iostream>
std::vector<int> getFourNums(){
int val;
std::vector<int> result;
cout << "Enter 4 nums: ";
for(int i = 0; i < 4; i++){
cin >> val;
result.push_back(val);
}
return result;
}
int main(int argc, char** argv){
std::vector<int> fourNums = getFourNums();
for (auto i : fourNums) {
cout << i << endl;
}
}
int getFourNums()
只会让你因为int
返回一个return myArray[i];
,而不是整个数组和i == 4
是出界。您只能使用范围[0,3]作为评价你的阵列。下面是与代码中的注释一个被整顿的版本。
#include <iostream>
#include <vector>
// don't do "using namespace std;" since it includes
// a lot of stuff you don't need.
// Here's a function that will return a vector of int's
// It'll behave much like a C style array
// but can have variable length and you can use
// a lot of standard functions on it.
std::vector<int> getNums(size_t count) {
// The "array" we'll return with "count" number of
// default constructed int:s (they will all be 0):
std::vector<int> myArray(count);
std::cout << "Enter " << count << " nums: ";
// A range based for loop that will go through
// all int:s in "myArray". "num" will be
// a reference to each int in the vector which
// means that if you change the value of "num",
// you'll actually change the value in the vector.
for(int& num : myArray) {
// read values into the int currently
// referenced by num
std::cin >> num;
}
// return the vector by value
return myArray;
}
// Put main() last so you don't have to forward declare the functions
// it uses
int main() {
// call getNums with the value 4 to read 4 int:s
std::vector<int> Nums = getNums(4);
std::cout << "The array is:";
// print each int in the vector. There's no need to use
// a reference to the int:s here since we won't be changing
// the value in the vector and copying an int is cheap.
for(int num : Nums) {
std::cout << " " << num;
}
// std::endl is rarely good when you only want to output a newline.
// It'll flush the buffer with is costly.
// Make a habit of using "\n" in most cases.
std::cout << "\n";
}
我看到你想返回整个数组,但只是看看你的返回类型:
int getFourNums()
你返回一个整数吗?在这种情况下返回的整数总是myArray[4]
。要知道,这是一个整数值,你回来的东西,不属于你居然!
那么该怎么办?我建议你通过你的阵列的功能是这样的:
void getFourNums(int myArray[]){
int i;
cout << "Enter 4 nums: ";
for(i = 0; i < SIZE; i++){
cin >> myArray[i];
}
}
现在,你充满了你的阵列。如何打印您的阵列呢?我们不能简单地给我们的数组名称,并告诉COUT像你这样打印出来(你可以不实际!)。什么神奇这里。我们将通过一个打印数组的元素之一:
void printFourNumbers(int array[])
{
for(int i = 0 ; i < SIZE ; ++i)
{
cout << array[i] << endl;
}
}
最后,整个代码看起来是这样的:
#include <iostream>
using namespace std;
const int SIZE = 4;
void getFourNums(int myArray[]);
void printFourNumbers(int array[]);
int main(int argc, char** argv){
int myArray[SIZE];
getFourNums(myArray);
printFourNumbers(myArray);
}
void getFourNums(int myArray[]){
int i;
cout << "Enter 4 nums: ";
for(i = 0; i < SIZE; i++){
cin >> myArray[i];
}
}
void printFourNumbers(int array[])
{
for(int i = 0 ; i < SIZE ; ++i)
{
cout << array[i] << endl;
}
}