Peanut Peanut - 2 months ago 6
C++ Question

assignment switch case c ++

Requirements


  1. VULTURE IS V, OWL IS O, EAGLE IS E...

  2. A
    for
    loop to input the data each bird watcher has collected.

  3. inside the
    for
    loop, a
    do ... while
    loop to input and process the data collected by one bird watcher.

  4. inside the
    do ... while
    loop a
    switch
    statement is used to calculate the number of eggs for each type of bird. the default option, which does nothing, is used when an
    x
    is entered.

  5. the
    do ... while
    loop is exited when an X is entered for the type of bird.

  6. The totals part is fine as per code below



ok, now my problem is I can't seem to get through my switch case. It prompts me for the first watcher's info, when I enter it, it never moves over to the next watcher.

The input data given is

3
E2 O1 V2 E1 O3 X0
V2 V1 O1 E3 O2 E1 X0
V2 E1 X


And here is the code that I got so far:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

int totNrVultureEggs, totNrEagleEggs, totNrOwlEggs, nrEggs,
nrVultureEggs, nrEagleEggs, nrOwlEggs, nrBirdWatchers, nrEggsEntered;

char bird;

// initialize grand totals for number of eggs for each type of bird
cout << "How many bird watchers took part in the study?";
cin >> nrBirdWatchers;

// loop over number of bird watchers
for (int i = 0; i < nrBirdWatchers ;i++ )
{
// initialize totals for number of eggs for each type of bird
// this bird watcher saw
nrVultureEggs = 0;
nrEagleEggs = 0;
nrOwlEggs = 0;
cout << "\nEnter data for bird watcher " << i + 1 << ":" << endl;

//loop over bird watchers
do{

cin >> bird >> nrEggs;
switch (bird)
{
case 'E':
case 'e':
nrEagleEggs = nrEagleEggs + nrEggs;

case 'O':
case 'o':
nrOwlEggs = nrOwlEggs + nrEggs;

case 'V':
case 'v':
nrVultureEggs = nrVultureEggs + nrEggs;

default :
nrBirdWatchers++;
break;

}

}while (i < nrBirdWatchers )
;
cout << "Bird watcher " << i + 1 << " saw " << nrVultureEggs;
cout << " vulture eggs, " << nrEagleEggs << " eagle eggs and ";
cout << nrOwlEggs << " owl eggs " << endl;
// increment grand totals for eggs
}

// display results
cout << "\nTotal number of vulture eggs: " << totNrVultureEggs;
cout << "\nTotal number of eagle eggs: " << totNrEagleEggs;
cout << "\nTotal number of owl eggs: " << totNrOwlEggs;
return 0;
}

Answer

I rewrote the whole program, now it works, but take care of the input: because of the type of the input, you have to give ALWAYS a couple of char-int, or you will have a bad time xD [The problem is in the buffer].

So the input would be:

3
E2 O1 V2 E1 O3 X0
V2 V1 O1 E3 O2 E1 X0
V2 E1 X0

The source below:

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

    bool done;
    char birdType;
    int eagleEggs, owlEggs, vultureEggs;
    int totEagleEggs, totOwlEggs, totVultureEggs;
    int eggsTemp, eggsIn, birdWatchers;

    cout << "How many bird watchers took part in the study?";
    cin >> birdWatchers;

    totEagleEggs = totOwlEggs = totVultureEggs = 0;

    for (int i = 0; i < birdWatchers ;i++ ){
        eagleEggs = owlEggs = vultureEggs = 0;
        done = false;

        cout << endl;
        cout << "Enter data for bird-watcher n. " << (i + 1) << ":" << endl;
        do{
            cin >>  birdType >> eggsTemp;
            switch (birdType)
            {
                case 'E':
                case 'e':
                eagleEggs += eggsTemp;
                totEagleEggs += eagleEggs;
                break;

            case 'O':
              case 'o':
                owlEggs += eggsTemp;
                totOwlEggs += owlEggs;
            break;

            case 'V':
            case 'v':
                vultureEggs += eggsTemp;
                totVultureEggs += vultureEggs;
            break;

            default:
                done = true;
            }
        }while (!done);

        cout << "The bird-watcher n. " << (i + 1) << " saw " << vultureEggs;
        cout << " vulture eggs, " << eagleEggs << " eagle eggs and ";
        cout << owlEggs << " owl eggs." << endl;
    }

    cout << endl;
    cout << "Total number of vulture eggs: " << totVultureEggs << endl;
    cout << "Total number of eagle eggs: " << totEagleEggs << endl;
    cout << "Total number of owl eggs: " << totOwlEggs << endl;
    system("PAUSE");
    return 0;
}