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

  1. #16
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Did you fix this?
    Quote Originally Posted by jlou
    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.

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    yes.

  3. #18
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Pat010101
    Code:
    char* tmp; 
    // ...
    cout << "Enter 1,2 or 3: ";
    cin >> tmp;
    You also need to allocate space for tmp. Try using:
    Code:
    char tmp[10];

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    I added this:
    Code:
    char tmp[10];
    It had not affect, i still couldnt use chocie 1.

  5. #20
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Maybe re-post your code. I removed the goto, changed the tmp declaration, used maxWord instead of 2, and changed <= to <. I ran the code, and while it's not perfect, it works.

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    Here:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    #define maxWord 3
    
    int main(int argc, char *argv[])
    {
    char* word[maxWord];
    char* def[2]; 
    char tmp[10]; 
    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;
    }

  7. #22
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Three problems:
    1. You forgot to change def[2] to def[maxWord].
    2. You need to remove the goto, it causes an infinite loop, possibly before your output is flushed to the console.
    3. Minor detail, but why does your compiler allow you to use <iostream> and cout without qualifying the std namespace? Are you changing your header so you won't get flamed.

  8. #23
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    thanks, i got it to work.

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