1

I am trying to input data to an array and then print them by using a function but I am not sure how to organize the code.

#include <iostream>
using namespace std;

void showGrade(double grade[], int size);

int main()
{
    double grade[];
    int size;

    for (int i = 0; i < size; i++) 
    {
        cout << "Please enter the number of grade" << endl;
        cin >> size;
    }

    showGrade(grade, size);
    return 0;
}

void showGrade(double grade[], int size)    //How many grade we have
{
    for (int counter = 0; counter < size; counter++) 
    {
        cout << "Please enter your grades: " << endl;
        cin >> grade[counter];
        cout << "Here are your grades: " << endl;
    }
}

I Expect to see how many grades I input and then show them.

UPDATE 8/28/19

I figured out how to do it in main function successfully. But what I really want is to put them in a seperate function. My new codes have error at the function call which is type name is not allowed and expected a ')'. How do I make it work?

#include <iostream>
using namespace std;

void showGrades(double ar[], int size);

int main()
{
    double ar[20];
    int size;

    showGrades(double ar[size], int size);

    system("pause");
    return 0;
}

void showGrades(double ar[], int size) {
    cout << "Please enter the number of grade "; // array size
    cin >> size;
    cout << "Please enter your grades " << endl;
    for (int i = 0; i < size; i++) {
        cin >> ar[i];
    }

    cout << "The grades you entered are: " << endl;

    for (int i = 0; i < size; i++) {
        cout << ar[i] << endl;
    }
}
  • `double grade[];` <-- No. Just no. Use a `std::vector`. – Jesper Juhl Aug 22 '19 at 19:25
  • `int size; for (int i = 0; i < size; i++) {` - That'll never work. `size` is uninitialized. – Jesper Juhl Aug 22 '19 at 19:26
  • `using namespace std;` - That's *usually* a bad idea. – Jesper Juhl Aug 22 '19 at 19:27
  • 1
    Unrelated: I would not expect a function named `showGrade` to take inputs fro grades. I would expect it to merely show grades. An `InputGrades` function should be created to handle the input task. In general, a function should do only one thing (even if that one thing is to aggregate the results of a bunch of functions that each do one thing) and be named according to the one thing it does. This allows you to build very simple and easily tested functions that serve as building blocks for a more complicated program, testing everything as you go. – user4581301 Aug 22 '19 at 20:10
  • Oh I thought I did initialize the size already. So where should I do it? in the main function? – Minh Nguyen Aug 23 '19 at 02:32

2 Answers2

1

this is not a valid C++ code:

double grade[];

You can use std::vector:

std::vector<double> grade;

To insert a grade to the vector you can use grade.push_back(someGrade);

Oblivion
  • 7,176
  • 2
  • 14
  • 33
1
  1. First of all, you need to use the standard container std::vector instead of the double grade[];, since you want a variable-length array as per user input.

  2. Secondly, you are using un-initialized size variable in

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

    hence it will be initialized with a garbage value. There you need no for-loop

A good starting would be:

#include <iostream>
#include <vector>    // std::vector
void showGrade(std::vector<double>& grade)
//            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> pass the vector by ref, as the grades should be inseted to the it
{
    // the logic
}

int main()
{
    int size;
    std::cout << "Please enter the number of grade" << endl;
    std::cin >> size;

    std::vector<double> grade;
    grade.reserve(size); // reserve memory for unwanted re-allocations

    showGrade(grade);
    return 0;
}

I leave it to you to complete, after reading about the std::vector more.

Also, do not practice with using namespace std;. Read more: Why is "using namespace std;" considered bad practice?

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Thanks for your suggestion, I'll read more how to use vector cause I don't about it. – Minh Nguyen Aug 23 '19 at 02:29
  • @MinhNguyen You are welcome: might be interesting to have a short read on the followings:[How to add an element in Vector using vector::push_back](https://thispointer.com/how-to-add-an-element-in-vector-using-vectorpush_back/) and [How to print out the contents of a vector?](https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector) – JeJo Aug 23 '19 at 06:34
  • I figured out how to do it in main function successfully. But what I really want is to put them in a separate function. My new codes have error at the function call which is type name is not allowed and expected a ')'. How do I make it work? – Minh Nguyen Aug 28 '19 at 23:28
  • I updated below the original code. Just scroll down a little bit. – Minh Nguyen Aug 29 '19 at 16:30
  • That's ok, I found out the way by adding & to the function prototype and function declaration. Thank you. – Minh Nguyen Aug 30 '19 at 06:18
  • @MinhNguyen your welcome. I am glad that helped you! – JeJo Aug 30 '19 at 06:39