Hi, Guys
I am a beginner please help me.
I wrote something like this:

#include <iostream.h>
#include <string.h>
#define number 1
#define length 35
char student[number][length];
int i,j,k;
char *BlankImprover(char *);
void InsertInfos(void);

void main(){
	InsertInfos();
	  	for(i=0;i<number;i++)
	  	for(j=0;j<15;j++)
		cout<<student[i][j];
}

void InsertInfos(){
	char name[15];
	for(i=0;i<number;i++){
	cout<<"Enter Student Number <"<<i+1<<"> Informations:"<<endl;

			cout<<"Enter Student First Name(MAX: 15 Characters) : ";
			cin>>name;
			char *NBName = BlankImprover(name);
				for(j=0;j<15;j++)
					student[i][j] = NBName[j];

	}
}
char *BlankImprover(char *string){

	char *BlankPattern="###############";
	strcpy(BlankPattern,string);
		//For Deleting NULL value from String
		for(i=0;i<15;i++){
			if(BlankPattern[i]=='#')
				BlankPattern[i-1]='#';
		}
		return BlankPattern;
}

I created a global 2D array and in InsertInfos() Function Inserts a string to elements 0 to 14 of array and if length of string inserted by user is less than 15 chars the BlankImprover(char *string) Function Fills rest of string by '#'. the Regenerated String will put on array.
EX: CIN>>name -->"mahdi"
VALUE TO Fill The Array: "mahdi##########"
other elements of array Student[][] will fill later. bu the problem is this:
in main() block ,cout<<student[j]; does not outputs "mahdi##########" in first 15 elements of array and prints null value. infact the text user inserted does not exist in array. please help me! why?
what is my mistake?

Recommended Answers

All 3 Replies

line 32 and 33: That doesn't work because the string is located in read-only memory, and attempting to write to that memory will most likely fail. Correction: This puts the string in writeable memory. char BlankPattern[]="###############"; line 39: The above doesn't work either because you can not return a character array from a function. So your only choice is to allocate memory. Note that the calling function will have to call delete[] to properly destroy this string.

char* BlankPattern = new char[16];
    // copy at most 15 characters to BlankPattern
    // if [b]string[/b] is longer than that, then the remaining
    // characters are ignored.
    strncpy(BlankPattern, string, 15);
    // now fill remaining positions with '#'
    for(int n = strlen(string); i < 15; n++)
        BlankPattern[n] = '#';
    BlankPattern[15] = 0;
    return BlankPattern;
}

Also, lines 13 and 14: why not just output it as a string with one simple line: cout << student[i] << "\n"; . The j loop is completly unnecessary.

Also, lines 13 and 14: why not just output it as a string with one simple line: cout << student[i] << "\n"; . The j loop is completly unnecessary.

Thank u Very Much Mr/Mrs Moderator. My Problem Solved. It was My First Post on DaniWeb. I Studied Something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.