Thread: problem with strcpy while copying a 2d string into 1d string in dev c++

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    25

    problem with strcpy while copying a 2d string into 1d string in dev c++

    insert
    Code:
    #include<iostream>
    #include<conio.h>
    #include<string.h>
    
    using namespace std;
    
    class dic
    {
          char w[5][10];
          char m[5][10];
          char a[5][10];
          
          public:
                 dic();
                 void search();
                 void setpos(int); 
                 void disrecsea();  
                 void display(); 
    };
    
    int j=0;
    
    dic::dic()
    {
                      strcpy(w[0],"first");
                      strcpy(w[1],"second");
                      strcpy(w[2],"third");
                      strcpy(w[3],"fourth");
                      strcpy(w[4],"fifth");
                 
                      strcpy(m[0],"1");
                      strcpy(m[1],"2");
                      strcpy(m[2],"3");
                      strcpy(m[3],"4");
                      strcpy(m[4],"5");
                 
                      strcpy(a[0],"6");
                      strcpy(a[1],"7");
                      strcpy(a[2],"8");
                      strcpy(a[3],"9");
                      strcpy(a[4],"10");
    }
    
    void dic::search()
    {
         int i;
         char word[10];
         cout<<"\n Enter the word = ";
         cin>>word;
         for(i=0;i<5;i++)
         {
           if(stricmp(word,w[i])==0)
           {
             cout<<"\n Meaning = "<<w[i];
             cout<<"\n Antonym = "<<a[i];
             setpos(i);
           }
         }
        if(i==5)
         cout<<"\n NOT FOUND ";
    } 
                 
    void setpos(int i)
    {
         char ch[10];
         
         strcpy(ch,w[j]); // this is where i am getting error
         strcpy(w[j],w[i]);
         strcpy(w[i],);
          
         strcpy(ch,a[j]);
         strcpy(a[j],a[i]);
         strcpy(a[i],ch); 
        
         strcpy(ch,m[j]);
         strcpy(m[j],m[i]);
         strcpy(m[i],ch); 
         j++;
    }
                  
    void dic::disrecsea()
    {
         cout<<"\n Recent searches \n";
         if(j==0)
          cout<<"\n No recent searches";
         for(int i=0;i<j;i++)
         {
           cout<<"\n Word    = "<<w[i];
           cout<<"\n Meaning = "<<m[i];
           cout<<"\n Antonym = "<<a[i];
           getch();
         }
    }
    
    void dic::display()
    {
      for(int i=0;i,5;i++)
      {
        cout<<"\n Word    = "<<w[i];
        cout<<"\n Meaning = "<<m[i];
        cout<<"\n Antonym = "<<a[i];
        getch();
      }
    }  
        
    int main()
    {
        char choice;
        dic o1;
        top:
        search();
        disrecsea();
        display();
        cout<<"\n Do you want to continue(y/n) = ";
        cin>>choice;
        if(choice=='y'||choice=='Y')
         goto top;
        return 0;
    }
    error:-'w' undeclared( first use this function )
    how to resolve???????

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You probably intend to be writing dic::setpos rather than setpos.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Code:
    void dic::setpos(int i)
    {
         char ch[10];
         
         strcpy(ch,w[j]); // this is where i am getting error
         strcpy(w[j],w[i]);
         strcpy(w[i],);
          
         strcpy(ch,a[j]);
         strcpy(a[j],a[i]);
         strcpy(a[i],ch); 
        
         strcpy(ch,m[j]);
         strcpy(m[j],m[i]);
         strcpy(m[i],ch); 
         j++;
    }
    You forgot to resolve your function with your class. Any special reason you are using char arrays and C functions in a C++ program? This isn't C++, you are making C with classes. You should look at implementing std::string and std::vector for this. It will make your program a lot easier to manage.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by shikhardeep View Post
    Code:
        
    int main()
    {
        char choice;
        dic o1;
        top:
        search();<--THIS WONT WORK EITHER
        disrecsea();<--THIS WONT WORK EITHER
        display();<--THIS WONT WORK EITHER
        cout<<"\n Do you want to continue(y/n) = ";
        cin>>choice;
        if(choice=='y'||choice=='Y')
         goto top;
        return 0;
    }
    There are only a few reasons to use goto. THIS IS NOT ONE OF THEM. Think about your program and restructure accordingly, such as using a loop.

    EDIT: None of your function calls will work either. May I suggest you look at CBoard - C++ Tutorials.
    Last edited by AndrewHunter; 08-09-2011 at 05:29 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    thank u all.Now its working.i can't believe how a silly mistake it was.there were some logical errors also but i corrected them and some others like calling member functions directly from main() and not using object.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I hope you fixed some of the things pointed out, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. Copying a string, into a string array.
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-31-2006, 05:19 PM
  4. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  5. Copy String without using strcpy
    By sunrise in forum C Programming
    Replies: 11
    Last Post: 02-06-2005, 03:24 PM