0

I'm having problem with the string input, the program stops running whenever i enter a string and also if possible kindly fix my statements on the records because they don't seem to work well. Also, how do i delete a string? I tried to replace it with 'none' here for a sample. Thank you in advance!

Here is my code:

#include <iostream>
using namespace std;

int main(){

    //Initializing
    char choice;
    int i, j, record=0, totalrecord;
    int id[record];
    string name[record];
    double price[record];
    bool back=true;
    string none;


    //Menu
    while(back=true){   
        cout<<"*******************************"<<endl;
        cout<<"[A] Add Record"<<endl;
        cout<<"[V] View Record"<<endl;
        cout<<"[E] Edit Record"<<endl;
        cout<<"[D] Delete Record"<<endl;
        cout<<"[L] View All Record"<<endl;
        cout<<"Enter your choice and press return: "<<endl;

        cin >> choice;

        switch (choice){

            //Add Record
            case 'a':
            case 'A':
            record++;
            cout<<"Input ID: ";
            cin>>id[record];
            cout<<"Input Name: ";
            cin>>name[record];
            cout<<"Input Price: ";
            cin>>price[record];
            break;

            //View Record
            case 'v':
            case 'V':
            cout<<"Enter ID you wish to view: ";
            cin>>id[record];
            cout<<"ID       Name        Price"<<endl;
            cout<<id[record]<<"         "<<name[record]<<"      "<<price[record]<<endl;
            break;

            //Edit Record
            case 'e':
            case 'E':
            cout << "Enter ID you wish to edit: ";
            cin>>id[record];
            cout<<"Input NEW name: ";
            cin>>name[record];
            cout<<"Input NEW price: ";
            cin>>price[record];
            break;

            //Delete Record
            case 'd':
            case 'D':
            cout << "Enter ID you wish to delete";
            cin>>id[record];
            id[record]=0;
            name[record]=none;
            price[record]=0;
            break;

            //View All Records
            case 'l':
            case 'L':
            cout<<"ID       Name        Price"<<endl;
            for(i=1; i<totalrecord+1; i++){
                cout<<id[i]<<"      "<<name[i]<<"       "<<price[i]<<endl;
            }
            break;

            //Exit Program if invalid input
            default:
            back=false;
            break;
        }
    }
    return 0;
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mark
  • 1
  • 1
  • 1
  • You have some pretty major issues, the most glaring of which is writing past the end of your arrays (pretty much everywhere you use `record`). Pick up a good [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and study how array work (specifically what the valid indexes are). – Stephen Newell Apr 24 '18 at 05:08
  • My compiler did not compile your code, because it is not valid! `int id[record];` is not allowed in c++. Maybe you use a language extension with your compiler. And having a length of zero is logically broken code! Please start reading a c++ beginner book, what arrays are good for and how to use container classes. – Klaus Apr 24 '18 at 05:37

1 Answers1

1

Well, one problem is that here:

int i, j, record=0, totalrecord;
int id[record];
string name[record];
double price[record];

You are trying to create variable length arrays, which isn't supported in C++. Not only that, but even if it were supported you would be creating an array of size 0, since record = 0.

Thus, you need a compile-time constant value for the size of the array that isn't 0.

constexpr std::size_t RECORD_SIZE = 100; // make this any other that isn't less than or equal to 0

Then, you can create your array like this

int id[RECORD_SIZE];
string name[RECORD_SIZE];
double price[RECORD_SIZE];

However, one problem with this strategy is that if you want have an amount of records that exceed the RECORD_SIZE since you can't resize the array. Thus, I recommend that you see my final point.

Your logic when viewing, editing, and deleting a record is also incorrect since you always access an invalid index as the value of record will point to an empty slot so long as the array isn't full. Not only that, but there is no error checking.

Nonetheless, I assume you wanted to do those operations based on an index.

std::size_t index = 0;
std::cin >> index;

Then for the viewing and editing operations you would use that index to manipulate the records.

For the deleting operation, you would have to shift the records that are to the right of the deletion point left.

However, for these tasks I'd ultimately recommend that you use std::vector because it has support for all the operations you require and because it has a much greater dynamic capacity.

Rabster
  • 933
  • 1
  • 7
  • 11