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;
    }
}
5
  • double grade[]; <-- No. Just no. Use a std::vector. Commented Aug 22, 2019 at 19:25
  • int size; for (int i = 0; i < size; i++) { - That'll never work. size is uninitialized. Commented Aug 22, 2019 at 19:26
  • using namespace std; - That's usually a bad idea. Commented Aug 22, 2019 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. Commented Aug 22, 2019 at 20:10
  • Oh I thought I did initialize the size already. So where should I do it? in the main function? Commented Aug 23, 2019 at 2:32

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your suggestion, I'll read more how to use vector cause I don't about it.
@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 and How to print out the contents of a vector?
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?
I updated below the original code. Just scroll down a little bit.
That's ok, I found out the way by adding & to the function prototype and function declaration. Thank you.
|
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);

2 Comments

Not sure this answers the question. Should probably be a comment instead.
Thank you, I don't really know how to use vector yet but I'll read more about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.