Thread: [HELP] about comparing between two strings...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    23

    [HELP] about comparing between two strings...

    Code:
    bool ChangeStatus()
    {
        char Seeker[MaxCallNumber];
        int SeekrPos = 0;
        cout << "CallNamber: " << endl;
        cin >> Seeker[MaxCallNumber];
        
        while(strcmp((Seeker[MaxCallNumber])!=(Database[SeekrPos].CallNumber)) != 0)
        {
            SeekrPos++;
        }
    i couldnt be able to compile this code... for this case... how can i do with it... my assignment doesnt allow me to change the main function... so i cannot pass a char array by value[const]... well... all i can change is to code inside the function... any idea?

    many thanks

    Cheers

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What this code is supposed to do?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    bool ChangeStatus()
    {
        char Seeker[MaxCallNumber];
        int SeekrPos = 0;
        cout << "CallNamber: " << endl;
        cin >> Seeker[MaxCallNumber];
    That should simply be:
    Code:
    bool ChangeStatus()
    {
        char Seeker[MaxCallNumber];
        int SeekrPos = 0;
        cout << "CallNamber: " << endl;
        cin >> Seeker;
    Code:
    while(strcmp((Seeker[MaxCallNumber])!=(Database[SeekrPos].CallNumber)) != 0)
    I'm guessing you want:
    Code:
    while(strcmp(Seeker,Database[SeekrPos].CallNumber) != 0)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char Seeker[MaxCallNumber];
    Declare an array of chars.

    Code:
        while(strcmp((Seeker[MaxCallNumber])!=(Database[SeekrPos].CallNumber)) != 0)
    strcmp() takes two arguments that are strings - you are passing one parameter that happens to be a char (at an index one step outside the size of your array). [Say MaxCallNumber is 10, then the array has indices 0..9, and you are passing element number 10, so one step "outside" the end.]

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    yes , i see, im a beginer... thank you guys!!!

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    i made a sutpid mistake~ -. -
    Last edited by kyaky; 08-06-2007 at 09:10 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kyaky View Post
    i made a supid mistake~ -. -
    That's part of learning - if we didn't make some rather embarrasing mistakes once in a while, we probably would never learn to not make the mistake... ;-) [If you think about things that you know to do and not to do, you'll probably find that the most "important" things that you know to NOT do come from experiencing the mistake and possibly the embarassment of makign this mistake]

    --
    Mats

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    btw~

    when im compiling these files using DEV or Qunciy in windows or anjuta in linux without a makefile, it says " [Linker error] undefined reference to XXX " something like that? do these programs supply a automake function to make a makefile [autolink] during the compiling? the only way i can compile those source files [cpp and .h files] is to use makefile in linux... anyone got any idea about that?

    Code:
    <main.cpp>
    /**
     * Main.cpp
     *
     * Main interface file, tests the input and output
     * functions.
     *
     * Expected outcome:
     * Should just print whatever have been entered to the screen.
     *
     * Purpose:
     * Created together with Input.cpp and Output.cpp to 
     * show the use of a Makefile.
     */
    #include <iostream>
    #include "Input.h"
    #include "Output.h"
    
    using namespace std;
    
    int main()
    {
      /* get a message from the input function */
      string in = input();
    
      /* output the message using the output function */
      output(in);
    
      return 0;
    }
    
    <input.cpp>
    /**
     * Input.cpp
     *
     * Functions to handle user text typed from keyboard.
     */
    #include <iostream>
    #include "Input.h"
    
    /**
     * This function would take user input and return the message
     * as a string.
     */
    string input()
    {
      /* In this simple example, just return a fixed string */
      string x;
      cout << "Please enter a statement to be printed out:" << endl;
      cin >> x;
      return x;
    }
    
    <input.h>
    #ifndef _INPUT_H_
    #define _INPUT_H_
    
    using namespace std;
    
    string input();
    
    #endif
    
    <output.cpp>
    /**
     * Output.cpp
     *
     * Functions to handle a message and pass it to
     * output devices such as screen.
     */
    
    #include <iostream>
    
    #include "Output.h"
    
    /**
     * This function outputs the given message to some
     * output device, in this case the screen.
     */
    void output(string in)
    {
      /* in this simple example, just write message to screen */
      cout << "You have entered: ";
      cout << in << endl;
    }
    
    <output.h>
    #ifndef _OUTPUT_H_
    #define _OUTPUT_H_
    
    using namespace std;
    
    void output(string);
    
    #endif
    
    <makefile>
    #
    # The "all" rule is the first rule that runs when running "make".
    # This is read as:  depends on the PROGRAM file,
    # thus invoking the rule below.
    #
    all: prog
    
    #
    # Links all object-files together into an executable.
    # If all object-files and includes are up to date, this does nothing.
    #
    prog: Main.o Input.o Output.o
    	g++ Main.o Input.o Output.o -o prog 
    
    #
    # Defines the source files dependencies on header files
    #
    Main.o: Main.cpp Input.h Output.h
    	g++ -c Main.cpp
    
    Input.o: Input.cpp Input.h
    	g++ -c Input.cpp
    
    Output.o: Output.cpp Output.h
    	g++ -c Output.cpp
    
    
    #
    # Removes all object-files and executables.
    # Run from command-line by typing "make clean".
    #
    clean:
    	rm -f prog Main.o Input.o Output.o
    Last edited by kyaky; 08-06-2007 at 09:11 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Makefile looks about right.

    What exactly is the symbol the linker is saying is missing - have you checked the spelling?

    --
    Mats

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    when im using DEV... do i have to use makefile? or i just click compile to compile them...?
    I am not sure how to compile these files without using a makefile in wiin32 OS like DEV.
    it works alrigh if i use linux's "make"
    Code:
    \ccG6baaa.o(.text+0x189) In function `main': 
      [Linker error] undefined reference to `input()' 
      [Linker error] undefined reference to `output(std::string)' 
    \ccG6baaa.o(.text+0x189) ld returned 1 exit status

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    thank you, matsp & hk_mp5kpdw.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will need to have a "make" program in your Windows system, or you need to make sure your DEV project is defined to consist of the files you have (main.c, input.c, output.c).

    --
    Mats

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I don't see a #include <string> anywhere... unless I've missed it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 strings with pointer
    By meriororen in forum C Programming
    Replies: 9
    Last Post: 05-22-2009, 07:37 PM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  4. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM