Simple Problem I'm sure..

This is a discussion on Simple Problem I'm sure.. within the C++ Programming forums, part of the General Programming Boards category; I went from C to C++, but I am sure that isn't the problem. Perhaps strcmp() cannot compare two char's? ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Simple Problem I'm sure..

    I went from C to C++, but I am sure that isn't the problem. Perhaps strcmp() cannot compare two char's?

    Thanks!

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
        string chain;
        string input;
        int done = 0;
        int compare;
        char YN;
    
    
        while (done==0)
            {
                cout << "Input next element in chain: " << endl;
                scanf("%s", input);
                chain = chain + input;
    
                cout << "Done? (Y/N) " << endl;
                scanf("%c", YN);
    
                if (compare = strcmp(YN,'Y') == 0)
                    done = 1
            }
    
        cout << Chain << endl;
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Did you look at the documentation for strcmp()? Are the parameters you are using C-strings?

    If you are writing a C++ program why are you using the C-stdio functions? The C-stdio functions do not know anything about C++ std::strings so your scanf() on line 20 should fail horribly. You should be using C++ iostreams to read and write variables to the console in C++.


    Jim

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Changed the char to a string so it should be able to compare it.

    I do not completely understand what you mean by the second paragraph though. I need <stdio.h> for the scanf(). I thought the header files were essentially the same. :/

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
        string chain;
        string input;
        int done = 0;
        int compare;
        string YN;
    
    
        while (done==0)
            {
                cout << "Input next element in chain: " << endl;
                scanf("%s", input);
                chain = chain + input;
    
                cout << "Done? (Y/N) " << endl;
                scanf("%s", YN);
    
                if (strcmp(YN,"Y") == 0)
                    done = 1;
            }
    
        cout << chain << endl;
    
        return 0;
    }

    Errors:
    Code:
    C:\CPP\ListC\main.cpp||In function 'int main()':|
    C:\CPP\ListC\main.cpp|20|warning: cannot pass objects of non-POD type 'struct std::string' through '...'; call will abort at runtime|
    C:\CPP\ListC\main.cpp|20|warning: format '%s' expects type 'char*', but argument 2 has type 'int'|
    C:\CPP\ListC\main.cpp|24|warning: cannot pass objects of non-POD type 'struct std::string' through '...'; call will abort at runtime|
    C:\CPP\ListC\main.cpp|24|warning: format '%s' expects type 'char*', but argument 2 has type 'int'|
    C:\CPP\ListC\main.cpp|26|error: cannot convert 'std::string' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'|
    C:\CPP\ListC\main.cpp|13|warning: unused variable 'compare'|
    ||=== Build finished: 1 errors, 5 warnings ===|

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    hmm seems like I needed to use char* and not string....

    only one problem, how to add them together in a string?

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Maybe I should be more clear in what I want.

    I want the program to ask for a list of elements that corresponds to how a molecule is, so I can get the resonance structures from them later. i.e. input "C" and "C" and you get two C's in a row the string chain would equal CC which would be outputted "C - C" which for chemistry people is actually C2H6 (Ethane). So right now is just the simple part before I make it more complex.

    I read a whole C book, at the end of the book I realized I really should have been learning C++... I'll have to read another book of stuff I already know again, fun. BUt for now, I just want to program a little.

    Thanks

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    nvm, that question was retarded...lol

    still have a problem though, will post in a nother topic.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Replace the horribly insafe scanf("%s",...) with std::getline and strcmp with the normal "==" operator. Then it will work.
    C++ is nice and pretty contrast to C.
    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. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 12:16 PM
  2. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  3. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 09:28 AM
  4. C problem (simple)
    By juststartedC in forum C Programming
    Replies: 13
    Last Post: 09-12-2007, 09:04 PM
  5. simple problem
    By none in forum C++ Programming
    Replies: 8
    Last Post: 10-30-2002, 06:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21