URGENT i have 2 hours to pass up my assignment

(Need to use sentinel-controlled while loop)

this is the work "students at a school volunteered to sell fresh baked cookies to raise funds to increase the number of computers in the computer lab. Each student reported the number of boxes he/she sold. Write a program that wil output the total number of boxes of cookies sold, the total revenue generated by selling the cookies and the saverage number of boxes sold by each student. The programmer do not know the exact number of volunteers involved, we assume that -1 that will mark the end of the data.
Assume the data input is in the following form: student's name followed by a space and the number of boxes sold by the student
1
2
3
4
5
6
std::string name ;
int num_boxes ;
while( std::cin >> name && name != "-1" && std::cin >> num_boxes && num_boxes != -1 )
{
    // ...
}
What is this?
@MulStudent

(Need to use sentinel-controlled while loop)

and
What is this?


It's your answer, on how to pass your assignment.!!
Last edited on
@whitenite1

so is that the only source code i need to type in c++ ?
My guess to what you mean by "sentinel-controlled while loop" is "loop until the user says to stop". If that is the case, you could make your loop conditional -1, but its much easier to use Boolean's (since these technically represent the values 0 and 1 respectively) once you understand the logic.

I think the code below is more what you're after:

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
const int NAME = 10;     // Max character counter for the student name.
const int MAX = 100;     // General maximum (for I/O and such).

int main()
{
    bool quit = false;          // Loop conditional.
    char student[NAME];    // Name of student.
   
    // Num of students & boxes.
    int numof_students = 0, numof_boxes = 0, totalboxes = 0;

    // Price per box, total/avg revenue.
    int price = 0, revenue = 0, avg_sold;

    cout << " Set the price per box: ";
    cin >> price;
    cin.ignore(MAX, '\n');

    do
    {
       cout <<endl << " Enter the student name: ";
       cin.get(student, NAME, '\n');    // Get the student's name.
       cin.ignore(NAME, '\n');

       numof_students++;    // Count the student.

       cout << endl << " Number of boxes sold: ";
       cin >> numof_boxes;
       cin.ignore(MAX, '\n');

       totalboxes += numof_boxes;    // Sum the num of boxes...

       revenue += (price * numof_boxes);    // ...and the revenue.

       char response;
       cout << " Enter another? (y/n):";    // Rinse & Repeat?
       cin >> response;
       cin.ignore(MAX, '\n');

       response = tolower(response);

       if(response == 'n')
            quit = true;    // Break from the loop. (quit = false otherwise)

    }while(!quit);

    avg_sold = revenue / numof_students;    // Calculate the avg.

    cout << endl << endl << endl
         << " The total revenue is: " << revenue << endl << endl
         << " Average number of boxes sold: " << avg_sold << endl << endl;
         
         
    return 0;
}



Hope that helps.
Last edited on
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
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct student
{
    string name;
    int boxesSold;
    float totalMade;
};


float calcAverageBoxes(vector<student> &, int);
float calctotalRev(vector<student> &, int);
int calcTotalBoxes(vector<student> &, int);

int main()
{
    vector<student> info;
    

    info.push_back(student());
    int size = info.size();
    int count = 0;
    cout << "enter -1 to quit" << endl << endl;
    while(true)
    {
        size = info.size();
        cout << "Enter in name of student " << size << " : ";
        getline(cin, info.at(count).name);
        if(info.at(count).name == "-1")
        {
            break;
        }
        cout << "How many boxes did student " << size << " sell: ";
        cin >> info.at(count).boxesSold;
        cout << "How many did student " << size << " make: ";
        cin >> info.at(count).totalMade;
        info.push_back(student());
        count++;
        cin.ignore();
    }
    for(int i = 0; i < size-1; i++)
    {
        cout << "Student: " << info.at(i).name << endl;
        cout << info.at(i).name << " sold boxes amount: " << info.at(i).boxesSold << endl;
        cout << info.at(i).name << " total money raised: " << info.at(i).totalMade << endl << endl;
    }
    cout << endl << endl;

    float averagebox = calcAverageBoxes(info, size); // average num of box sold by stuent
    int totalbox = calcTotalBoxes(info, size); // total box sold
    float rev = calctotalRev(info, size); // total rev

    //cout << "Average boxes sold by a student: " << averagebox << endl;
    //cout << "Total boxses sold: " << totalbox << endl;
    cout << "Total money raised: $" << rev << endl;

    return 0;
}


float calcAverageBoxes(vector<student> &info, int cnt)
{
    float x = 0;
    for(int i = 0; i < cnt; i++)
        x = x+info.at(i).boxesSold;
    return x/info.size();
}

float calctotalRev(vector<student> &info, int cnt)
{
    float x = 0;
    for(int i = 0; i < cnt; i++)
        x = x+info.at(i).totalMade;
    return x;
}

int calcTotalBoxes(vector<student> &info, int cnt)
{
    int x = 0;
    for(int i = 0; i < cnt; i++)
        x = x+info.at(i).boxesSold;
    return x;
}


here is what i got. it's pretty messy, but if you can clean it up, good for u
Alright thank you very much!
Topic archived. No new replies allowed.