none
A first year student of Computer Science RRS feed

  • Dotaz

  • I have a problem in the program: The program ran but she gets stuck in the middle and when I do dibbger it works.
    I will be happy if you help me problem.
    It does not work in the case 0.

    ************************************************************************

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <string.h>
    using namespace std;

    void mystrcpy(char *s1, char*s2)
    {
    while (*s1)
    *s1++ = *s2++;
    *s2 = '\0';
    }

    void newStr(char **&lexicon, char newWord[], int sizeArr, int count)
    {
    char **tmp = NULL;
    if (count == 1) //כאן נקצה את הגודל להיות אחד עבור מחרוזת אחת 
    {
    lexicon = new char*[1];
    for (int i = 0; i < count; i++)
    {
    lexicon[i] = new char[sizeArr + 1];
    mystrcpy(lexicon[i], newWord);
    }
    }
    else
    {
    tmp = new char *[count];
    for (int i = 0; i < count; i++) //כיון שבלולאה זו מטפלים באיבר החדש צריך שהלולאה תרוץ עד count כדי שהמחרוזת החדשה תשובץ גם כשהיא בסוף
    {
    if (i == count || strcmp(newWord, lexicon[i]) <= 0) //ההשואה צריכה להיות לפי strcmp
    {
    tmp[i] = new char[sizeArr + 1];
    mystrcpy(tmp[i], newWord);

    for (int j = i + 1; j < count; j++, i++)
    tmp[j] = lexicon[j - 1];
    break; //אין צורך להמשיך עם הלולאה במקרה זה כי כבר האיברים הועתקו עד הסוף

    }
    else
    tmp[i] = lexicon[i];
    }

    }
    if (count > 1)
    {

    delete[] lexicon;
    lexicon = tmp;
    }
    }

    void printAll(char **&lexicon, int count)
    {
    cout << "*******************the words are:*******************" << endl;
    for (int i = 0; i < count; i++)
    cout << lexicon[i] << endl;
    }

    void delStr(char **&lexicon, int count, char delWord[])
    {
    int index = 0;
    char ** tmp=NULL;
    for (int i = 0; i < count; i++)
    {
    if (strcmp(delWord, lexicon[i]) == 0)
    {
    index = i;
    break;
    }
    }
    if (index >= 0)
    {
    tmp = new char*[count - 1];
    for (int i = 0; i < count; i++)
    {
    if (strcmp(delWord, lexicon[i]) == 0)
    {
    for (int j = i+1; j < count ; j++,i++)
    tmp[i] = lexicon[j];
    break;
    }
    else
    tmp[i] = lexicon[i];
    }
    }
    delete[] lexicon;
    lexicon = tmp;
    }

    int main()
    {
    int choice, count = 0, sizeArr;
    char **lexicon = NULL, newWord[81],delWord[81]; ///בהתחלה הלקסיקון ריק
    do
    {
    cout << "enter 0-5" << endl;
    cin >> choice;
    switch (choice)
    {
    case 0: cout << "enter the word:" << endl;
    cin >> newWord;
    sizeArr = strlen(newWord);   // counting whitout NULL
    count++;
    newStr(lexicon, newWord, sizeArr, count);
    printAll(lexicon, count);
    break;
    case 1: cout << "enter the word to delete:" << endl;
    cin >> delWord;
    delStr(lexicon, count, delWord);
    printAll(lexicon, count);
    break;
    case 5:break;
    default:cout << "ERROR" << endl;

    }

    } while (choice != 5);
    return 0;
    }

    středa 1. července 2015 8:37