Thread: Index no more space

  1. #1
    Registered User
    Join Date
    May 2006
    in your head

    Index no more space

    Hi all i have a problem here in my program it is already complete it's a grocery program... I have 4 index and i want to add something there but if the index reaches 5 when i choose add again it must say there is no more space because i only have a max of 4 index.. here is my program..

    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <stdlib.h>
    using namespace std;
    struct grocery{
    char product[10], item_name[10], mfg[10], exp[10], price[10], ava[10], descp[30];
    int n=0;
    int ch, num, in;
    int a=0;
    cout <<"1. Add\n" <<"2. Delete\n"<<"3. List\n"<<"4. Edit\n" <<"5. Exit\n";
    cout <<"Choice: ";
    cin >> ch;
    case 1:
    cout <<"==============\n\n";
    cout<<"Index # " << a+1;
    cout<<"\nProduct = ";
    cin>> pro[a].product;
    cout<<"\nItem name = ";
    cin>> pro[a].item_name;
    cout<<"\nMfg Date = ";
    cin>> pro[a].mfg;
    cout<<"\nExp Date = ";
    cin>> pro[a].exp;
    cout<<"\nPrice = ";
    cin>> pro[a].price;
    cout<<"\nAvailability = ";
    cin>> pro[a].ava;
    cout<<"\nDescription = ";
    cin>> pro[a].descp;
    case 2:
       cout <<"Please wait...\n";
       cout <<"Complete!!!\n";
       for (int b=0; b<=3; b++){
       strcpy(pro[b].product, " ");
       strcpy(pro[b].item_name, " ");
       strcpy(pro[b].mfg, " ");
       strcpy(pro[b].exp, " ");
       strcpy(pro[b].price, " ");
       strcpy(pro[b].ava, " ");
       strcpy(pro[b].descp, " ");
       cout << endl;
    case 3: for (int c=0; c<=3; c++){
       cout <<"==============\n";
       cout <<"Index # " << c+1;
       cout <<"\n";
       cout <<"Product = "<< pro[c].product << "\n";
       cout <<"item name = "<< pro[c].item_name <<"\n";
       cout <<"Mfg Date = "<< pro[c].mfg << "\n";
       cout <<"Exp Date = "<< pro[c].exp << "\n";
       cout <<"Price = "<< pro[c].item_name <<"\n";
       cout <<"Availability = "<< pro[c].mfg << "\n";
       cout <<"Description = "<< pro[c].exp << "\n";
       cout <<endl;
    case 4: cout << "index to edit: ";
    cin>> in;
    if (in>=5){
       cout <<"no such index!!!\n";
       cout <<"EXITING...";
    for (int d=in; d<=in; d++){
    cout<<"\nProduct: ";
    cin>> pro[d].product;
    cout<<"\nItem name: ";
    cin>> pro[d].item_name;
    cout<<"\nMfg Date: ";
    cin>> pro[d].mfg;
    cout<<"\nExp Date: ";
    cin>> pro[d].exp;
    cout <<"\n";
    case 5: exit(1);
    anyone?... and about in list... for example i have added a data at index 1 so index is not empty and i have not yet added any data at index 2, 3 and 4 so it must say when i pick on list the data that i have added in index 1 will show up while index 2, 3 and 4 have no entry so it will say at index 2,3 and 4 empty.. hope you understand me tnx..
    Last edited by limitmaster; 01-05-2008 at 11:19 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    To know whether you have space left, you need to know how many spots you've used. It looks like you've made a first cut at that with the really-quite-horribly-named variable a; you need to be more careful of all the places a should change. And then the test is simple: if you've already used four spots, you don't have any more. Same deal with printing.

    That's answered your questions; now for the free advice you didn't want, but are going to get:

    1. I don't know if it's just here in preview mode, but your formatting isn't very good. (Edit: it's not just in preview mode. Get ready for the tomatoes.) The point of indenting is that you can easily tell visually where, for instance, case 1 ends; if every line starts at the same point, you can't see that.

    2. You got carried away with the cut'n'paste towards the end.

    3. Your reaction to an invalid index to edit seems ... extreme.

    4. And why can't they edit each field of your struct while they're there?

    5. Variable names should be more descriptive.

    6. Only nine characters for a description?

  3. #3
    Registered User
    Join Date
    May 2006
    in your head
    First of all sorry about my errors and thanks for the advice... about the variable a i can use for loop like so it stops at 4... but the problem is my teacher wants it to be done one index every time i add... so i change it and turn it like that... uhmmm can u give me a clue where will "a" change cause i've already tried everything that i know.. and im still a beginner when it comes to programming.. thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    a changes (or, should change) every time the number of items in your list changes. Which of your menu options change the number of items in your list? Those cases must then change a accordingly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Reiventing the wheel (again)
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2007, 03:26 AM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 02:47 AM