simple practice help

Hi, I am hoping for some quick help: I am getting this code straight from textbook but cannot get it to pass line 35 when compiling. Any suggestions?

I am trying to learn how to fill an array from keyboard or from a file and this is one of the exercises I am using to gain know-how from.

// arrfun2.cpp -- functions with an array argument
// yost87 June 17, 2011
//
#include <iostream>
const int ArSize = 8;
int sum_arr(int arr[], int n);
// use std:: instead of using directive
int main()
{
int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128};
// some arrays require a preceeding int with static to
// enable array initialization

std::cout << cookies << " = array address, ";
// some systems require a type cast: unsigned (cookies)

std:: cout << sizeof cookies << " = sizeof cookies\n";
int sum = sum_arr(cookies, ArSize);
std::cout << "Total cookies eatenL " << sum << std::endl;
sum = sum_arr(cookies, 3); // a lie
std::cout << "First three eaters ate " << sum << " cookies.\n";
sum = sum_arr(cookies + 4, 4); // another lie
std::cout << "Last four eaters ate " << sum << "cookies.\n";
return 0;
}

// return the sum of an integer array
int sum_arr(int arr[], int n)
{
int total = 0;
std::cout << arr << " = arr, ";
// some systems require a type cast: unsigned (arr)
std::cout << sizeof arr << " = sizeof arr\n";
for(int i = 0, i < n, i++) // HERE IS PROBLEM <--------------------------
total = total + arr[i];
return total;
}
Last edited on
Posting the exact error the compile log spouts out may make it easier to help you.
Gisle Aune, Thanks, that would help...

35 C:\Documents and Settings\D\Desktop\DMY\arrfun2.cpp In function `int sum_arr(int*, int)':
35 C:\Documents and Settings\D\Desktop\DMY\arrfun2.cpp expected `;' before ')' token
37 C:\Documents and Settings\D\Desktop\DMY\arrfun2.cpp expected primary-expression before "return"
37 C:\Documents and Settings\D\Desktop\DMY\arrfun2.cpp expected `;' before "return"
37 C:\Documents and Settings\D\Desktop\DMY\arrfun2.cpp expected `)' before "return"


the for loop should be: for(int i = 0; i < n; i++) -- semi-colons, not commas.
Code tags, please.
Gisle Aune, thanks again, that was the problem. I am new to this and missed that, thanks. I cannot seem to "put it all together" and get over the hump of learning this to write code successfully.

I do have another question:

I will begin a new thread titled "passenger List help"

All help will be appreciated and I thank you all in advance for even looking.
yost
Topic archived. No new replies allowed.