【问题标题】:facing problem while nesting of member functions in c++在 C++ 中嵌套成员函数时遇到问题
【发布时间】:2022-01-09 04:11:23
【问题描述】:

基本上我只是想创建一个程序,在检查数字是二进制后交换二进制数。 我和 harry 一起从 youtube 频道观看了名为 code 的教程 22,他做了类似类型的代码,我想我会像他一样自己做一些事情,但有些不同

这是我写的代码

#include <iostream>
#include <string>
using namespace std;

// A program to check binary number and interchange 0's and 1's 

class binarychange{
   string s;

   public:
       void read(void);
       void chk_bin(void);
       void change(void);
       void display(void);
};

void binarychange :: read(void){
    cout<<"enter a binary number"<<endl;
    cin>>s;
}

void binarychange :: chk_bin(void){
    for (int i = 0; i < s.length(); i++)
    {
        if(s.at(i)!='0' && s.at(i)!='1'){
            cout<<"Invalid binary format"<<endl;
        }
    }
        
}

void binarychange :: change(void){
    for (int i = 0; i < s.length(); i++)
    {
       if (s.at(i)==0)
       {
           s.at(i)=1;
       }
       else{
           s.at(i)=0;
       }
    }
    
}

void binarychange :: display(void){
    cout<<"Displaying your binary number"<<endl;
    for (int i = 0; i < s.length(); i++)
    {
        cout<<s.at(i);
    }
    cout<<endl;
}

int main(){
   binarychange b;
   b.read();
   b.chk_bin();
   b.display();
   b.change();
   b.display();



    return 0;
}

我得到的输出

enter a binary number
101
Displaying your binary number
101
Displaying your binary number
                               

我想要的输出

enter a binary number
101
Displaying your binary number
101
Displaying your binary number
010

【问题讨论】:

  • 这并没有解决问题,但是在这两个for 循环中,您知道 i 是字符串的有效索引,因为您编写了它这样,您就无需使用s.at(i) 进行检查。只需使用索引运算符:s[i]

标签: c++ c-strings


【解决方案1】:

s.at(i) 的值是一个字符,而不是一个整数,所以你应该在binarychange :: change 方法中测试s.at(i)=='0',而不是s.at(i)==0。因此,您应该分别分配'1''0' 而不是10

【讨论】:

    【解决方案2】:

    嘿伙计们,我得到了答案,愚蠢的我只是被一个小错误击中

    正确的代码应该是

    #include <iostream>
    #include <string>
    using namespace std;
    
    // A program to check binary number and interchange 0's and 1's 
    
    class binarychange{
       string s;
    
       public:
           void read(void);
           void chk_bin(void);
           void change(void);
           void display(void);
    };
    
    void binarychange :: read(void){
        cout<<"enter a binary number"<<endl;
        cin>>s;
    }
    
    void binarychange :: chk_bin(void){
        for (int i = 0; i < s.length(); i++)
        {
            if(s.at(i)!='0' && s.at(i)!='1'){
                cout<<"Invalid binary format"<<endl;
                exit(0);
            }
        }
            
    }
    
    void binarychange :: change(void){
        for (int i = 0; i < s.length(); i++)
        {
           if (s.at(i)=='0')
           {
               s.at(i)='1';
           }
           else{
               s.at(i)='0';
           }
        }
        
    }
    
    void binarychange :: display(void){
        cout<<"Displaying your binary number"<<endl;
        for (int i = 0; i < s.length(); i++)
        {
            cout<<s.at(i);
        }
        cout<<endl;
    }
    
    int main(){
       binarychange b;
       b.read();
       b.chk_bin();
       b.display();
       b.change();
       b.display();
    
    
    
        return 0;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-02
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多