request for member error

here is the error

In function ‘void get_line(std::ifstream&, std::ofstream&)’:
lab11new.cc:49:17: error: request for member ‘at’ in ‘line.std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[]<char, std::char_traits<char>, std::allocator<char> >(((std::__cxx11::basic_string<char>::size_type)i))’, which is of non-class type ‘char’
if (line [i].at(0) < line [j].at(0)) {
^
lab11new.cc:49:34: error: request for member ‘at’ in ‘line.std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[]<char, std::char_traits<char>, std::allocator<char> >(((std::__cxx11::basic_string<char>::size_type)j))’, which is of non-class type ‘char’
if (line [i].at(0) < line [j].at(0)) {
^
lab11new.cc:50:25: error: conversion from ‘char’ to non-scalar type ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ requested
string tmp = line [i];
^
lab11new.cc:52:14: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
line [j] = tmp;


here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <algorithm>

using namespace std;

int get_file (ifstream& in, ofstream& out, string filename, string output); //this function opens the input file and output file and checks to see if they are open
void get_line (ifstream& in, ofstream& out); //this function brings the input file into a string and then changes all lowercase letters to uppercasse and leaves uppercase letters alone and changes digits to a * symbol then outputs its a$
//void name (ifstream& in, ofstream& out);
//void salary (ifstream& in, ofstream& out);

int main () {
        ifstream in; //instream
        ofstream out; //outstream
        string filename, output; //input and output file
        get_file (in, out, filename, output);
        get_line (in, out);
//        name (in, out);
//        salary (in, out);
}
int get_file (ifstream& in, ofstream& out, string filename, string output) {
        cout << "enter the file you would like to open" << endl;
        cin >> filename; //takes in the name of the file your opening
        in.open (filename.c_str()); //opens the file
        if (in.fail()) { //checks to see if the file is open
               cout << "that is not a file" << endl;
               return 0;
        }
        cout << "Input the name of the output file" << endl;
        cin >> output;
        out.open (output.c_str());
        if (in.fail()) {
                cout << "that is not a file" << endl;
                return 0;
        }
}
void get_line (ifstream& in, ofstream& out) {
        string line;
        while (getline (in, line)){ // using getline to read in a single line at a time
                for (int i = 0; i < 100; i++) {
		out << line [i] << endl;
	}

	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 100; j++) {
			if (line [i].at(0) < line [j].at(0)) {
				string tmp = line [i];
				line [i] = line [j];
				line [j] = tmp;
			}
		}

	}
	out<< endl;

	for (unsigned int i = 0; i < 100; i++) {
		cout << line [i] << endl;
	}
        //        out << line << endl; //output the whole string to the output file
        }
                in.close (); //closes the input file
                out.close (); //closes the output file
}

Last edited on
The string subscript operator returns a char.

http://www.cplusplus.com/reference/string/string/operator%5B%5D/
do i have to change my for loop to for (int i=0; i<str.length(); ++i) or do i have to return 0; anc change my function to an int or char?
Last edited on
this function brings the input file into a string and then changes all lowercase letters to uppercasse and leaves uppercase letters alone and changes digits to a * symbol then outputs its

the program comments state above to be the object of the get_line() function – to read the file into a string it'd be more efficient to allocate memory for the string first with the std::string reserve() method (this avoids re-allocations while the file is being read)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

int main ()
{
    std::string str{};

    std::ifstream file ("D:\\test.txt", std::ios::in|std::ios::ate);//file opens with file pointer at end (ios::ate)
    if (file)
    {
        auto filesize = file.tellg();
        str.reserve(filesize);//allocate memory for string

        file.seekg(0);//brings file pointer back to beginning
        while (file)
        {
            if (file) str += file.get();
        }
   }
  std::cout << str << "\n";
  for (auto & elem : str)
  {
      if(std::islower(elem))
      {
          elem = std::toupper(elem);//change lower case to upper case
      }
      if(std::isdigit(elem))
      {
          elem = '*';//replace digits with *
      }
  }
  std::cout << "Revised string \n" << str << "\n";
}


this mode of reading a file into a string is from the cplusplus.com:
http://www.cplusplus.com/reference/istream/istream/tellg/
however the use of tellg() does have its critics:
http://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file and this link will also provide alternative ways of getting the file size first
This loop is wrong; it will place a spurious character (the result of the narrowing conversion of
std::ifstream::traits_type::eof() to char) at the end of the string.
1
2
3
4
    while (file)
    {
        if (file) str += file.get();
    }
gunnerfunner my comments are for a previous code so what i am doing in the function is different i am trying to do is read a file in to an array of structures and i have to sort a list like this

1000 George Washington 10000
2000 John Adams 15000
1212 Thomas Jefferson 34000
1313 Abraham Lincoln 45000
1515 Jimmy Carter 78000
1717 George Bush 80000

by alphabetical order by last name and there is supposed to be a comma after the last name like washington, george
Last edited on
since you have to sort the list eventually it'd be better to word with an ordered container like std::set throughout, assuming each president appears only once:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <set>
#include <fstream>
#include <sstream>

struct President
{
    double m_id = 0;
    std::string m_firstName = " ";
    std::string m_lastName = " ";
    double m_votes = 0;
    President(){}
};
struct PrezCompare//custom compare for std::set
{
    bool operator () (const President& lhs, const President& rhs)
    {
        return lhs.m_lastName < rhs.m_lastName ;
    }
};

std::ostream& operator << (std::ostream& os, const President& p)//for printing
{
    os << p.m_lastName << ", " << p.m_firstName << " " << p.m_id << " " << p.m_votes << "\n";
    return os;
}

int main()
{
    std::set<President, PrezCompare> presidents{};//std::set with custom compare
    std::ifstream inFile("D:\\test.txt");
    if(inFile)
    {
        std::string line{};
        while (getline(inFile, line))
        {
            std::istringstream stream{line};
            President p{};//default ctor
            stream >> p.m_id >> p.m_firstName >> p.m_lastName >> p.m_votes;//overwrite default values
            if(inFile)//check file is still open (good practice but not strictly reqd for std::set as each item is unique)
            {
                presidents.insert(p);
            }
        }
    }
    for (const auto& elem : presidents)std::cout << elem ;
}

thats the thing i think they appear multiple times because i have to go through again and sort by salary and output it to the same file just under it if that makes sense and thanks for the help dude your awesome
Topic archived. No new replies allowed.