-1

I have two problems with my code. The first one is when the code is executed for second time and so on, it skip the first cin command but it does generate the first cout. My second problem is that if I input a value true for the lowers if statement I don't generate the cout. Everything else is working correctly except for does two thing

#include "pch.h"
#include <iostream>
#include <string>
#include <random>
#include <ctime>>

using namespace std;
int main()
{
    mt19937 generator;
    generator.seed(time(0));
    uniform_int_distribution<uint32_t>dice(1000, 9999);

    int ownderID=dice(generator);
    string ownerName;
    string dogName;
    string dogBreed;
    float dogAge;
    float dogWgt;
    int exits;
    int weekCost = 0;
    int dogAmount = 0;
    int dailyPrice = 0;

    do
    {
        cout << "Owner Name: ";
        getline(cin, ownerName);
        cout << endl;

        cout << "Dog Name: ";
        getline(cin, dogName);
        cout << endl;

        cout << "Dog Breed: ";
        getline(cin, dogBreed);
        cout << endl;

        cout << "Dog Age: ";
        cin >> dogAge;
        cout << endl;

        cout << "Dog Weight in lb: ";
        cin >> dogWgt;
        cout << endl;

        cout << "Daily Bill" << endl;
        cout << "Owner Name: " << ownerName << " ID" << "[" << ownderID << "]" << endl;
        cout << "Dog Name: " << dogName << endl;
        cout << "Dog Age: " << dogAge << endl;
        cout << "Dog Breed: " << dogBreed << endl;

        if (0<dogWgt && dogWgt <= 15)
        {
            dailyPrice = 55;
            weekCost += 55;
            if (15 < dogWgt && dogWgt <= 30)
            {
                dailyPrice = 75;
                weekCost += 75;
                if (30 < dogWgt && dogWgt <= 80)
                {
                    dailyPrice = 105;
                    weekCost += 105;
                    if (dogWgt > 80)
                    {
                        dailyPrice = 125;
                        cout << "Daily Cost for (" << dogWgt << ") is $" << dailyPrice << endl;
                        weekCost += 125;
                    }
                }
            }
        }

        cout << "To terminate enter -1; To add another dog information enter 1: ";
        cin >> exits;
        cout << endl;

        dogAmount += 1;

    } while (exits != -1);
    cout << "Total week cost: " << weekCost << endl;
    cout << "Total number of daycare entries: " << dogAmount << " dogs" << endl;
    system("PAUSE");
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    You need to use `if/else` not nested if. – Brandon Dyer Feb 14 '20 at 20:53
  • 1
    The code in the block following an if-statement will only execute **if the condition is true**. So you only check if the dog weight is between 15 and 30 if the dog weight was found to be between 0 and 15. How can both of those ever be true? Do you see why that doesn't make sense? – Klaycon Feb 14 '20 at 20:56
  • 1
    Unrelated: `#include >` looks a bit odd. Not sure what the compiler will make of it. – user4581301 Feb 14 '20 at 20:58
  • `#include "pch.h"` suggests the use of Visual Studio. Visual Studio has a functional `random_device` and I recommend using it in place of `time(0);` in `generator.seed(time(0));`. Eg: `generator.seed(random_device{}());`. – user4581301 Feb 14 '20 at 21:06

1 Answers1

1

In the very end of the do-while loop insert the following call to remove the new line character from the input stream that is leaved there after this statement

cin >> exits;

That is

#include <limits>

//...

    cin >> exits;
    cout << endl;
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    dogAmount += 1;

} while (exits != -1);

Substitute these nested if statements

    if (0<dogWgt && dogWgt <= 15)
    {
        dailyPrice = 55;
        weekCost += 55;
        if (15 < dogWgt && dogWgt <= 30)
        {
            dailyPrice = 75;
            weekCost += 75;
            if (30 < dogWgt && dogWgt <= 80)
            {
                dailyPrice = 105;
                weekCost += 105;
                if (dogWgt > 80)
                {
                    dailyPrice = 125;
                    cout << "Daily Cost for (" << dogWgt << ") is $" << dailyPrice << endl;
                    weekCost += 125;
                }
            }
        }
    }

for if-else statements like

    if (0<dogWgt && dogWgt <= 15)
    {
        dailyPrice = 55;
        weekCost += 55;
    }
    else if (15 < dogWgt && dogWgt <= 30)
    {
        dailyPrice = 75;
        weekCost += 75;
    }
    else if (30 < dogWgt && dogWgt <= 80)
    {
        dailyPrice = 105;
        weekCost += 105;
    }
    else // (dogWgt > 80)
    {
        dailyPrice = 125;
        cout << "Daily Cost for (" << dogWgt << ") is $" << dailyPrice << endl;
        weekCost += 125;
    }

Or maybe you mean that the last else statement should be

    else // (dogWgt > 80)
    {
        dailyPrice = 125;
        weekCost += 125;
    }

after which this statement follows

    cout << "Daily Cost for (" << dogWgt << ") is $" << dailyPrice << endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Typo at `std::cin>ignore( std::numeric_limits::max(), '\n' );`. Not sure this sis such a good idea. What it there isn't anything to ignore, the first iteration, for example. – user4581301 Feb 14 '20 at 21:00
  • @user4581301 Thanks. I fixed the typo. – Vlad from Moscow Feb 14 '20 at 21:02
  • 1
    @user4581301 If the buffer is empty then nothing will be removed from it. So there is no problem. – Vlad from Moscow Feb 14 '20 at 21:04
  • But it will block and wait for the user to hit enter. – user4581301 Feb 14 '20 at 21:10
  • @user4581301 As fat as I know ignore waits nothing. – Vlad from Moscow Feb 14 '20 at 21:11
  • The way I read the docs, `ignore` will go on ignoring until it finds a newline, the input exceeds the `max()`, or the input stream ends. There may be nothing in the stream at start-up, but it hasn't ended. I'm pretty sure this will need to be kick-started with an extra newline. Put the ignore after the input that leaves the newline in the stream instead of before the next input and the problem becomes moot. – user4581301 Feb 14 '20 at 21:28
  • @user4581301 The buffer is empty. There is nothing to ignore.:) – Vlad from Moscow Feb 14 '20 at 21:34