PropellerAds
Posted by : Unknown Saturday 17 January 2015

This program save the user name and age of person in two separate array and index them so that the data could be retrieved them when required. The program also ensures that no user name could be repeated the program structure uses function and 1-D array only.

For any query about code comment below

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

using namespace std;

int enter_name(char name[]) {
int i = 0;

cout << "Please Enter your user name.(Max chars 10. Enter \" - \" at end of your name)" << endl;

while (i < 10 && name[i-1] != '-'){
cin >> name[i];
i++;
}

cout << "Checking UserName...." << endl;

return --i;
}

void check_username(char user_name[], int start_name[], int &total_names, char name[], int &chars_input, int &end_point_last_name, int age[]) {

int start_point;
int end_point;
int chars_matched = 0;
int counter = 0;
bool name_found = 0;
int start_save = 0;
int name_found_index = 0;

for(int i = 0; i < total_names; i++){
start_point = start_name[i];
end_point = start_name[i+1];
chars_matched =0;

if(chars_input == (end_point - start_point)){
for (int j = start_point; j < end_point; j++){
if(user_name[j] == name[chars_matched]){
chars_matched++;
}
}

if (chars_input == chars_matched){
cout << "UserName is already in use" << endl;
name_found = 1;

cout << "User Name:\t";

for (int k = start_point; k < end_point; k++){
cout << user_name[k];
}

cout << endl << "Age:\t\t" << age[i];
}
}
}

start_point = start_name[total_names];
chars_matched = 0;

if(chars_input == (start_point)){
for (int j = start_point; j < (start_point + end_point_last_name); j++){
if(user_name[j] == name[chars_matched]){
chars_matched++;
}
}

if (chars_input == chars_matched){
cout << "UserName is already in use"<< endl;
name_found = 1;

cout << "User Name:\t";

for (int k = start_point; k < (start_point + end_point_last_name); k++){
cout << user_name[k];
}

cout << endl << "Age:\t\t" << age[total_names];

}
}

chars_matched = 0;

if (name_found == 0){

start_save = start_name[total_names];

cout << "User name is not in use" << endl;

cout << "Enter Your Age: ";
cin >> age[total_names];

for (int i = 0; i < chars_input; i++){
user_name[start_save] = name[i];
start_save++;
}



cout << "User name and age entered" << endl;


start_name[total_names+1] = start_name[total_names] + chars_input;

end_point_last_name = chars_input;

total_names++;
}


for (int i = 0; i < 10; i++){
name[i] = '+';
}
}

void main(){
char user_name[400] = { '+' };
int age[40] = {'0'};

int start_name[40] = { 0 };
int total_names = 0;

char name[10] = { '+' };
int chars_input = 0;
int end_point_last_name = 0;



while (true){
chars_input = enter_name(name);
check_username(user_name, start_name, total_names, name, chars_input, end_point_last_name, age);

/*
cout << total_names << endl;

for (int i = 0; i < total_names; i++){
cout << start_name[i] << "start_name i value" << endl;
}

cout<< "chars input" << chars_input << endl << "endpointlastname" << end_point_last_name << endl;

for (int i = 0; i < start_name[total_names]+ end_point_last_name; i++){
cout << user_name[i];
}

cout<< endl << "Check Array" << endl << endl;
*/

chars_input = 0;

cout << "Press any key to enter new user name";

_getch();

cout << endl << endl;

}
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

PropellerAds
PropellerAds

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -