-2

I am a newbie programmer in c++ started my Computer Science degree. I got a problem with my assignment.

You need to make a function char calculate_daily_sale(int []).
You need to define and read an array of integer values of length 10 in the main() function.
Write a function charcalculate_daily_sale (int []) that accepts the array as argument from the main() function. The function will sum the values of array. If the values are greater than or equal to 15,000 the function return ‘y’ back to the main function, otherwise ‘n’ is returned from the function.

This is the code that I managed to write:

#include <iostream>

using namespace std;

char calculate_daily_sale(int[])
{
    int arr[10];
    int *ptr[10];
    int sum=0;
    for(int j=0; j<=9; j++)
    {
        ptr[j]=&arr[j];
        sum=sum+arr[j];
        cout<<sum;
    }
}

int main()
{
    int n,y;
    int arr[10];
    int *ptr[10];
    for(int i=0; i<=9; i++)
    {
        ptr[i]=&arr[i];
        cin>>*ptr[i];
    }
    if(calculate_daily_sale(int[])>=15000)
    {
        return y;
    }
    else
    {
        return n;
    }
       return 0;
}

The error that I am getting is:

expected primary expression before 'int'

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Dark Vedar
  • 29
  • 9
  • Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). It starts with "_Write a title that summarizes the specific problem_". – Ted Lyngmo May 12 '20 at 08:14
  • What is the purpose of `ptr`? Why don't you simply work just with `arr`? – Daniel Langr May 12 '20 at 08:15
  • 3
    `if (calculate_daily_sale(int[])...` is not how to call a function with an argument. – Sani Huttunen May 12 '20 at 08:15
  • 2
    You're not passing in a array variable when calling calculate_daily_sale and you're not declaring a parameter name in your declaration of calculate_daily_sale. – auburg May 12 '20 at 08:15
  • No.1 tip, ditch magic numbers its a recipe for disaster, replace 10 with a definition or const then replace 9 with the same definition or const -1. Change calculate_daily_sale and replace int[] in the call with "arr" – SPlatten May 12 '20 at 08:15
  • You probably meant to pass the array `arr` to `calculate_daily_sale`. Also `calculate_daily_sale` needs to return a value (probably the sum you were calculating, expecting a return type of `int` or the 'y' or 'n' character in case of `char`). – E. van Putten May 12 '20 at 08:17
  • Note that `return y;` and `return n;` are returning uninitialized variables and that returning them will not print anything. (Just in case you expect any output) – Lukas-T May 12 '20 at 08:19
  • Can you guys post the solution to it? I've been stuck with this from 2-3 days. I've done the rest of my assignment just stuck with this. – Dark Vedar May 12 '20 at 08:19
  • @HumayunSaeed You should have likely better spent those 2-3 days by reading some [good C++ book for beginners](https://stackoverflow.com/q/388242/580083). Stackoverflow is a not free code-writing service. What will be good for to solve your assignment for you? It's basically a tutorial code. – Daniel Langr May 12 '20 at 08:21
  • 1
    There are many bugs in this code; people usually answer only one specific question. Even if your question is answered, your code will not work, and you will have to solve the next error. Better start with minimal code, so there is only one bug you need to solve. See also: [mcve]. – anatolyg May 12 '20 at 08:22
  • @DanielLangr Thanks for that and I'll look into that, I get this problem because my college professor seldom helps. I managed to do rest of my assignment on my own was stuck with this only one. – Dark Vedar May 12 '20 at 08:24
  • @anatolyg Thanks for the suggestion, I'll definitely look into you suggestion. – Dark Vedar May 12 '20 at 08:26
  • You will always return `n` (uninitialized, so you have undefined behavior) since `calculate_daily_sale` will never be able to return something larger than 127 (on common systems). `calculate_daily_sale` is however missing a `return` statement so your program has undefined behavior there too. – Ted Lyngmo May 12 '20 at 08:26
  • 2
    You should enable your compiler warnings and fix them. At my college our code was built with `-Wall -Wextra -pedantic -Werror`. If you had an error the tutor wouldn't even check your code and you didn't pass the assignment. – Thomas Sablik May 12 '20 at 08:26
  • 1
    Why do you have a pointer `ptr` in `calculate_daily_sale` if you don't use it? Why do you have uninitialized variables `n` and `y`? Why do you return its values from `main`? `calculate_daily_sale` should return a char but doesn't return anything. You compare its return value in `if(calculate_daily_sale(int[])>=15000)` with 15000. A char can't be larger than 255. – Thomas Sablik May 12 '20 at 08:30
  • @ThomasSablik I don't know what I was doing there, I am rewriting the code again. Let's see what I can do with the bunch of bugs in the code. – Dark Vedar May 12 '20 at 08:34

2 Answers2

3

You need to take a step back and learn the basics of C++ programming.

Some points you should be looking at are:

char calculate_daily_sale(int[])

The function has a return type of 'char' and therefore needs a return statement. The function parameter is not named and not used. It can be removed entirely.

if(calculate_daily_sale(int[])>=15000)

When calling a function, you need to pass a value, not a type 'int[]' The return type of is char so it seems odd to be comparing it with 15000.

return y and return n

n and y are uninitialized.

A value returned from main is simply an error code returned to the operating system that runs the programme. It seems unlikely that you want to return these numbers, whatever they are. My reading of the spec is that you need to be returning the characters 'n' and 'y' (for 'yes' and 'no') from your calculate_daily_sale and to main, which is why the return type is char.

Jasper Kent
  • 3,546
  • 15
  • 21
1

Error messages always mention line number. This is the way you can locate the error precisely. Assuming your error is in this line

    if(calculate_daily_sale(int[])>=15000)

You probably meant to pass the array arr to calculate_daily_sale:

    if(calculate_daily_sale(arr)>=15000)
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • This is by far not the only problem in OP's code. This: `if(calculate_daily_sale(arr)>=15000)` by itself will not work since the function parameter is not used. – Daniel Langr May 12 '20 at 08:24
  • I am not going to fix all bugs in the code: that would ruin all the 'fun' for OP! This specific question is worth answering because the specific error message may look too cryptic to a newbie. – anatolyg May 12 '20 at 08:28
  • I don't want the full solution anyways this much would do, I'll check for the other bugs that I have in this code. Thanks for the help. If I get the whole solution that wouldn't do any good for me and I would struggle to do other complex problems in future and wouldn't learn anything. – Dark Vedar May 12 '20 at 08:32