Thread: New to C++ have a problem with dictionary code

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    12

    New to C++ have a problem with dictionary code

    Hi,
    when I choose the first choice in this program, "1"
    it will come up with nothing. If I put in the second choice "2" it gives the defonition. How can i get it so It will do all choices, not just number 2. Thanks

    #include <iostream>
    #include <stdlib.h>
    #include <string.h>

    #define maxWord 1

    int main(int argc, char *argv[])
    {
    char* word[2];
    char* def[2];
    char* tmp;
    char test;


    word [1] = "1";
    def [1] = "ab";

    word [1] = "2";
    def [1] = "abc";





    cout << "Enter 1 or 2: ";
    cin >> tmp;

    for (int i = 1; i <= maxWord; i++)
    {
    if(strcmp(tmp, word[i]) == 0)
    cout << def[i];
    }
    test:
    goto test;
    system("PAUSE");
    return 0;
    }

  2. #2
    Hello,

    Firstly, you write word[1] twice. For example:
    Code:
    word [1] = "1";
    word [1] = "2";
    This will automatically set word[1] to "2" before you check. The first subscript for an array is 0.

    To solve this issue, you can do the following:
    Code:
    //First index in array
    word [0] = "1";
    def [0] = "ab";
    
    // Second index
    word [1] = "2";
    def [1] = "abc";
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    I tried this but it came up the same :
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>

    #define maxWord 1

    int main(int argc, char *argv[])
    {
    char* word[2];
    char* def[2];
    char* tmp;
    char test;


    word [0] = "1";
    def [0] = "ab";

    word [1] = "2";
    def [1] = "abc";





    cout << "Enter 1 or 2: ";
    cin >> tmp;

    for (int i = 1; i <= maxWord; i++)
    {
    if(strcmp(tmp, word[i]) == 0)
    cout << def[i];
    }
    test:
    goto test;
    system("PAUSE");
    return 0;
    }

  4. #4
    I see,

    It is your for loop. You are saying, the loop starts at 1, and loop goes until 1, then increment. Rather, you need to change the first expression to 0. If not you are not going anywhere in your loop.
    Code:
    for (int i = 0; i <= maxWord; i++)
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    Thank you! I got it to work correctly

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Also, please also use code tags. Put [/code] at the end of your code, and [code] at the beginning. It formats your code nicely and indents it so it's easier for us to read.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    actually, i just put those there because after i put in 1 or 2, it would say "press any key to exit" I just did that for the time being until i looped it back to the start.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    edit: the above was a response to some lines edited out from my post. I queried the use of labels and "goto".

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    1 last question, i tried going over 2 possible choices and the third one didnt work.


    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    #define maxWord 1
    
    int main(int argc, char *argv[])
    {
    char* word[2];
    char* def[2]; 
    char* tmp; 
    char test;
    
    
    word [0] = "1";
    def [0] = "ab";
    
    word [1] = "2";
    def [1] = "abc";
    
    word [2] = "3";
    def [2] = "abcd";
    
    
    
    
    cout << "Enter 1,2 or 3: ";
    cin >> tmp;
    
    for (int i = 0; i <= maxWord; i++)
    {
    if(strcmp(tmp, word[i]) == 0)
    cout << def[i];
    }
    test: 
    goto test;
    system("PAUSE"); 
    return 0;
    }

  10. #10
    Hello,

    Your loop is confined to starting at 0 and ending at 1. If you want more possibilities, I would suggest you increase #define maxWord from 1 to 2, etc...


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    thanks

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    I thought it was working. Now 2 and 3 work but choice #1 doesnt:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    #define maxWord 3
    
    int main(int argc, char *argv[])
    {
    char* word[2];
    char* def[2]; 
    char* tmp; 
    char test;
    
    
    word [0] = "1";
    def [0] = "ab";
    
    word [1] = "2";
    def [1] = "abc";
    
    word [2] = "3";
    def [2] = "abcd";
    
    
    
    
    cout << "Enter 1,2 or 3: ";
    cin >> tmp;
    
    for (int i = 0; i <= maxWord; i++)
    {
    if(strcmp(tmp, word[i]) == 0)
    cout << def[i];
    }
    test: 
    goto test;
    system("PAUSE"); 
    return 0;
    }

  13. #13
    Hello,

    You array is also confined to 0 and 1. See:
    Code:
    char* word[2];
    char* def[2];
    You will also need to increase these if you want to store more variables. For example change 2 to 3 in this example.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Or change 2 to maxWord so that it stays consistent:
    Code:
    char* word[maxWord];
    Also, your for loop should run from 0 to less than maxWord, not less than or equal:
    Code:
    for (int i = 0; i < maxWord; i++)
    Array indexes are zero-based, meaning an array of size 3 has valid values at indexes 0, 1 and 2. Array index 3 would be invalid, so you only want to loop while the index is less than the size.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    Quote Originally Posted by Stack Overflow
    Hello,

    You array is also confined to 0 and 1. See:
    Code:
    char* word[2];
    char* def[2];
    You will also need to increase these if you want to store more variables. For example change 2 to 3 in this example.

    - Stack Overflow

    I tried making
    [/color]* word[2];
    into
    [/color]* word[3];
    But the program crashed when i tried it.

    I tried doing [/color]* word[maxword];
    but it also crashed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM