小编hak*_*ata的帖子

cin和getline跳过输入

早些时候我发布了一个关于cin跳过输入的问题,我得到了刷新和使用的结果istringstream,但现在我尝试了所有可能的解决方案,但它们都没有工作.

这是我的代码:

void createNewCustomer () {
    string name, address;

    cout << "Creating a new customer..." << endl;
    cout << "Enter the customer's name: "; getline(cin, name);
    cout << "Enter the customer's address: "; getline(cin, address);

    Customer c(name, address, 0);
    CustomerDB::addCustomer(c);

    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

但我仍然得到同样的东西,跳过输入,当它确实需要输入时,它需要它们并且名称中的存储空白,并且在地址中它采用我在名称中写的但是从第二个字母到结尾

我的代码出了什么问题?

我尝试了cin.ignore(),cin.get()cin.clear()所有的人一起,独自一人,没有一次成功

编辑:

main.cpp中的main方法mainMenu()只调用

void mainMenu () {
    char choice;

    do {
        system("cls");
        mainMenuDisplay();
        cin >> choice;
        system("cls");

        switch (choice) {
            case '1': …
Run Code Online (Sandbox Code Playgroud)

c++ input cin getline

42
推荐指数
2
解决办法
11万
查看次数

在ArrayList中添加foreach循环时,ConcurrentModificationException

我正在尝试使用arraylist的foreach循环,但是当我使用它时,它会给我错误,但是当我使用普通for循环时,它完美地工作,可能是什么问题?

代码在这里:

for (Pair p2 : R) {
    if ((p2.getFirstElm() == p.getSecondElm()) && (p2.getFirstElm() != p2.getSecondElm())) 
        R.add(new Pair (p.getFirstElm(), p2.getSecondElm()));
    else if ((p2.getSecondElm() == p.getFirstElm()) && (p2.getFirstElm() != p2.getSecondElm())) 
        R.add(new Pair (p2.getFirstElm(), p.getSecondElm()));

    // else
    // There are no transitive pairs in R.
}
Run Code Online (Sandbox Code Playgroud)

这是不起作用的循环,这是一个有效的循环:

for (int i = 0; i < R.size(); i++) {
    if ((R.get(i).getFirstElm() == p.getSecondElm()) && (R.get(i).getFirstElm() != R.get(i).getSecondElm())) 
        R.add(new Pair (p.getFirstElm(), R.get(i).getSecondElm()));
    else if ((R.get(i).getSecondElm() == p.getFirstElm()) && (R.get(i).getFirstElm() != R.get(i).getSecondElm())) 
        R.add(new Pair (R.get(i).getFirstElm(), p.getSecondElm())); …
Run Code Online (Sandbox Code Playgroud)

java foreach for-loop arraylist

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

从迭代器返回对象的引用

我想从向量返回一个对象的引用,该对象在一个迭代器对象中.我怎样才能做到这一点?

我尝试了以下方法:

Customer& CustomerDB::getCustomerById (const string& id) {
    vector<Customer>::iterator i;
    for (i = customerList.begin(); i != customerList.end() && !(i->getId() == id); ++i);

    if (i != customerList.end())
        return *i; // is this correct?
    else
        return 0; // getting error here, cant return 0 as reference they say
}
Run Code Online (Sandbox Code Playgroud)

在代码中,customerList是客户的向量,函数getId返回客户的id.

是对的*i吗?我怎么能返回0或null作为参考?

c++ null iterator reference vector

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

NSException和NSError自定义异常/错误

我最近开始学习Objective-C,我正在开发一个iOS应用程序作为练习,无论如何,我想通过抛出异常来处理溢出(我来自Java背景),我只搜索引用NSException,但后来我阅读有关异常处理主题的部分,他们说要使用NSError,我读了参考但他们有相同的协议和方法,那么它们之间的区别是什么?哪个更好?

另外,我想创建自己的异常或错误类,是否应该包含任何方法或字段?(就像Exception在Java中实现接口时一样).谢谢

exception objective-c nserror nsexception

12
推荐指数
1
解决办法
8290
查看次数

zip和解压缩文件c ++

我想编写将文件夹压缩到.zip文件的代码,或者将.zip文件解压缩到文件夹中.我相信我需要一些支持它的图书馆,对吗?如果是这样,我应该使用特定的库吗?我想在Linux机器(Ubuntu)上用C++编写代码.谢谢您的帮助.

c++ linux zip unzip

12
推荐指数
4
解决办法
2万
查看次数

c ++枚举并使用它们从用户输入

我的代码中有一个枚举如下:enum Status {In-Active, Active};.状态对象Person作为参数传递给对象,因此我希望用户输入Active或In-Active并将它们保存在Status变量中.我怎样才能做到这一点?我一开始尝试了它,但它没有用.

这是我的代码:

#include <iostream>

using namespace std;

enum Status {InActive, Active};

class Person {
    private:
        string name;
        int age;
        Status status;

    public:
        Person (const string &_name, const int _age, const Status _status) : name(_name), age(_age), status(_status) {}

        string &getName () { return name; }
        int getAge () { return age; }
        Status getStatus () { return status; }

        void setName (const string &_name) { name = _name; }
        void setAge (const int _age) …
Run Code Online (Sandbox Code Playgroud)

c++ enums input

11
推荐指数
1
解决办法
7958
查看次数

将对象转换为作为参数传递的类类型

我有一个父类和2个子类.我正在尝试实现一个函数,该函数将子类型和子项作为参数.

当我使用时child.newInstance(),我想将它存储在传递的类型的变量中,并从第二个参数调用一个函数.

以下是课程

public class Parent {
    public void test() {
        System.out.println("Test from parent");
    }
}

public class ChildA extends Parent {
    public void testChildA() {
        System.out.println("Test from child a");
    }
}

public class ChildB extends Parent {
    public void testChildB() {
        System.out.println("Test from child b");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我试图实现的方法

public class Driver {
    Parent func(Class child, String whichChild) throws Exception {
        // whichChild: "ChildA" or "ChildB"

        Object obj = child.newInstance();
        // cast obj to type of child and …
Run Code Online (Sandbox Code Playgroud)

java generics class

6
推荐指数
2
解决办法
1万
查看次数

构造函数处理异常并使用此关键字Java

我的类有两个构造函数,一个接受File对象,另一个接受一个String对象,我想使用this关键字.实现的函数是带有Fileas参数的函数和带有String将调用的函数this.现在我想在构造函数中检查异常,String但是我得到了错误,那this应该是第一行.如何检查错误然后调用this.

这是我的代码:

public Test (String filename) {
    if (filename == null)
        throw new NullPointerException("The String you entered is null.");
    if (filename.isEmpty())
        throw new IllegalArgumentException("The String you entered is empty.");

    this(new File(filename)); // error
}

public Test (File f) {
    /* implementation here */
}
Run Code Online (Sandbox Code Playgroud)

这是确切的错误: Constructor call must be the first statement in a constructor

java constructor exception this

5
推荐指数
1
解决办法
1404
查看次数

从java代码获取cmd命令的输出

我有一个程序,我可以从我的代码成功执行cmd命令,但我希望能够从cmd命令获取输出.我怎样才能做到这一点?

到目前为止我的代码是:

Second.java:

public class Second {
    public static void main(String[] args) {
        System.out.println("Hello world from Second.java");
    }
}
Run Code Online (Sandbox Code Playgroud)

和Main.java

public class Main {
    public static void main(String[] args) {
        String filename = args[1].substring(0, args[1].length() - 5);
        String cmd1 = "javac " + args[1];
        String cmd2 = "java " + filename;

        Runtime r = Runtime.getRuntime();
        Process p = r.exec(cmd1); // i can verify this by being able to see Second.class and running it successfully
        p = r.exec(cmd2); // i need …
Run Code Online (Sandbox Code Playgroud)

java windows cmd exec runtime.exec

5
推荐指数
1
解决办法
1万
查看次数

Java Stream GroupBy 和Reduce

我有一个 Item 类,其中包含代码、数量和金额字段,以及可能包含许多项目(具有相同代码)的项目列表。我想按代码对项目进行分组并汇总它们的数量和金额。

我能够使用流groupingBy和实现一半的目标reduce。分组有效,但减少是将所有分组的项目减少为一个在不同代码(groupingBy键)上重复的单个项目。

不应该在此处减少减少地图中每个代码的项目列表吗?为什么它要为所有人重新调整相同的组合项目。

下面是示例代码。

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.Map;

class HelloWorld {
    public static void main(String[] args) {
        List<Item> itemList = Arrays.asList(
            createItem("CODE1", 1, 12),
            createItem("CODE2", 4, 22),
            createItem("CODE3", 5, 50),
            createItem("CODE4", 2, 11),
            createItem("CODE4", 8, 20),
            createItem("CODE2", 1, 42)
        );
        
        Map<String, Item> aggregatedItems = itemList
            .stream()
            .collect(Collectors.groupingBy(
                Item::getCode,
                Collectors.reducing(new Item(), (aggregatedItem, item) -> {
                    int aggregatedQuantity = aggregatedItem.getQuantity();
                    double aggregatedAmount = aggregatedItem.getAmount();
                    
                    aggregatedItem.setQuantity(aggregatedQuantity + item.getQuantity());
                    aggregatedItem.setAmount(aggregatedAmount + item.getAmount()); …
Run Code Online (Sandbox Code Playgroud)

java reducing java-stream collectors groupingby

5
推荐指数
1
解决办法
5399
查看次数