小编Iam*_*man的帖子

错误输入的 std::cin 无限循环

在我下面的代码中,我想循环直到用户提供正确的输入。但是当我尝试时,它变成了一个不间断的循环。
Please Enter Valid Input.
如果没有 while 循环,它也是一样的。

这里有while循环:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;

class library {
public:
    library() {
        int mainOption;

        cout<<"Please choose the option you want to perform."<<endl;
        cout<<"1. Member Section"<<"\n"<<"2. Books, Lending & Donate Section"<<"\n"<<"3. Returning Section"<<endl;
        bool option=true;
        while (option==true) {
            cin>>mainOption;
            if (mainOption==1) {
                cout<<"section 1"<<endl;
                option=false;
            } else if (mainOption==2) {
                cout<<"section 1"<<endl;
                option=false;
            } else if (mainOption==3) {
                cout<<"section 1"<<endl;
                option=false;
            } else {
                cout<<"Please …
Run Code Online (Sandbox Code Playgroud)

c++ cin

3
推荐指数
1
解决办法
530
查看次数

If和Else条件都执行C++

我有一个从文本文件中删除数据的功能.

输入有效输入时遇到问题:

  • 如果输入错误的输入,它只会else按预期执行该部分
  • 如果我输入有效输入,它将执行ifelse部分.

这是我的代码:

void Member::processTransaction()
{
    fstream f;

    f.open("Member.txt",ios::in|ios::out);

    int x = 0, y = 0, z = 0;

    while (!f.eof())
    {
        f >> idList[x] >> nameList[y];

        if (id == idList[x])
        {
            cout << idList[x] << "\t" << nameList[x] << endl;

            cout << "Do you want to delete the entry (Y/N) : " << endl;

            char deleteEntry = getche();

            if(deleteEntry=='Y'||deleteEntry=='y')
                deleteInformation();  

            f.close();
        }
        else
        {
            cout << "No Matches Found!";
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
753
查看次数

字符串在C++中为整数

我有这样的字符串值: 2014-04-14

我想将此转换为Integer 20140414

我知道字符串输入到Integer可以这样做

std::string myString = "45";
int value = atoi(myString.c_str()); //value = 45
Run Code Online (Sandbox Code Playgroud)

但我不知道如何删除-标志.

c++ string integer type-conversion

-1
推荐指数
1
解决办法
132
查看次数

标签 统计

c++ ×3

cin ×1

integer ×1

string ×1

type-conversion ×1