CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28
  1. #16
    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
    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;
    }
    There is rhyme and reason to formatting. Do it right and code is much easier to understand. Just correctly formatting your code points out a pretty obvious error with the for statement and the lines that follow.

    Your conversation about strings and ints still doesn't make sense.
    Last edited by GCDEF; June 1st, 2012 at 11:41 AM.

  2. #17
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    Quote Originally Posted by GCDEF View Post
    There is rhyme and reason to formatting. Do it right and code is much easier to understand. Just correctly formatting your code points out a pretty obvious error with the for statement and the lines that follow.

    Your conversation about strings and ints still doesn't make sense.
    When I compile I don't get any errors in my for statement I keep getting this as my errors

    C:\MinGW\include\c++\3.4.2\bits\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> >&)'|
    and your right I don't know what I was even thinking i was trying to sort int's instead of strings.

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

    Re: Algorithm problem

    You don't need that param at all. Just remove it an sort will sort the strings in ascending order.
    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. #19
    Join Date
    Jan 2009
    Posts
    596

    Re: Algorithm problem

    Quote Originally Posted by GCDEF View Post
    There is rhyme and reason to formatting. Do it right and code is much easier to understand. Just correctly formatting your code points out a pretty obvious error with the for statement and the lines that follow.
    Quote Originally Posted by smurf12125 View Post
    When I compile I don't get any errors in my for statement I keep getting this as my errors
    ...
    GCDEF doesn't mean the for statement has a compile error - he means it has a logical error. I.e. it isn't doing what you think it is.

    In your code:
    Code:
        std::string inString;
        for (int i = 0; i < 5; i++ )
            s.push_back(inString);
        {  
            getline (cin, inString);
            myfile << inString << endl;
        }
    I have highlighted all the code which is part of the for loop in red. The remainder is executed once the loop is finished. Is this what you thought the code means? I would guess not.

  5. #20
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    that's just to loop the program to keep repeating until it has 5 inputs. What is the statement doing then?
    Last edited by smurf12125; June 5th, 2012 at 11:35 AM.

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

    Re: Algorithm problem

    It's nothing wrong with it except that it does push_back 5 times with an empty string. Check where the braces are placed.
    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

  7. #22
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    oops had an unnecessary pair of braces there.

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

    Re: Algorithm problem

    No you don't. Just as GCDEF said the code does probably not doing what you want it to do.
    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

  9. #24
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    Quote Originally Posted by S_M_A View Post
    It's nothing wrong with it except that it does push_back 5 times with an empty string. Check where the braces are placed.
    He explained what it was doing and wow not thinking clearly today I just need to move my braces around my for statement and keep the other brace after my statement that has my input statement.

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

    Re: Algorithm problem

    You probably want to move the push_back line as well.
    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

  11. #26
    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
    He explained what it was doing and wow not thinking clearly today I just need to move my braces around my for statement and keep the other brace after my statement that has my input statement.
    You probably want to actually get the string before you add it to s also.

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

    Re: Algorithm problem

    Quote Originally Posted by smurf12125 View Post
    oops had an unnecessary pair of braces there.
    No. This is the syntax for a multi-line for loop:
    Code:
    for (initializer; condition; postcondition)
    {
         // everything goes here that falls into the loop.
    }
    I don't know why you say you have unnecessary braces, when what's in the braces determines what is being looped. If you thought that mere indenting the code under the for() did anything, it does absolutely nothing.
    Code:
      std::string inString;
        for (int i = 0; i < 5; i++ )
            s.push_back(inString);
            getline (cin, inString);
            myfile << inString << endl;
    If this is what you thought meant "I want to loop those three lines of code for 5 iterations", well C++ doesn't work by indents. The only line that falls in the loop in the above example is the push_back line. The indented lines below that are just that -- indented. Indentation doesn't play any part in how C++ works, only how your code looks.

    Regards,

    Paul McKenzie

  13. #28
    Join Date
    Apr 2012
    Posts
    37

    Re: Algorithm problem

    Quote Originally Posted by GCDEF View Post
    Repeating what? Explain in English what you want that for loop to accomplish.
    The loop is supposed to make five copys of InString for the five different inputs that are going to input into the "database"

Page 2 of 2 FirstFirst 12

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