Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to egalise two strings

How to egalise two strings

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
41 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    in fact i think i did not express the problem correctly: i don´t want to egalise two string, but to compare two strings. ( sorry for my english):sigh:

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #11

    but what does equalize mean for you ?? and in what does it differ from comparing 2 strings ? also, don't hesitate to tell what kind of string you use (me know that you use std::string, but most people who replied here didn't know that, hence the CString references) to avoid bad answers


    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    C 1 Reply Last reply
    0
    • L Lost User

      mandanani wrote:

      == operaotr compares not equalizes

      then why does the code: c1='aaa bbb ccc'; f1='aaa bbb ccc'; cout< _return on screen: c c 0_

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #12

      I think you have to understand how strings function. What are c1 and f1 ? char pointers, std::string or CString ? Without knowing that we can't really help you. If these are standard char pointers, you need to use strcpy to be able to store one string in the array (but first, it must be allocated with enough size). Then, you can compare two strings using strcmp function. Everything has already been told before. So a code example: char string1[255]; char string2[255]; strcpy(string1,"Test"); strcpy(string2,"Test"); if (strcmp(string1,string2) == 0) cout << "Strings are equal"; else cout << "Strings are different"; Check the documentation of the functions on MSDN to be sure you understood correctly the concept.


      Cédric Moonen Software developer
      Charting control [v1.2]

      1 Reply Last reply
      0
      • T toxcct

        but what does equalize mean for you ?? and in what does it differ from comparing 2 strings ? also, don't hesitate to tell what kind of string you use (me know that you use std::string, but most people who replied here didn't know that, hence the CString references) to avoid bad answers


        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #13

        toxcct wrote:

        me know that you use std::string

        How do you know that ??


        Cédric Moonen Software developer
        Charting control [v1.2]

        J T 2 Replies Last reply
        0
        • C Cedric Moonen

          toxcct wrote:

          me know that you use std::string

          How do you know that ??


          Cédric Moonen Software developer
          Charting control [v1.2]

          J Offline
          J Offline
          Jonathan Darka
          wrote on last edited by
          #14

          Cedric Moonen wrote:

          How do you know that ??

          I suspect he made an assumption based on the fact the code was using the '==' operator to attempt the comparison, but could obviously be wrong.


          Jonathan Wilkes Darka[Xanya.net]

          C 1 Reply Last reply
          0
          • C Cedric Moonen

            toxcct wrote:

            me know that you use std::string

            How do you know that ??


            Cédric Moonen Software developer
            Charting control [v1.2]

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #15

            because he asked a question related 2 posts below ;P check this[^]


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            L 1 Reply Last reply
            0
            • J Jonathan Darka

              Cedric Moonen wrote:

              How do you know that ??

              I suspect he made an assumption based on the fact the code was using the '==' operator to attempt the comparison, but could obviously be wrong.


              Jonathan Wilkes Darka[Xanya.net]

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #16

              I think the OP was using the == operator with char pointers and that's why it didn't work.


              Cédric Moonen Software developer
              Charting control [v1.2]

              1 Reply Last reply
              0
              • T toxcct

                because he asked a question related 2 posts below ;P check this[^]


                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #17

                If somebody is motivated to understand that, i have tried to regroup and order the programme. I hope you will help me. #include #include using namespace std; #include #include #include #include #include int main() { // Openning the flow : fstream list; list.open("listfile.txt",ios::in); // Decalaration of the variables: char t; int i; string s1; string s2; string s3; string s4; string s5; string s6; string s7; string s8; string s9; string s10; string s11; //Getting the name of the files: getline(list,s1);cout<tm_mday <<"-" << date->tm_mon + 1 <<"-" << date->tm_year + 1900 << ".txt"; //Declaration of the variables: int a1=0; int a2=0; int a3=0; int a11=0; int a4=0; int a5=0; int a6=0; int a7=0; int a8=0; int a9=0; int a10=0; char c; char t1; char t2; char t3; char t11; char t4; char t5; char t6; char t7; char t8; char t9; char t10; char f; // Opening clippr´ flow: fstream clippr; clippr.open("clippr.txt",ios::in); // Opening the files´ flow : fstream fichier1; fichier1.open(s1.c_str(),ios::in); fstream fichier2; fichier2.open(s2.c_str(),ios::in); fstream fichier3; fichier3.open(s3.c_str(),ios::in); fstream fichier11; fichier11.open(s11.c_str(),ios::in); fstream fichier4; fichier4.open(s4.c_str(),ios::in); fstream fichier5; fichier5.open(s5.c_str(),ios::in); fstream fichier6; fichier6.open(s6.c_str(),ios::in); fstream fichier7; fichier7.open(s7.c_str(),ios::in); fstream fichier8; fichier8.open(s8.c_str(),ios::in); fstream fichier9; fichier9.open(s9.c_str(),ios::in); fstream fichier10; fichier10.open(s10.c_str(),ios::in); // Getting the file lines: string c1;string c2; string c3; string f1;string f2; string f3; getline(clippr,c1); getline(clippr,c1);cout<

                T 1 Reply Last reply
                0
                • L Lost User

                  Hello, I would like to compare two strings: i used: if (s1==s2) { I have no problem to compile, but even when s1=s2 it doesnt work...

                  S Offline
                  S Offline
                  ShilpiP
                  wrote on last edited by
                  #18

                  if(strcmp(s1,s2)==0)

                  Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                  T L 2 Replies Last reply
                  0
                  • S ShilpiP

                    if(strcmp(s1,s2)==0)

                    Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #19

                    s1 and s2 are std::strings...


                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    L 1 Reply Last reply
                    0
                    • T toxcct

                      s1 and s2 are std::strings...


                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #20

                      Et cela pose problème? :doh:

                      T 1 Reply Last reply
                      0
                      • L Lost User

                        If somebody is motivated to understand that, i have tried to regroup and order the programme. I hope you will help me. #include #include using namespace std; #include #include #include #include #include int main() { // Openning the flow : fstream list; list.open("listfile.txt",ios::in); // Decalaration of the variables: char t; int i; string s1; string s2; string s3; string s4; string s5; string s6; string s7; string s8; string s9; string s10; string s11; //Getting the name of the files: getline(list,s1);cout<tm_mday <<"-" << date->tm_mon + 1 <<"-" << date->tm_year + 1900 << ".txt"; //Declaration of the variables: int a1=0; int a2=0; int a3=0; int a11=0; int a4=0; int a5=0; int a6=0; int a7=0; int a8=0; int a9=0; int a10=0; char c; char t1; char t2; char t3; char t11; char t4; char t5; char t6; char t7; char t8; char t9; char t10; char f; // Opening clippr´ flow: fstream clippr; clippr.open("clippr.txt",ios::in); // Opening the files´ flow : fstream fichier1; fichier1.open(s1.c_str(),ios::in); fstream fichier2; fichier2.open(s2.c_str(),ios::in); fstream fichier3; fichier3.open(s3.c_str(),ios::in); fstream fichier11; fichier11.open(s11.c_str(),ios::in); fstream fichier4; fichier4.open(s4.c_str(),ios::in); fstream fichier5; fichier5.open(s5.c_str(),ios::in); fstream fichier6; fichier6.open(s6.c_str(),ios::in); fstream fichier7; fichier7.open(s7.c_str(),ios::in); fstream fichier8; fichier8.open(s8.c_str(),ios::in); fstream fichier9; fichier9.open(s9.c_str(),ios::in); fstream fichier10; fichier10.open(s10.c_str(),ios::in); // Getting the file lines: string c1;string c2; string c3; string f1;string f2; string f3; getline(clippr,c1); getline(clippr,c1);cout<

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #21

                        very bad thing to post a huge block of code and tell to anyone to see what's wrong with it... you must post relevant samples, and try to understand which are the incriminated lines. and use your debugger if you can


                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                        1 Reply Last reply
                        0
                        • S ShilpiP

                          if(strcmp(s1,s2)==0)

                          Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #22

                          Shilpi Boosar wrote:

                          if(strcmp(s1,s2)==0)

                          lead to the following error: <;146 C:\Documents and Settings\garfaoui\Ambiente de trabalho\test\clippr\main.cpp no matching function for call to `strcmp(std::string&, std::string&)' >

                          1 Reply Last reply
                          0
                          • L Lost User

                            Et cela pose problème? :doh:

                            T Offline
                            T Offline
                            toxcct
                            wrote on last edited by
                            #23

                            pourquoi tu le prend comme ca ? je le signalais juste à celui qui te répondait que son code ne marche que si ce sont des char*, pas des strings :~


                            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                            C L 2 Replies Last reply
                            0
                            • T toxcct

                              pourquoi tu le prend comme ca ? je le signalais juste à celui qui te répondait que son code ne marche que si ce sont des char*, pas des strings :~


                              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                              C Offline
                              C Offline
                              CPallini
                              wrote on last edited by
                              #24

                              OK, ragazzi, ora, rilassatevi...:-D

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                              T 1 Reply Last reply
                              0
                              • T toxcct

                                pourquoi tu le prend comme ca ? je le signalais juste à celui qui te répondait que son code ne marche que si ce sont des char*, pas des strings :~


                                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #25

                                Oula je ne suis pas du tout enervé, choqué, ou autre. Je bois les paroles de tous ceux qui postent un message sur ce forum car je n´ai a mon actif ke 30h de langage c++. Je n´y connais pas grand chose. :) Je suis actuellement en stage et je dois realiser ce programme. Cela fait maintenant une semaine que je suis dessus, et je sens qu´il est tout près d´aboutir. Désolé si ma réponse a pu paraitre sèche, ce n´etait pas du tout l´esprit du message. C´est l´effet message ecrit qui ne transcrit pas le ton de la voix. :)

                                T R 2 Replies Last reply
                                0
                                • C CPallini

                                  OK, ragazzi, ora, rilassatevi...:-D

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                  T Offline
                                  T Offline
                                  toxcct
                                  wrote on last edited by
                                  #26

                                  uhh, sorry, i don't understand much italian ! lol


                                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                  C 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Oula je ne suis pas du tout enervé, choqué, ou autre. Je bois les paroles de tous ceux qui postent un message sur ce forum car je n´ai a mon actif ke 30h de langage c++. Je n´y connais pas grand chose. :) Je suis actuellement en stage et je dois realiser ce programme. Cela fait maintenant une semaine que je suis dessus, et je sens qu´il est tout près d´aboutir. Désolé si ma réponse a pu paraitre sèche, ce n´etait pas du tout l´esprit du message. C´est l´effet message ecrit qui ne transcrit pas le ton de la voix. :)

                                    T Offline
                                    T Offline
                                    toxcct
                                    wrote on last edited by
                                    #27

                                    ok, let's continue in english now. you still didn't answered a question of mine... what, for you, is the difference between equalize and compare 2 strings ?


                                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                    L 1 Reply Last reply
                                    0
                                    • T toxcct

                                      ok, let's continue in english now. you still didn't answered a question of mine... what, for you, is the difference between equalize and compare 2 strings ?


                                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #28

                                      to me, compare is looking at 2 objects and see if they are equal or not. egalise a and b is a=b (change the value of a or b so that it is equal to the other value) is it clear :doh:

                                      T 1 Reply Last reply
                                      0
                                      • T toxcct

                                        uhh, sorry, i don't understand much italian ! lol


                                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                        C Offline
                                        C Offline
                                        CPallini
                                        wrote on last edited by
                                        #29

                                        Oh, pardon, I assumed it was a multi-ethnic thread... :-D

                                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          to me, compare is looking at 2 objects and see if they are equal or not. egalise a and b is a=b (change the value of a or b so that it is equal to the other value) is it clear :doh:

                                          T Offline
                                          T Offline
                                          toxcct
                                          wrote on last edited by
                                          #30

                                          ok, i get what's your problem then... you are trying to speak english with french words ;P don't say equalize but assign. anyway, as you're using std::strings, just use the = operator for this, and == operator to compare their content.

                                          std::string s1 = "Hello";
                                          std::string s2 = "World";

                                          ASSERT(s1 != s2); // the objects contain different strings

                                          s1 = s2; //Assign s2 content into s1
                                          ASSERT(s1 == s2); // the objects contain same strings


                                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                          L 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups