How To Add extra String "Names" in a String Array?
I have a Major Assignment and i was doing this but i got stuck in this one problem.
#include<iostream>
using namespace std;
choice(){
int num;
cout<<endl<<endl;
cout<<1<<". Add Records"<<endl;
cout<<2<<". List Records"<<endl;
cout<<3<<". Modify Records"<<endl;
cout<<4<<". Delete Records"<<endl;
cout<<5<<". Exit Program"<<endl;
cout<<endl<<endl<<"Select Your Choice :=> ";
}
main(){
//---------------Declaration-----------
int addid, login=0, id, exnum=4, num;
string input,password,name, addname, addpassword;
int exid[exnum]={12890,12892,12894,12896};
string expassword[exnum]={"pass1","pass2","pass3","pass4"};
string exname[exnum]={"Asim","Hammad","Shayan","Faizan"};
//--------------Login/Signup-----------
cout<<"Do you want to Login or SignUp? (Login/Signup) \n";
cin>>input; system("cls");
//---------------Login-----------------
if(input=="Login"||input=="login"||input=="LOGIN"){
cout<<"Enter your ID: "; cin>>id;
cout<<"Enter your Password: "; cin>>password;
for(int a=0; a<exnum; a++){
if(id==exid[a]){
if(password==expassword[a]){
login=1;
system("cls");
cout<<"You have logged in for the demo version "<<exname[a]<<"."<<endl<<endl;
break;}}}
if(login==0){
system("cls");
cout<<"ID or Password incorrect.";
cout<<" Try Again."<<endl<<endl; main();}
//------------Switch Statement for Choice Function------------------
while(true){
choice();
cin>>num;
system("cls");
switch(num){
case 1:{
cout<<"Enter your Name: "; cin>>addname;
cout<<"Enter your ID: "; cin>>addid;
cout<<"Enter your Password: "; cin>>addpassword;
exid[exnum]=addid;
exname[exnum]=addname;
expassword[exnum]=addpassword;
exnum++;
break;}
case 2:{
for(int b=0; b<exnum; b++){
cout<<b+1<<". ID: "<<exid[b]<<" Name: "<<exname[b]<<endl;}
break;}
}}}
Its unfinished and i haven't put the rest of it here but the main problem lies in here when adding records. i'm a 1st semester uni student fresh out of college so this is kinda hard for me idk what to do i looked alot on google but sadly got no answer and vector or stuff like that didn't help plus we have been given a restriction on using only arrays, loops and the easy stuff no pointers or including other libraries stuff like that. so it's kinda hard. can anyone help on this? just need to understand how to add names to a string array through user input. Any help will be appreciated :)
Solution 1:
How To Add extra String "Names" in a String Array?
It isn't possible to add elements to an array. The size of an array remains constant through its entire lifetime.
int addid, login=0, id, exnum=4, num;
// ...
int exid[exnum]={12890,12892,12894,12896};
The size of an array must be a compile time constant expression. As a non-const variable exnum
cannot be compile time constant; hence this array declaration is ill-formed. Same applies to your other array variables. Don't do this.
exid[exnum]=addid; exname[exnum]=addname; expassword[exnum]=addpassword;
This writes outside the bounds of the array, and the behaviour of the program is undefined. Don't do this.
main(){
This looks a bit like the beginning of a function definition, but it lacks a return type. This isn't allowed. Don't do this.
main();
::main
function must never be called. Don't do this. Write a loop, not recursion.