小编ole*_*ard的帖子

C++检查整数.

C++新手.在处理错误时正确循环问题.我试图检查用户输入是否是整数,并且是正数.

do{
    cout << "Please enter an integer.";
    cin >> n;

    if (cin.good())
    {
        if (n < 0) {cout << "Negative.";}
        else {cout << "Positive.";}
    }
    else
    {
        cout << "Not an integer.";
        cin.clear();
        cin.ignore();
    }
}while (!cin.good() || n < 0);

cout << "\ndone.";
Run Code Online (Sandbox Code Playgroud)

输入非整数时,循环中断.我觉得像我误解的固有使用cin.clear()cin.ignore()的状态和cin这个循环过程中.如果我删除了cin.ignore(),循环变得无限.为什么是这样?我该怎么做才能使它成为一个优雅的循环?谢谢.

c++ cin

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

干掉只有const不同的代码

有没有办法干这个(没有宏)?

void clear_tp_vec(vector<const Tk*>& tps){
    for(auto it=tps.begin();it!=tps.end();++it ){
        const ValT* vp=dynamic_cast<const ValT*>(*it);
        if(vp!=NULL) 
            delete vp;
    }
    tps.clear();
};

void clear_tp_vec(vector<Tk*>& tps){
    for(auto it=tps.begin();it!=tps.end();++it ){
        ValT* vp=dynamic_cast<ValT*>(*it);
        if(vp!=NULL) 
            delete vp;
    }
    tps.clear();
};
Run Code Online (Sandbox Code Playgroud)

第二个重载唯一不同的是const限定符(缺少它).注释掉任一版本都会破坏我的代码.

c++ const dry

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

cin for int输入一个char会导致应该检查输入的循环变为疯狂

这是我的游戏的功能,它会要求输入和cin到"iAuswahl"!然后while循环检查它是否是我想要1-9的值之一,如果不是它激活并且应该要求新输入.巫婆为int做的.但是,如果我输入一个类似r的字符,它会变得疯狂,只是继续给我回来我的cout并跳过cin!我的问题是它为什么这样做,我如何阻止它?

void zug(string sSpieler, int iDran){
    int iAuswahl;
    char cXO = 'O';

    if (iDran == 1)
    {
        cXO = 'X';
    }

    cout << sSpieler << ", Sie sind am Zug. Bitte waehlen sie eins der Felder.\n" << endl;
    grafik();
    cout << "Sie sind >> " << cXO << " <<." << endl;
    cin >> iAuswahl;
    cout << endl;

    while ( 
        iAuswahl != 1 
        && iAuswahl != 2 
        && iAuswahl != 3 
        && iAuswahl != 4 
        && iAuswahl != 5 
        && …
Run Code Online (Sandbox Code Playgroud)

c++ int buffer cin char

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

整数指针指向常量整数

我想知道内部发生了什么以及它与显示的值的关系.代码是:

# include <iostream>

int main(){
    using namespace std;
    const int a = 10;

    int* p = &a;  //When compiling it generates warning "initialization from int* to 
              //const int* discard const -- but no error is generated

    cout << &a <<"\t" << p <<endl; //output: 0x246ff08 0x246ff08 (same values)
    cout << a << "\t" << *p << endl; //output: 10 10

    //Now..
    *p = 11;
    cout << &a <<"\t" << p <<endl; //output: 0x246ff08 0x246ff08 (essentially, 
                               //same values and same …
Run Code Online (Sandbox Code Playgroud)

c++

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

使用 fstream 的 C++ 未定义引用

所以我不得不在几分钟内为 TSP 类型的问题快速编写代码,但是当我试图读入文件时,我的编译器吓坏了。这是示例代码,导致此错误的原因是什么?

#include <cstdlib>
#include <iostream>
#include <fstream> 
using namespace std;

int tour_cost (int start, int end, int* array, int* tour) //calculate tour cost
{
    int cost = 0;
    for (int i = start; i < end; i++) 
        cost += array[tour[i] * tour[i] + tour[i + 1]];
    return cost;
}

int main()   
{
    int array [625];
    int tour [25];
    int dist = 0;   
    ifstream infile ("cities.txt");
    for (int i = 0; i < 25; i++) {
        for (int j …
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
1891
查看次数

c ++ 11 vs c ++ - 枚举差异

//c++03
enum Something
{
    S1 = 0,
    S2,
    SCOUNT
};
int arr[SCOUNT];

//c++11
enum class Something
{
    S1 = 0,
    S2,
    SCOUNT
};
int arr[(int) Something::SCOUNT];
Run Code Online (Sandbox Code Playgroud)

在这种情况下如何在不将枚举计数转换为int的情况下使用枚举?

c++ enums c++11 strongly-typed-enum

3
推荐指数
2
解决办法
462
查看次数

为什么我的while循环结束?

无论我把Y还是N都没关系,在我回答"更多的肉?"之后,我的程序结束了.我期待它将响应返回到循环.

#include <iostream>
using namespace std;
int main()
{
    char response = 'y';
    double price;
    double total = 0;
    while (response == 'Y' || 'y') {

        cout << "Please enter price of meat: ";
        cin >> price;

        total += price;

        cout << "More meat? (Y/N)";
        cin >> response;
        return response;
    }
    cout << "Your total is: " << total;

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

c++ loops return response while-loop

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

12天的圣诞节C计划

所以我自己尝试了12天的圣诞节编码.我还没有完成歌词,我还在努力弄明白.但我不明白为什么我的圣诞节"第一天"加倍并与另一份礼物合作,并且在第12天,没有礼物出现.我检查了我的开关盒,他们似乎是对的.是否有可能减少我的代码打印出完整的歌词?

#include <stdio.h>
#include <conio.h>

int main() // Main Function
{
    int days, counter, num;
    //int counter = 1;  

    printf("\t\t***TWELVE DAYS OF CHRISTMAS***\n");
    printf("\t\t______________________________\n\n\n");

    for (counter=0; counter<=12; counter++)
    {   
        // counter++;
        switch(counter)
        {
            case 1: printf("\t\tA Partridge in a Pear Tree\n");break; // Day 12
            case 2: printf("\t\tTwo Turtle Doves\n"); break;
            case 3: printf("\t\tThree French Hens\n"); break;
            case 4: printf("\t\tFour Calling Birds \n"); break;
            case 5: printf("\t\tFive Golden Rings\n"); break;
            case 6: printf("\t\tSix Geese a Laying\n"); break;
            case 7: printf("\t\tSeven Swans …
Run Code Online (Sandbox Code Playgroud)

c c++

2
推荐指数
2
解决办法
5898
查看次数

Mockito和jdbc程序调用

我正在尝试使用mockito编写一些jdbc过程调用的单元测试.这是我第一次用模拟对象(mockito)编写测试.

我试图测试的方法看起来像这样......

 public void deleteData(final Connection connection, final AnObject ) {
     CallableStatement statement = null;

     statement = connection.prepareCall("{call DEL_DATA(?)}");
     statement.setInt(1, object.getId());

     statement.executeUpdate();

     connection.commit();

     DatabaseSql.close(statement);
}
Run Code Online (Sandbox Code Playgroud)

如何使用mockito和junit测试这样的方法?

提前致谢.

junit jdbc mockito

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

重载>>运算符给我运行时错误C++

谁能帮我.下面是我正在尝试执行的代码.没有编译时错误,但是当控件转到字符串复制语句时程序崩溃.我试图修复它差不多一个小时,但仍然没有成功.

#include <iostream>

using namespace std;

class test
{
      private:
              char* name;
      friend istream& operator >>(istream&, test&);
};

istream& operator >> (istream& is, test& t)
{
          char c[20];

          cout << "enter something";
          is >> c;
          strcpy(t.name, c);
          return is;
}

int main()
{
    test obj;
    cin >> obj;

}
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

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

c ++到c#for语句

unsigned long uds_calc_key(unsigned long seed)
{
    unsigned long temp;
    unsigned short int  index;
    unsigned short int  mult1;
    unsigned short int  mult2;

    if(seed == 0)
    {
        seed = NC_DEFAULT_SEED;
    }
    else
    {}

    for (index=0x5D39, temp=0x80000000; temp; temp>>=1)
    {
        if (temp & seed)
        {
            index = croshortright(index, 1);
            if (temp & NC_UDS_KEYMASK)
            {
                index ^= 0x74c9;
            }
        }
    }

    mult1 = (nc_uds_keymul[(index>>2) & ((1<<5)-1)]^index);
    mult2 = (nc_uds_keymul[(index>>8) & ((1<<5)-1)]^index);
    temp = (((unsigned long)mult1)<<16)|((unsigned long)mult2);
    temp = mulu32(seed,temp);
    return temp;
}
Run Code Online (Sandbox Code Playgroud)

我不明白如何在for语句中转换该临时值.我将临时值定义为uint …

c# c++

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

为什么这个C++代码不应该工作?

我正在使用Robert Lafore的书(OOP with C++)学习C++.在本书中,我遇到了这个例子:

#include <iostream>
#include <conio.h>

using namespace std;

void main ()
{
    int numb;

    for ( numb = 1 ; numb <= 10 ; numb++ )
    {
        cout << setw(4) << numb;
        int cube = numb*numb*numb;
        cout << setw(6) << cube << endl;
    }

    getch();
}
Run Code Online (Sandbox Code Playgroud)

变量'cube'已在循环体内声明为int'.

int cube = numb*numb*numb;
Run Code Online (Sandbox Code Playgroud)

由于循环迭代10次,变量'cube'也将被声明10次.无论迭代次数如何,'cube'都可以在循环体内访问.所以,当我们进入第二次迭代的循环体时,'cube'已经知道了(因为它已经在第一次迭代中声明了),而'cube'的另一个声明应该给出"重新定义"的错误.但相反,它成功构建并调试没有问题.为什么?

c++ debugging loops

1
推荐指数
4
解决办法
427
查看次数

为什么这个程序没有编译

#include <iostream>
class Abc // created class with name Abc
{
    void display()//member function
    {
        cout<<"Displaying NO."<<endl;
    }
};
int main()
{
    Abc obj;//creating object of the class
    obj.display();//calling member function inside class
}
Run Code Online (Sandbox Code Playgroud)

它返回错误为

main.cpp: In function 'int main()':
main.cpp:5:10: error: 'void Abc::display()' is private
     void display()
          ^
main.cpp:13:17: error: within this context
     obj.display();
                 ^
Run Code Online (Sandbox Code Playgroud)

我试图使显示功能,public int main但它给出了错误

main.cpp:5:11: error: expected ':' before 'void'
    public void display()
           ^
Run Code Online (Sandbox Code Playgroud)

c++

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