CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Apr 2012
    Posts
    37

    Algorithm problem

    I have a algorithm bulit but I have errors and don't know why I am getting them.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    #include <algorithm>
    #include <vector>
    using namespace std;
    typedef std::vector <std::string> StringArray;
    int main()
    {
    cout << "This program is a database for storing information." << endl;
        ofstream myfile;
        myfile. open ("data.txt", ios::app);
    cout << "Please input what you want to store." << endl;
     StringArray s;
        std::string inString;
        for (int i = 0, i < 5; i++ ;); /// line 17
        s.push_back(inString);
    {   getline (cin, inString);
    myfile << inString << endl;
    }
        sort (s.begin(), s.end(), greater<int>());
        myfile. close ();
    system("pause");
    return 0;
    }
    I get the errors

    In function `int main()':|
    |17|error: expected init-declarator before '<' token|

    |17|error: expected `,' or `;' before '<' token|

    stl_algo.h|2319|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
    C:\MinGW\include\c++\3.4.2\bits\stl_function.h|218|note: candidates are: bool std::greater<_Tp>:: operator()(const _Tp&, const _Tp&) const [with _Tp = int]|

    stl_algo.h||In function `const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::greater<int>]':|

    stl_algo.h|124|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_algo.h|125|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_algo.h|127|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_algo.h|131|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_algo.h|133|error: no match for call to `(std::greater<int>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_algo.h|2041|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_algo.h|2044|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_algo.h|2145|error: no match for call to `(std::greater<int>) (std::string&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'


    stl_algo.h|2093|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|


    stl_heap.h|279|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|

    stl_heap.h|166|error: no match for call to `(std::greater<int>) (std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|


    What is going on here? I've been researching this for a couple days and can't figure out whats going wrong.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Algorithm problem

    Code:
    for (int i = 0, i < 5; i++ ;); /// line 17
    Check your formatting there.
    you probably want
    Code:
    for (int i = 0; i < 5; i++ ) /// line 17

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Algorithm problem

    Code:
    for (int i = 0, i < 5; i++ ;);
    Shouldn't that be?
    Code:
    for (int i = 0; i < 5; i++ )
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    for (int i = 0, i < 5; i++ ; ); /// line 17
    that was to solve another error I was getting saying
    |17|error: expected `;' before ')' token|

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Algorithm problem

    Quote Originally Posted by smurf12125 View Post
    that was to solve another error I was getting saying
    |17|error: expected `;' before ')' token|
    But it's not correct syntax for a for statement.

  6. #6
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    Quote Originally Posted by GCDEF View Post
    you probably want
    Code:
    for (int i = 0; i < 5; i++ ) /// line 17
    That just is leading me to more errors and expecting more primary expressions before every semicolon

  7. #7
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    Quote Originally Posted by GCDEF View Post
    But it's not correct syntax for a for statement.
    here is the full error when I remove that semicolon
    C:\Documents and Settings\indstdy\Desktop\database.cpp|17|error: expected `;' before ')' token|

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Algorithm problem

    Quote Originally Posted by smurf12125 View Post
    That just is leading me to more errors and expecting more primary expressions before every semicolon
    Then you have other problems. That's syntactically correct. Are you sure that's exactly how you have it? Paste your exact code with that syntax.

    The only error I get is it doesn't know what greater is.
    Last edited by GCDEF; May 31st, 2012 at 10:39 AM.

  9. #9
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    You were right there was an comma I accidentally didn't delete so I fixed that now all the rest of the errors are saying that happen in the algo.h file but here is my code now
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    #include <algorithm>
    #include <vector>
    using namespace std;
    typedef std::vector <std::string> StringArray;
    int main()
    {
    cout << "This program is a database for storing information." << endl;
        ofstream myfile;
        myfile. open ("data.txt", ios::app);
    cout << "Please input what you want to store." << endl;
     StringArray s;
        std::string inString;
        for (int i = 0; i < 5; i++ );
        s.push_back(inString);
    {   getline (cin, inString);
    myfile << inString << endl;
    }
        sort (s.begin(), s.end(), greater<int>());
        myfile. close ();
    system("pause");
    return 0;
    }

  10. #10
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Algorithm problem

    Hi,
    Although I'm not sure what you are attempting to do next line is wrong:
    Code:
    for (int i = 0, i < 5; i++ ;); /// line 17
    It should be
    Code:
    for (int i = 0; i < 5; i++);
    Anyway, above expression is useless because of ';' at the end (out of parenthesis). It just iterates 5 times doing nothing else.

    There's another compiler mistake. StringArray is a vector of strings. You sorting there as if they were a vector of ints?
    Code:
    sort (s.begin(), s.end(), greater<int>());
    Hope this helps.

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  11. #11
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    Quote Originally Posted by AlbertGM View Post
    There's another compiler mistake. StringArray is a vector of strings. You sorting there as if they were a vector of ints?
    Code:
    sort (s.begin(), s.end(), greater<int>());
    Hope this helps,
    Albert.
    Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Algorithm problem

    Quote Originally Posted by smurf12125 View Post
    Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.
    Not following that one. Wouldn't you need to what?

  13. #13
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Algorithm problem

    Quote Originally Posted by smurf12125 View Post
    Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.
    It could be because of my terrible english, but what this has to do with your compiler errors? I don't get it.

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Algorithm problem

    Quote Originally Posted by smurf12125 View Post
    Wouldn't I need to since StringArray would repeat until it a break function is input? Because StringArray would keep inputing text to the .txt file and would clear itself when it inputs the text to the file.
    Point to us where you were told that std::sort did anything that you stated. Or are you just making things up?

    std::sort does that -- sorts a sequence of values. It doesn't do anything else. The last argument depends on the first two -- you are sorting std::string, not int, therefore the greater template must be based on std::string.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    Quote Originally Posted by Paul McKenzie View Post
    Point to us where you were told that std::sort did anything that you stated. Or are you just making things up?

    std::sort does that -- sorts a sequence of values. It doesn't do anything else. The last argument depends on the first two -- you are sorting std::string, not int, therefore the greater template must be based on std::string.

    Regards,

    Paul McKenzie
    I need a break function to end the loop of inputting my text not for std::sort and Albert was talking about how I was sorting it as a vecor of ints instead of a vector of strings. I asked since I was using StringArray that I would need to have them input as as int's instead of strings. Here is my code now so you can see what I mean.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    #include <algorithm>
    #include <vector>
    using namespace std;
    typedef std::vector <std::string> StringArray;
    int main()
    {
    cout << "This program is a database for storing information." << endl;
        ofstream myfile;
        myfile. open ("data.txt", ios::app);
    cout << "Please input what you want to store." << endl;
     StringArray s;
        std::string inString;
        for (int i = 0; i < 5; i++ )
        s.push_back(inString);
    {   getline (cin, inString);
    myfile << inString << endl;
    }
        std::sort (s.begin(), s.end(), greater<int>());
        myfile. close ();
    system("pause");
    return 0;
    }

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured