-1

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 :)

  • Have you learned about `struct` or `class`? Have you learned about `std::vector`? – Eljay Jan 16 '22 at 13:47
  • Please check your indentation, and use an appropriate style, to make your code more readable. Also c++ isn't python, just indenting lines under an `if` statement doesn't work you'll need to put those lines in a block of `{}` braves. – πάντα ῥεῖ Jan 16 '22 at 13:49
  • You are allowed to use arrays but not pointers?! – Wais Kamal Jan 16 '22 at 13:49
  • yeah smth like that – MasonSkellon Jan 16 '22 at 14:02
  • also sorry to the second comment but i'm new and i've been at this for like 8 or more hours from morning and i currently can't do much organizing in it – MasonSkellon Jan 16 '22 at 14:03
  • first comment no we have not – MasonSkellon Jan 16 '22 at 14:04
  • I expect you will need to provide an array that has excess capacity, and something that tracks how much of that capacity has been used. The right thing to do is use a `std::vector`, but since you cannot that'd be a reasonable compromise for an exercise program. – Eljay Jan 16 '22 at 14:30
  • and how do i use vectors? we havent learned that yet. I don't even know what pointers are and how they are used in arrays. it's been like 4 months since i first started coding. and this is the major thing that i need to do my friend has done this with pointers. while another just asked his friend to do it and both of them have different methods. i looked at both their codes to just get an idea and i was going pretty well until this roadblock. i'm thinking of skipping the name display in the list and add records but then The names have basically no use :( – MasonSkellon Jan 16 '22 at 14:35
  • and i forogt to add another thing he also said to make a search feature too. from what i understand i think he means that when i enter the id or name and it shows up or just says user exists – MasonSkellon Jan 16 '22 at 14:36

1 Answers1

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • the main() i have written it in the rest of my code which i havent shared – MasonSkellon Jan 16 '22 at 14:00
  • Secnd i mean i think i made a dynamic array at least in the case of integers it doesnt work with strings tho – MasonSkellon Jan 16 '22 at 14:01
  • @MasonSkellon You haven't made a dynamic array. – eerorika Jan 16 '22 at 14:03
  • well not in that sense but in switch statement if you look at exnum++ at the end of case1. well it does register and even when seeing it the array does add the ID i put in but not the name. it shows empty there. that was what i was having a problem with – MasonSkellon Jan 16 '22 at 14:09
  • @MasonSkellon `if you look at exnum++ at the end of case` That increments the integer variable and has no effect on the size of any array. `well it does register and even when seeing it the array does add the ID` No, it doesn't. The behaviour of the program is undefined. – eerorika Jan 16 '22 at 14:10
  • at first the program would just end but my friend suggested me to put 'exid[exnum-1]=addid' and 'exname[exnum-1]=addname]' and to the password one as well. so now i at least get the choice screen again but when viewing the list only the id shows and the name space is blank – MasonSkellon Jan 16 '22 at 14:13
  • so what can i do to fix this problem? like what can i do to add names to the string arrays – MasonSkellon Jan 16 '22 at 14:14
  • @MasonSkellon `so what can i do to fix this problem?` First step is to read through all the compiler errors and warnings and get the program to compile: https://godbolt.org/z/o5q3on6s6 One step at a time. – eerorika Jan 16 '22 at 14:16
  • i use devcpp for this and it doesnt show me errors when compiling and running and i've been doing this for more than 8 hours straight. i can't do this anymore :( – MasonSkellon Jan 16 '22 at 14:19
  • https://godbolt.org/ i hope this shows my full code now – MasonSkellon Jan 16 '22 at 14:21
  • soo i tried and i can't understand these errors variables-sized objects? and the others too – MasonSkellon Jan 16 '22 at 14:39
  • @MasonSkellon This should be of help: https://stackoverflow.com/q/388242/2079303 – eerorika Jan 16 '22 at 14:48
  • Thankyou for the suggestion i will look into it. Also i removed the main() and replaced it with infinite for loop basically. I want to make a dynamic kind of array which i can for integers but not string. Also the exname[exnum-1] i said? It works but it replaces the already given Names – MasonSkellon Jan 16 '22 at 15:01
  • @MasonSkellon `I want to make a dynamic kind of array` That's what `std::vector` is for. Use `std::vector` for integers and `std::vector` for strings. – eerorika Jan 16 '22 at 15:04
  • how can i use that? so far i've looked first i need to inlude vector then they use smth like push_back and stuff and where would i be able to use this in my code? i mean before Switch statement or in the switch statement – MasonSkellon Jan 16 '22 at 15:09