C++ - 如何知道在哪里放置 cin.ignore

C++ - How to know where to put cin.ignore

本文关键字:cin ignore 在哪里 何知道 C++      更新时间:2023-10-16

我有一个来自我朋友的代码,我们不知道如何使用 cin。

我们的问题是我们正在使用 do while 并且在循环结束时,我们想要求用户输入是否要再次输入一些值,正如您将从代码本身中看到的那样。

如果答案是"y",那么他可以再次"写入",但问题是我们正在使用 getline,并且我们在这个循环中遇到第一个 getline 的问题。程序在首次使用后无法识别它。

这是代码:

int main() {
ofstream datoteka("podaci.txt", ios::app);
if (datoteka.fail()) {
cout << "Ne postojeca datoteka";
exit(1);
}
string ime;
string prezime;
char pol;
int godiste;
float prosjek;
char odluka;
do{
system("CLS");
cout << "Unesite ime: ";
getline(cin, ime);
datoteka << ime;
cout << "Unesite prezime: ";
getline(cin, prezime);
datoteka << " " << prezime;
cout << "Unesite pol(M - musko, Z - zensko): ";
cin >> pol;
datoteka << " " << pol;
cout << "Unesite godiste: ";
cin >> godiste;
datoteka << " " << godiste;
cout << "Unesite prosjek: ";
cin >> prosjek;
datoteka << " " << prosjek << endl;
cout << endl;
cout << "Da li zelite unijeti podatke za jos jednu osobu?" << endl; 
cout << "[Y] za da, [N] za ne : ";
cin >> odluka;
} while (odluka != 'N' || odluka !='n');

问题出在

getline(cin,ime);

第一次使用后它不会识别它。 有人可以帮助我吗?

基本问题是此代码混合了两种不同形式的输入。流提取器(operator>>(做格式化输入;他们跳过空格,然后尝试解释非空格字符,并在遇到与他们正在寻找的内容不符的内容时停止。当您有多个提取器时,这工作正常:std::cin >> x >> y >> z;

getline()是一个未格式化的输入函数;它抓取输入流中的任何内容,直到第一个换行符。

如果你把它们混合在一起,你可能会遇到麻烦。解决此问题的常用方法是在从格式化输入切换到非格式化输入时调用cin.ignore()的一些变体,这就是问题中的代码误入歧途的地方。

在循环结束时,代码调用std::cin >> odluka;。这会从控制台读取一个字符,并将任何其他输入保留在原处。由于控制台本身通常会等待字符,直到看到换行符,因此键入该单个字符还需要按 Enter 键,这会将换行符放入输入流中。提取器将换行符保留在那里。当循环重复时,代码调用std::getline(std::cin, ime),它会看到换行符并停止读取输入。

因此,每当您从格式化输入过渡到无格式输入时,您都必须清除先前输入工作的任何残留物。

或者,您始终可以一次读取一行,并自己解析输入。

我不确定问题到底是什么。 我拿了你的代码,它在我的机器上做了它应该做的事情(做了一些调整,以便我能理解发生了什么(:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace::std;
int main() {
ofstream datoteka("podaci.txt", ios::app);
if (datoteka.fail()) {
cout << "Ne postojeca datoteka";
exit(1);
}
string ime;
string prezime;
char pol;
int godiste;
float prosjek;
// changed it to a char pointer
char odluka[] =  { "Y" };
do{
system("CLS");
cout << "1: ";
getline(cin, ime);
datoteka << ime;
cout << "2: ";
getline(cin, prezime);
datoteka << " " << prezime;
cout << "3: ";
cin >> pol;
datoteka << " " << pol;
cout << "4: ";
cin >> godiste;
datoteka << " " << godiste;
cout << "5: ";
cin >> prosjek;
datoteka << " " << prosjek << endl;
cout << endl;
cout << "6" << endl; 
cout << "[Y], [N]: ";
cin >> odluka;
// added this. it was not waiting for my input properly.
cin.get();
// print results
system("cls");
printf("results (press any key to move on)nn1: %sn2: %sn3: %cn4: %dn5: %.02fn6: %cnn", ime.c_str(), prezime.c_str(), pol, godiste, prosjek, odluka);
getchar();
} while (_stricmp(odluka, "n")); // while "odluka" is not "n" or "N" (the _stricmp is not case-sensitive so they both return the same result)
system("cls");
printf("press any key to exit!n");
getchar();
getchar();
}

以下是"Podaci.txt"的输出:

test1 test1 1 1 1.1
test2 test2 2 2 2.2

考虑将来将 scanf/sprintf/printf 与 C 字符串一起使用,如果这仍然出现故障?