functions

alright i have this code now i have been asked to break it functions now iv been asked to break it down more which i don't what more i could do any suggestions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//Determines the total number of green-necked vulture eggs
//counted by all conservationists in the conservation district.
#include <iostream>
using namespace std;

void displayInfo();
int getNumReport();
int get_one_total();

//Precondition: User will enter a list of egg counts
//followed by a negative number.
//Postcondition:returns a number equal to the sum of all the egg counts.

int main( )
{

    displayInfo();
	
    int number_of_reports;
    
	getNumReport();
	 
    int grand_total = 0, subtotal, count;
    for (count = 1; count <= number_of_reports; count++)
    {
        cout << endl << "Enter the report of "
             << "conservationist number " << count << endl;
        subtotal = get_one_total ();
        cout << "Total egg count for conservationist "
             << " number " << count << " is " 
             << subtotal << endl;
        grand_total = grand_total + subtotal;
    }

    cout << endl << "Total egg count for all reports = "
         << grand_total << endl;

    return 0;
}


int getNumReport()
{
     int number_of_reports;
	  cout << "How many conservationist reports are there? ";
      cin >> number_of_reports;
	  return number_of_reports;
	  
 } 
 
//Uses iostream:
int get_one_total()
{
int total;
    cout << "Enter the number of eggs in each nest.\n"
         << "Place a negative integer"
         << " at the end of your list.\n";

    total = 0;
    int next;
    cin >> next;
    while (next >= 0)
    {
        total = total + next;
        cin >> next;
  return total;
  }
}

void displayInfo()
{
cout << "This program tallies conservationist reports\n"
<< "on the green-necked vulture.\n"
         << "Each conservationist's report consists of\n"
         << "a list of numbers. Each number is the count of\n"
         << "the eggs observed in one"
         << " green-necked vulture nest.\n"
         << "This program then tallies"
         << " the total number of eggs.\n";
}
Really? Your instructor wants you to break this down into more functions instead of making it so that it works? Your variable 'number_of_reports' is undefined, so it's not known if your for loop will ever execute which makes doing anything other then fixing that point a bit moot.

EDIT: Also, you while loop in "get_one_total()" is broken, python delimits scope with white space, C\C++ do not.



Last edited on
yeah i think he wants this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{

    displayInfo();
	
    int number_of_reports;
    
	getNumReport();
	 
    int grand_total = 0, subtotal, count;
    for (count = 1; count <= number_of_reports; count++)
    {
        cout << endl << "Enter the report of "
             << "conservationist number " << count << endl;
        subtotal = get_one_total ();
        cout << "Total egg count for conservationist "
             << " number " << count << " is " 
             << subtotal << endl;
        grand_total = grand_total + subtotal;
    }

    cout << endl << "Total egg count for all reports = "
         << grand_total << endl;

    return 0;
}


more broken down but i dont want mess with that and ive complied it and it gives me no error
Line 19 should be: int number_of_reports = getNumReport(); Line 21 becomes redundant after that.

The 'return' on Line 66 should be outside of the while loop. Also consider what it would mean if you made that into a 'do...while()' loop instead.

You would know more about what your instructor wants then I would. But putting everything from main into another function doesn't make sense, main is a function. That doesn't change just because you aren't supposed to call it.
Last edited on
alright i did all that and it works and he just wants to shorten it up by putting it in functions
Topic archived. No new replies allowed.