小编Fru*_*der的帖子

用c ++创建一个类对象数组

嗨,大家好我想创建一个类对象数组....这样我就可以在运行时继续创建尽可能多的对象,并在需要时编写以下代码,但它给了我错误:

class contact{
public:
    string name;//ALL CLASS VARIABLES ARE PUBLIC
    int phonenumber;
    string address;

    contact(){//Constructor
        name= NULL;
        phonenumber= NULL;
        address= NULL;
    }

    void input_contact_name(string s){//function to take contact name
        name=s;
    }
    void input_contact_number(int x){//function to take contact number
        phonenumber=x;
    }
    void input_contact_address(string add){//function to take contact address
        address=add;
    }
}

int main(){
    contact *d;
    d= new contact[200];
    string name,add;
    int choice;//Variable for switch statement
    int phno;
    static int i=0;//i is declared as a static int variable
    bool flag=false;
    cout<<"\tWelcome to …
Run Code Online (Sandbox Code Playgroud)

c++

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

链接列表中的错误

我为链接列表编写了以下代码来创建一个Book的序列号.并搜索它.我正在使用链表.
当我添加我的第一个条目时,它会成功添加,但是当我添加第二个条目时,它会显示分段错误.我无法弄清楚原因.请帮忙.
提前致谢.
码:

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

struct book
{
  int accno;
  string name;
  book* next;
};



int main()

{
  bool flag=false;
  int x,m;
  string s;
  book* front=NULL;
  book* n;
  do
    {

      cout<<"\nPlease select the following:\n1.Create and append\n2.Search\n3.Exit";
      cin>>m;
      switch(m)
    {
    case 1:
      n=new book();
      cout<<"\nEnter the book name: ";
      cin>>s;
      cout<<"\nEnter the acc no.: ";
      cin>>x;

      if(front==NULL)
        {
          front=n;
        }
      else
        {   n=front;

          while(n->next!=NULL)
        {
          n=n->next;
        }
          n=n->next;
        }

      n->accno=x;
      n->name=s;
      break;


    case 2:
      cout<<"Enter …
Run Code Online (Sandbox Code Playgroud)

c++

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

c ++中的文件处理

我编写了以下代码来输入文件中的数字,但是最后一个数字被打印了两次,这让我感到困惑.什么是可能的答案.提前致谢.

码:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int x;
    ifstream f;
    f.open("note");
    while(!f.eof()){
            f>>x;
            cout<<x<<endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

给出输出:

1
2
3
4
5
5
Run Code Online (Sandbox Code Playgroud)

文件说明的内容是:

1
2
3
4
5 
Run Code Online (Sandbox Code Playgroud)

c++ file-handling

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

在c ++中接受字符串空格的问题

我写了这段代码来获取名字.一个人的电话和地址,然后我将这些输入到类对象变量中:

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

class contact{
public:
    string name;//ALL CLASS VARIABLES ARE PUBLIC
    unsigned int phonenumber;
    string address;

    contact(){//Constructor
        name= "Noname";
        phonenumber= 0;
        address= "Noaddress";
    }

    /*void input_contact_name(string s){//function to take contact name
        name=s;
    }
    void input_contact_number(int x){//function to take contact number
        phonenumber=x;
    }
    void input_contact_address(string add){//function to take contact address
        address=add;
    }*/
};

int main(){
    contact *d;
    d= new contact[200];
    string name,add;
    int choice;//Variable for switch statement
    unsigned int phno;
    static int i=0;//i is declared …
Run Code Online (Sandbox Code Playgroud)

c++ string

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

河内问题塔

嗨大家我在河内的塔上遇到了一个问题:
我们给了一堆交替颜色的圆柱体交替堆叠在一起.
我的工作是将相同颜色的两个堆栈分开,
我可以使用递归为常规的河内塔写代码(算法),但我无法弄清楚这一部分.有人可以帮忙吗?
常规河内问题的代码:

#include<iostream>

using namespace std;
int count=0;

void hanoi(char a,char b,char c,int x)
{
  if(x>1)
  {
    hanoi(a,c,b,x-1);
    hanoi(a,b,c,1);
    hanoi(c,b,a,x-1);
  }
  else
  {
    cout<<"Move a Disk from "<<a<<" to "<<b<<endl; count++;  
  }
}

int main()
{
  int n;
  cout<<"Enter the height of stack";
  cin>>n;
  hanoi('A','B','C',n);
  cout<<"\nNo. of changes done:"<<count;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×5

file-handling ×1

string ×1