c++从string中读取整行
C++ reading a whole line from string
我有一个大问题…只要运行程序,点击"新枪",然后点击"手枪"。当您必须进入模型时,问题就出现了。例如,如果我输入"Desert Eagle",在文本文件中它只输出"Eagle"。真奇怪,我解不出来。
代码:#include <fstream>
#include <iostream>
#include <windows.h>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
void setcolor(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
int main (){
system("chcp 1251 > nul");
system("title GunnZone");
string gunModel;
int gunManufactureYear;
int itemNumber;
double gunWeight;
double price;
ofstream myfileHandguns("Handguns.txt", ios::app);
ofstream myfileRifles("Rifles.txt", ios::app);
ofstream myfileShotguns("Shotguns.txt", ios::app);
ofstream myfileSnipers("Snipers.txt", ios::app);
ofstream myfileGranades("Granades.txt", ios::app);
ofstream myfileExplosives("Explosives.txt", ios::app);
int choice;
cout << "1.New Gun" << endl;
cout << "2.List Guns" << endl;
cout << "3.Exit" << endl << endl;
cout << "Enter your choice: ";
cin >> choice;
if(choice == 1){
system("cls");
int gunTypeChoice;
cout << "1.Handgun" << endl;
cout << "2.Rifle" << endl;
cout << "3.Shotgun" << endl;
cout << "4.Sniper" << endl;
cout << "5.Granade" << endl;
cout << "6.Explosives" << endl << endl;
cout << "Enter your choice: ";
cin >> gunTypeChoice;
if(gunTypeChoice == 1){
system("cls");
cout << "Model: ";
cin >> gunModel;
getline(cin, gunModel);
cout << endl << endl;
cout << "Year of Manufacture: ";
cin >> gunManufactureYear;
cout << endl << endl;
cout << "Weight: ";
cin >> gunWeight;
cout << endl << endl;
cout << "Item Number: ";
cin >> itemNumber;
cout << endl << endl;
cout << "Price: ";
cin >> price;
myfileHandguns << "Model: " << gunModel << "nn";
myfileHandguns << "Year of Manufacture: " << gunManufactureYear << "nn";
myfileHandguns << "Weight: " << gunWeight << " g" << "nn";
myfileHandguns << "Item Number: " << itemNumber << "nn";
myfileHandguns << "Price: " << price << "$" << "nn";
myfileHandguns.close();
}
}
system("pause > nul");
return 0;
}
按建议删除>>
操作符。使用clear
和ignore
清除错误跳过新行字符n
cout << "Model: ";
//cin >> gunModel;
std::cin.clear();
std::cin.ignore(0xffff, 'n');
std::getline(cin, gunModel);
cout << "Testing... you entered " << gunModel << endl;
cout << endl << endl;
参见为什么我们要在读取输入后调用cin.clear()和cin.ignore() ?
相关文章:
- 如何使用 rapidjson 读取 json 文件并输出到 std::string?
- 使用 std::find 查找从二进制文件中读取的字符并转换为 std::vector 中的 std::string<string> 会产生这种不可预测的行为?
- 通过读取文件创建映射<字符串,矢量>时如何管理内存<string>
- 如何在C 中的文件中直接读取std :: String中的字符串
- 比较输入的C弦与文件读取c-string
- 我可以读取向量<string>::迭代器的数字位置吗?
- 使用 std::write 和 std::read C++ 读取包含 std::string 到.dat文件的结构
- 读取STD :: String Line(带有空格和特殊钥匙支持)
- C 从套接字读取到std :: String
- 我想从文件中读取数据块并将其存储在矢量<string> buf 中。我做错了什么?
- 读取 std::string,从 std::string 中删除所有特殊字符
- 读取 std::map<string, string> 使用 ReadProcessMemory
- 将 ASCII 文件读取到C++ std::string 中的最简单方法是什么?
- 为什么从文件输入中读取 std::string 会返回空白
- 如何在c++中读取图像文件并将其存储在内存中(std::string)
- 无法在循环中读取Char/string
- 从文件中读取数据类型string, int double.将每一行数据放入对象向量中
- 使用文件提取操作符读取int或string
- 如何从键盘中读取包含c-string, int和float的结构变量
- 如何在c++中从文件中读取string和相应的int