如何显示我的字符串?

时间:2016-12-23 16:25:33

标签: c++ string pointers

我想知道为什么我无法使用

打印record[i][1]
puts (record[i][1]);

如果我这样做:

cout<< record[i][1]; 

然后只出现字符串的第一个字母,而不是其余字母。

这是针对学校项目的。

我在Turbo C ++(TC4)上编码。

#include< fstream.h>
#include< strstream.h>
#include< conio.h>
#include< dos.h>
#include< graphics.h>
#include< string.h>
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#include< ctype.h>
#include< iomanip.h>
#include< iostream.h>

char record[500][5]; //array to store 500 entries each consisting of
//name,cell no, agesex, crime and sentenced.

int cricode=1;

char name[35];
char criminalno[10]; 
char agesex[5];
char sentenced[3];
char crime[20];
char passwordU[30];

int checkpass(char[]);
void menu(int);
void advmenu(int); 
void inputdata();
void report(int)  ;
void searchrecN(char [][5], int);
void searchrecC(char [][5], int);
void selectcrime(char []);
void changepass();
void displaydata(int);

void main()
{
    clrscr();
    ifstream fp;
    fp.open("criminal.txt");
    do
        {char a;
            fp.getline(&record[cricode][1],30,'!')  ;  //name
            fp.getline(&record[cricode][2],10,'@');   //cellno
            fp.getline(&record[cricode][3],7,'%');  //agesex
            fp.getline(&record[cricode][4],20,'^');   //crime
            fp.getline(&record[cricode][5],3,'\n');  //sentenced
            cricode++ ;
        }while (fp.eof() == 0);

    cricode--;    //final value of cricode
    fp.close();
    menu(cricode);
    getch();
}

void menu(int code)
{
    fstream fp;
    int hell;
    clrscr();

    cout<<"\n\n";
    cout<<"______________________________________\n";
    cout<<"          L O C A L  M E N U          \n";
    cout<<"______________________________________\n";
    cout<<"\n\n";
    cout<<"\t\t\t"<<"\n\n[1] Create a Record.";
    cout<<"\t\t\t"<<"\n\n[2] Search for a record.";
    cout<<"\t\t\t"<<"\n\n[3] Advance Options.";
    cout<<"\t\t\t"<<"\n\n[4] Display all entries.";
    cout<<"\t\t\t"<<"\n\n[5] Exit       \n    :";

    cin>>hell;
    switch(hell)
        {
        case 1: clrscr();
            inputdata();
            system("pause");
            menu(code);
            break;

        case 2: clrscr();

            char na[30];
            int ch;
            clrscr();
            cout<<"\t\t\t"<<"\nSearch by ->  ";
            cout<<"\t\t\t\t"<<"\n[1] Name           : ";
            cout<<"\t\t\t\t"<<"\n[2] Cell No.   : ";
            cin>>ch;
            switch(ch)
                {
                case 1:
                    cout<<"\n Please Enter Name             : ";
                    //gets(na);
                    searchrecN(record,code);
                    break;
                case 2:
                    cout<<"\n Please Enter Cell No.    : ";
                    //gets(na);
                    searchrecC(record,code);

                    break;
                default : cout<<"invalid";
                }
            menu(code);

            break;

        case 4:   clrscr();
            for(int i=0;i<code;i++)
                {
                    cout<<"\nName \t Cellno \t age/sex \t crime \t sentenced \n";
                    cout<<record[i][1]; //only one character is displayed of the 
                    // entire word.

                    // if i do 
                    //puts(record[i][1]); // Error: cannot convert 'int' to 'const char *'
                }
            cout<<"\n";
            system("pause");
            menu(code);
            break;

        case 5: //clrscr();
                //intro();
            break;

        default:
            cout<<"\a";
        }
}

int i; 

void report(int cc)
{
    cout    <<record[cc][1]<<"\t"
                <<record[cc][2]<<"\t"
                <<record[cc][3]<<"\t"
                <<record[cc][4]<<"\t"
                <<record[cc][5]<<"\n";
}

void inputdata()
{
    char ch;
    char recname[30],reccell[10],recagex[7],recrime[20],recsent[3];

    ofstream fp;
    fp.open("criminal.txt", ios::app);

    do
        {
            cout<<"\n[.] Name                       : ";
            gets(recname);
            cout<<"\n[.] Criminal Number            : ";
            gets(reccell);
            cout<<"\n[.] Age/Sex {eg: 21/M}         : ";
            gets(recagex);
            cout<<"\n[.] Select Crime               : ";
            selectcrime(recrime);
            cout<<"\n[.] Sentenced For              : ";
            gets(recsent);

            fp  <<recname
                <<"!"<<reccell
                <<"@"<<recagex
                <<"%"<<recrime
                <<"^"<<recsent
                <<"\n";

            cout<<"Do you Want to Continue(Y/N): ";
            cin>>ch;
        }while((ch=='y')||(ch=='Y'));
    system("pause");
}

void searchrecN(char record[][5], int cricode)
{
    int cnt;
    char search[30];
    cout<<"Enter Cellno.: ";
    gets(search);

    for(i=0;i<cricode;i++)
        {
            if (strcmpi(search,&record[i][1])==0)
                {
                    clrscr();
                    cout<<"\nRecord Found.";
                    cout<<"\nLoading Info......";

                    system("pause");
                    report(i);
                    cnt=1;
                    break;
                }
        }

    if(cnt==0)
        {
            clrscr();
            cout<<"\n ***Record does not exist*** ";
            cout<<"\n ***Redirecting***\n";
            system("pause");
        }
}

void searchrecC(char record[][5], int cricode)
{  
    int cnt;
    char search[10];
    cout<<"Enter Cellno.: ";
    gets(search);

    for(i=0;i<cricode;i++)
        {
            if (strcmpi(search,&record[i][2])==0)
                {
                    clrscr();
                    cout<<"\nRecord Found.";
                    cout<<"\nLoading Info......";

                    system("pause");
                    report(i);

                    cnt=1;
                    break;
                }
        }
    if(cnt==0)
        {
            clrscr();
            cout<<"\n ***Record does not exist*** ";
            cout<<"\n ***Redirecting***\n";
            system("pause");
        }

}

void displaydata(int c)
{
    clrscr();

    fstream fp;

    for(i=0;i<c;i++)
        {
            cout<<"Name\tCellno\tage/sex\tcrime\tsentenced";
            cout    <<record[i][1]<<"\t"<<record[i][2]<<"\t"
                        <<record[i][3]<<"\t"<<record[i][4]<<"\t"
                        <<record[i][5]<<"\n";
        }
    /*  cout<<setw(100)<<"\n[.] Criminal No.          :  ";
        puts(record[i][2]);

        cout<<setw(100)<<"\n[.] Age/Sex               :  ";
        puts(record[i][3]);

        cout<<setw(100)<<"\n[.] Alleged for :         :  ";
        //  puts(obj[i].category);

        cout<<setw(100)<<"\n[.] Crime                 :  ";
        puts(record[i][4]);

        //  cout<<setw(100)<<"\n[.] Expenses per month    :  ";
        puts(record[i][4]);

        /*  cout<<setw(100)<<"\n[.] Expenses per Month    :  ";
        cout<<exp;

        cout<<setw(100)<<"\n[.] Sentenced for         :  ";
        puts(record[i][5]); */
}

2 个答案:

答案 0 :(得分:1)

您的record数组是一个包含500行 5个单个字符的数组,而不是5个字符串。

我建议您使用结构建模数据:

struct Record
{
  char name[35];
  char criminalno[10]; 
  char agesex[5];
  char sentenced[3];
  char crime[20];
  char passwordU[30];
};

您的数据库可以是500条记录:

Record database[500];

仅供参考,使用std::stringstd::vector的更安全且不太麻烦的技术。

答案 1 :(得分:0)

简短回答是record[i][1]是一个char,而不是字符串。

答案越长,你为什么使用Turbo C ++ 4 ...... ???如果可以,请使用其中一个现代编译器 - MSVC或Clang或G ++或MinGW,甚至是melpon.org/wandbox等在线编译器。

完整的答案:如果您不能使用std::string而不是char[],请使用C编程语言查找解决方案。您需要逐步浏览char[]中的每个元素并单独输出。像这样:

for(int i = 0; i < MAX_LENGTH && myCharArray[i] != '\0'; i++) {
  std::cout << myCharArray[i];
}