[英]I'm having trouble in C++ geting input from a user in a function, adding to an array, and printing that array
我正在學習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
,包含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;
}
要打印矢量,請參閱此問題 。 我的個人偏好是基於范圍的向量循環。 您的口味可能會有所不同。
您的代碼中的一個問題是這樣的循環
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.