Thread: New to C++

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12

    New to C++

    Hi all I am having some problems with compiling my c++ code. I have had lots of experience in Java but c++ is new.

    I am trying to read in a file that contains words, sort them, then print them out to a new file in my program but the error is saying there is error on line 22 involving iterating through the elements of a vector. I am confused and any help is greatly appreciated!

    Code:
    #include <iostream>
    #include <list>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
    string x;
    list<string> myList;
    ifstream inFile;
    ofstream outFile;
    
    inFile.open("dictionary");
    	while (inFile >> x) {
    		myList.push_back(x);
    	}
    	inFile.close();
    	myList.sort();
    	outFile.open("sortedDictionary");
    	for (int i = 0; i < myList.size(); i++) {
    			outFile << myList[i] << endl;
    		}
    		outFile.close();
    		return 0;
    	}

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by gbman88 View Post
    Hi all I am having some problems with compiling my c++ code. I have had lots of experience in Java but c++ is new.

    I am trying to read in a file that contains words, sort them, then print them out to a new file in my program but the error is saying there is error on line 22 involving iterating through the elements of a vector. I am confused and any help is greatly appreciated!

    Code:
    #include <iostream>
    #include <list>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
    string x;
    list<string> myList;
    ifstream inFile;
    ofstream outFile;
    
    inFile.open("dictionary");
        while (inFile >> x) {
            myList.push_back(x);
        }
        inFile.close();
        myList.sort();
        outFile.open("sortedDictionary");
        for (int i = 0; i < myList.size(); i++) {
                outFile << myList[i] << endl;
            }
            outFile.close();
            return 0;
        }
    std::list has no operator []. Replace it with an std::vector and it'll work fine. Otherwise, just change the loop to:

    Code:
    for (list< string >::const_iterator it = myList.begin( ), end = myList.end( ); it != end; it++) {
                outFile << *it << endl;
            }
    Incidentally, the above code works with virtually every type of container, so that idiom is most preferable...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In my opinion, the error is clear:

    Compiling: main.cpp
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox\main.cpp: In function 'int main()':
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox\main.cpp:23:37: warning: comparison between signed and unsigned integer expressions
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox\main.cpp:25:28: error: no match for 'operator[]' in 'myList[i]'
    Process terminated with status 1 (0 minutes, 1 seconds)
    1 errors, 1 warnings
    You can fix everything by using iterators:
    Code:
     for ( std::list<std::string>::iterator i = mylist.begin(); i != mylist.end(); ++i) ...
    std::list doesn't support random access because it models a linked list and linked lists don't have random access.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    Wow thanks all! Its been a long day and I seemed to have forgotten I was dealing with a list and not a vector. Thanks for pointing out the obvious mistake.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I hope nobody minds, but I "borrowed" this and tried it myself... got it working with the iterators as suggested...

    Now, if I was using a vector <string> instead of the list.... how would I sort that?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by CommonTater View Post
    I hope nobody minds, but I "borrowed" this and tried it myself... got it working with the iterators as suggested...

    Now, if I was using a vector <string> instead of the list.... how would I sort that?
    std::sort.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    See... now that was too easy...
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
      { ifstream inFile;
        vector<string> myList;
        string x;
        inFile.open("test.txt");
        while (inFile >> x)
          { myList.push_back(x); }
        inFile.close();
    
        sort(myList.begin(),myList.end());
    
        ofstream outFile;
        outFile.open("words.txt");            
        for (int i = 0; i != myList.size(); i++)
          {	outFile << *i << endl;
            cout << myList[i] << endl;  }
        outFile.close();
        return 0; }

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by CommonTater View Post
    See... now that was too easy...
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
      { ifstream inFile;
        vector<string> myList;
        string x;
        inFile.open("test.txt");
        while (inFile >> x)
          { myList.push_back(x); }
        inFile.close();
    
        sort(myList.begin(),myList.end());
    
        ofstream outFile;
        outFile.open("words.txt");            
        for (int i = 0; i != myList.size(); i++)
          {    outFile << *i << endl;
            cout << myList[i] << endl;  }
        outFile.close();
        return 0; }
    >> outFile << *i << endl;

    Except...'i' is an int.

    Here's yet another way to do it, by the way:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <iterator>
    int main()
    { 
        using namespace std;
        ifstream inFile("test.txt");
        vector<string> myList;
        copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(myList));
        sort(myList.begin(), myList.end());
        ofstream outFile("words.txt");            
        copy(myList.begin(), myList.end(), ostream_iterator<string>(outFile, "\n"));
        return 0; 
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sebastiani View Post
    >> outFile << *i << endl;

    Except...'i' is an int.
    Oh crap... I have it working but I must have copied that to the clipboard before I commented that line out... ... such is life...

Popular pages Recent additions subscribe to a feed