-1

I am trying to use cin.get() to pause a loop on every time around.

prodAtr.h:

#ifndef PRODATR
#define PRODATR

#include <array>
#include <vector>
#include <string>

extern std::array<std::string, 6> sProductType = { //Array contents here };

extern std::vector<std::vector<double>> nProductRates = {
    { //Array contents here },
    { //Array contents here },
    { //Array contents here },
    { //Array contents here },
    { //Array contents here },
    { //Array contents here }
};

#endif

Wholesale.cpp:

#include "stdafx.h"
#include <iostream>
#include "prodAtr.h"

int ShowProdOpt();
float GetCost();
void CalulateTiers(float, int);


int main()
{
    using namespace std;

    float fCost = GetCost();
    cout << endl;
    int nOptChoice = ShowProdOpt();

    CalulateTiers(fCost, nOptChoice);
    return 0;
}

int ShowProdOpt()
{
    using namespace std;

    cout << "Please select you product type: " << endl;
    for (unsigned int i = 0; i < sProductType.size(); i++)
    {
        cout << "[" << i + 1 << "]" << sProductType[i] << " ";
    }
    cout << endl;

    int nResult;
    cin >> nResult;
    return nResult;
}

float GetCost()
{
    float fCost;

    std::cout << "What is the cost? $";
    std::cin >> fCost;

    return fCost;
}
void CalulateTiers(float fCost, int nType)
{
    using namespace std;

    int iii = 0;
    while(iii < 10)
    {
        int jjj = iii + 1;
        float fPrice = floor(((nProductRates[nType - 1][iii] * fCost) + fCost) * 100 + 0.5) / 100;
        cout << "Tier[" << jjj << "]: $" << fPrice << endl;
        cin.get();
        iii++;
    }
}

VS 2013 log output (minus file loc info):

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

But my result is:

Tier[1]: $1.34
Tier[2]: $1.22

then cin.get() seems to pause and work properly from there.

How do I get cin.get() to pause after every execution of the loop?

Daniel Toebe
  • 2,719
  • 3
  • 17
  • 18
  • 1
    The code you provided is not very informative. Please give a minimal example that compiles, runs, and reproduces your issue. – AndyG Nov 12 '14 at 20:26
  • Sorry new to c++, and didn't know cin had a buffer, but I added all my code. Please excuse some of the whitespace and some seemingly unnecessary includes... I am trying this out to get a feel for c++ programming. – Daniel Toebe Nov 12 '14 at 20:42
  • 1
    Try flushing that cin buffer and see what happens. – Silicomancer Nov 12 '14 at 20:43
  • 1
    possible duplicate of [cin.get() in a loop](http://stackoverflow.com/questions/15121948/cin-get-in-a-loop) – Silicomancer Nov 12 '14 at 20:45

2 Answers2

2

I can't give a definitive answer because you have not provided more code, but it seems like there was already something in your cin buffer, so it took that in the get() and continued execution.

Try flushing the buffer before you enter the loop.

SEE: How do I flush the cin buffer?

Community
  • 1
  • 1
BananasGoMoo
  • 68
  • 2
  • 10
1

Ok I added

cin.clear();
cin.ignore();

right before the while loop. Now it works as desired.

Daniel Toebe
  • 2,719
  • 3
  • 17
  • 18