当循环时,在c++中的cin.get()之前循环两次
While Loop, Loops twice before cin.get() in c++
每次我都试图使用cin.get()来暂停循环。
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日志输出(减去文件位置信息):
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
但我的结果是:
Tier[1]: $1.34
Tier[2]: $1.22
然后cin.get()似乎会暂停并从那里正常工作。
如何在每次执行循环后让cin.get()暂停?
我不能给出一个明确的答案,因为你还没有提供更多的代码,但你的cin缓冲区中似乎已经有了一些东西,所以它在get()中接受了这些并继续执行。
在进入循环之前,请尝试刷新缓冲区。
SEE:如何冲洗cin缓冲液?
好的,我添加了
cin.clear();
cin.ignore();
就在while循环之前。现在它按要求工作。
相关文章:
- 我用C++做了一个计算器,但我对"type of calculator"的要求循环了两次。有人可以帮助指出我的错误吗?
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 两次相同的 for 循环:一个编译,另一个不编译
- while 循环在一个函数调用中执行两次
- C++ 为什么我不能使用循环:while(fin>>x) 两次
- 如何防止字符在 for 循环中打印两次?C++
- 做 同时循环重复 cout 线 每循环两次
- 在 for 循环(链表)中删除两次后,变量不可用
- C++ cout 打印两次在循环中做
- c++While循环打印couts两次
- 当循环应该只运行一次时,执行两次while循环(C++)
- 如何循环两次,将不同的值插入两个不同的堆栈
- c ++ 如何阻止我的循环两次吐出最后一个输入
- 当循环时,在c++中的cin.get()之前循环两次
- C++程序 尝试连续运行两次写入循环时显示".. stopped working"错误
- 通过两次链接同一个库来解决循环依赖关系
- 一旦用户错了两次,就结束循环
- 在循环重复两次之前,break 语句不起作用
- 为什么使用while(!input.eof())循环两次不工作
- 在c++中,循环两次比循环一次要快