need help finishing the c++ program

Pages: 12
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
#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); 
//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
}


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
i am trying to do is read a file in to an array of structures

You haven't defined the structure anywhere. Start with that. Then write code to read into an instance of the structure. Then create an array (or vector) of the structures and use a loop to read the file into the array.

Finally, write code to sort the array. Can you use the standard sort() template?
im unsure of how to create a structure i know how to make a class if that helps but could you help me start or show me how?
im unsure of how to create a structure i know how to make a class if that helps but could you help me start or show me how?


In C++ a struct is almost the same as a class, the difference is the access: class is private by default; struct is public by default.

One could literally replace class with struct.
closed account (48T7M4Gy)
im unsure of how to create a structure

http://www.cplusplus.com/doc/tutorial/structures/
ok thank you but i just dont get why a class would help for this i have to do it but can someone explain to me why its useful in this situation thanks.
mikael200 wrote:
i am trying to read a file into an array of structures

The class is useful because it groups together all of the information associated with a single entry in the file. By doing this, you organize the data in such a way that it is easy to manipulate -- most importantly, you can define a total ordering between entries and sort entire entries at a time instead of attempting to manage multiple independent collections at once, which is much more difficult and very error-prone.

For lack of problem context I'll name the class "Entry":
1
2
3
4
5
6
7
8
struct Entry { 
  explicit Entry(std::string const& line) { /* create an entry from the given line */ };
  // etc.
};

bool operator <(Entry const& e1, Entry const& e2) 
{ /* compare two Entries */ }
bool operator >(Entry const& e1, Entry const& e2) { /* etc */ }


Each field on every line of your input file represents something. Create entries from it, then sort them.
Last edited on
damn i get it now the class would help with splitting a string into first name and last name and stuff like that and i can use the member functions to output to the output file what i want but how do i split the string up in the class if that makes sense
You can use std::istringstream to help with that:
1
2
3
4
5
6
7
...
explicit Entry(std::string const& line) { 
    std::istringstream s{line};
    if (s >> n1 >> first >> last >> n2) { 
        std::cout << "read entry: " << last << ", " << first << '\n';
    } else throw "uh oh!"; // couldn't parse an entry!
}
Demo:
http://coliru.stacked-crooked.com/a/eb34011c096916bf
Last edited on
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
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <sstream>

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
int get_line (ifstream& in, ofstream& out, vector <Presidents> &sort, const string &firstname, const string &lastname, const string &salary, const string &id); //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);

class Presidents {
	Public:
		string firstname;
		string lastname;
		string salary;
		string id;
		
		Presidents (const string &firstname, const string &lastname, const string &salary, const string &id) {
			firstname = firstname;
			lastname = lastname;
			salary = salary;
			id = id;
		}
};  
int main () {
	vector <Presidents> sort;
        ifstream in; //instream
        ofstream out; //outstream
        string filename, output; //input and output file
        get_file (in, out, filename, output);
        get_line (in, out, sort, );
//        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;
        }
}
int get_line (ifstream& in, ofstream& out, vector <Presidents> &sort, const string &firstname, const string &lastname, const string &salary, const string &id) {
        string line;
        while (getline (in, line)){ // using getline to read in a single line at a time
		string id, firstname, lastname, salary;
		istringstream iss (line);
		iss >> id >> firstname >> lastname>> salary;
		Presidents person (id, firstname, lastname, salary);
		sort.push_back (person);
	}
	sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.lastname < b.lastname; });
        for (Presidents i : sort) {
		out << i.id << " " << i.lastname << ", " << i.firstname << " " << "$" << i.salary << endl;
	}
                in.close (); //closes the input file
                out.close (); //closes the output file
		return 0;
}


ive used what you have given me and theres my code but i get so many compiler errors and i dont know how to fix them or what they mean but does anything look wrong with this?
Last edited on
im dumb i noticed i did not declare the class in the main and i didnt even call all the variables in this definition get_line (in, out, sort, ); but im still getting errors
closed account (48T7M4Gy)
First thing, please edit your last post so lines aren't more than about 80 characters wide.

As far as the errors are concerned just concentrate on the first one, fix it then recompile. Don't try and fix all of them at once.

The first one require you to place the first two functions after the clas definition for President. Just cut and repast the two lines after the class. Then recompile and see what happens.
NPP_EXEC: "Compile C++ File"
NPP_SAVE: C:\Users\lords_000\Downloads\pocketcpp\lab11.cc
g++ -o "C:\Users\lords_000\Downloads\pocketcpp\lab11" "C:\Users\lords_000\Downloads\pocketcpp\lab11.cc" -static -std=c++14
Process started >>>
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:13:2: error: 'Public' does not name a type
Public:
^~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc: In constructor 'Presidents::Presidents(const string&, const string&, const string&, const string&)':
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:20:16: error: passing 'const string {aka const std::__cxx11::basic_string<char>}' as 'this' argument discards qualifiers [-fpermissive]
firstname = firstname;
^~~~~~~~~
In file included from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\string:52:0,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\locale_classes.h:40,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\ios_base.h:41,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ios:42,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ostream:38,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\iostream:39,
from C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:1:
c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\basic_string.h:565:7: note: in call to 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
operator=(const basic_string& __str)
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:21:15: error: passing 'const string {aka const std::__cxx11::basic_string<char>}' as 'this' argument discards qualifiers [-fpermissive]
lastname = lastname;
^~~~~~~~
In file included from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\string:52:0,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\locale_classes.h:40,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\ios_base.h:41,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ios:42,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ostream:38,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\iostream:39,
from C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:1:
c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\basic_string.h:565:7: note: in call to 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
operator=(const basic_string& __str)
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:22:13: error: passing 'const string {aka const std::__cxx11::basic_string<char>}' as 'this' argument discards qualifiers [-fpermissive]
salary = salary;
^~~~~~
In file included from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\string:52:0,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\locale_classes.h:40,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\ios_base.h:41,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ios:42,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ostream:38,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\iostream:39,
from C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:1:
c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\basic_string.h:565:7: note: in call to 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
operator=(const basic_string& __str)
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:23:9: error: passing 'const string {aka const std::__cxx11::basic_string<char>}' as 'this' argument discards qualifiers [-fpermissive]
id = id;
^~
In file included from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\string:52:0,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\locale_classes.h:40,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\ios_base.h:41,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ios:42,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\ostream:38,
from c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\iostream:39,
from C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:1:
c:\users\lords_000\downloads\pocketcpp\mingw\include\c++\6.1.0\bits\basic_string.h:565:7: note: in call to 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
operator=(const basic_string& __str)
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc: In function 'int main()':
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:39:34: error: 'firstname' was not declared in this scope
get_line (in, out, sort, firstname, lastname, id, salary);
^~~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:39:45: error: 'lastname' was not declared in this scope
get_line (in, out, sort, firstname, lastname, id, salary);
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:39:55: error: 'id' was not declared in this scope
get_line (in, out, sort, firstname, lastname, id, salary);
^~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:39:59: error: 'salary' was not declared in this scope
get_line (in, out, sort, firstname, lastname, id, salary);
^~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc: In function 'int get_line(std::ifstream&, std::ofstream&, std::vector<Presidents>&, const string&, const string&, const string&, const string&)':
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:66:53: error: 'Presidents::Presidents(const string&, const string&, const string&, const string&)' is private within this context
Presidents person (id, firstname, lastname, salary);
^
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:19:3: note: declared private here
Presidents (const string &firstname, const string &lastname, const string &salary, const string &id) {
^~~~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc: In lambda function:
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:69:89: error: 'std::__cxx11::string Presidents::lastname' is private within this context
sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.lastname < b.lastname; });
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:15:10: note: declared private here
string lastname;
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc: In function 'int get_line(std::ifstream&, std::ofstream&, std::vector<Presidents>&, const string&, const string&, const string&, const string&)':
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:69:113: error: no match for call to '(std::vector<Presidents>) (std::vector<Presidents>::iterator, std::vector<Presidents>::iterator, get_line(std::ifstream&, std::ofstream&, std::vector<Presidents>&, const string&, const string&, const string&, const string&)::<lambda(const Presidents&, const Presidents&)>)'
sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.lastname < b.lastname; });
^
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:71:12: error: 'std::__cxx11::string Presidents::id' is private within this context
out << i.id << " " << i.lastname << ", " << i.firstname << " " << "$" << i.salary << endl;
^~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:17:10: note: declared private here
string id;
^~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:71:27: error: 'std::__cxx11::string Presidents::lastname' is private within this context
out << i.id << " " << i.lastname << ", " << i.firstname << " " << "$" << i.salary << endl;
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:15:10: note: declared private here
string lastname;
^~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:71:49: error: 'class Presidents' has no member named 'firstname'; did you mean 'lastname'?
out << i.id << " " << i.lastname << ", " << i.firstname << " " << "$" << i.salary << endl;
^~~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:71:78: error: 'std::__cxx11::string Presidents::salary' is private within this context
out << i.id << " " << i.lastname << ", " << i.firstname << " " << "$" << i.salary << endl;
^~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:16:10: note: declared private here
string salary;
^~~~~~
<<< Process finished. (Exit code 1)
================ READY ================
these are the errors i am getting i fixed the one you told me by putting the prototypes after the class declaration
Last edited on
You need to realize that C++ is case sensitive, Public is not the same as public and remember C++ reserved words are always all lower case.

Also if you're going to use parameters with the same names as your class member variables in your constructor must either use initialization lists ( preferred ) or properly scope the class member variables using the this pointer (this->member_variable_name).

By the way your error messages tell you the line you need to begin looking at for the problems:

C:\Users\lords_000\Downloads\pocketcpp\lab11.cc: In function 'int get_line(std::ifstream&, std::ofstream&, std::vector<Presidents>&, const string&, const string&, const string&, const string&)':
C:\Users\lords_000\Downloads\pocketcpp\lab11.cc:66:53: error: 'Presidents::Presidents(const string&, const string&, const string&, const string&)' is private within this context
Presidents person (id, firstname, lastname, salary);
^


And there can be multiple messages for the same problem, as in the above messages. The first line tells you the function in question, the second line tells you the line number.

Last edited on
kemort gave you a very good advice. If you get a lot of errors focus on the first one first. When you have fixed the first error compile the code to see if you still get errors. An error can lead to secondary errors so chances are that the list of error message will have been reduced by more than one. Repeat until you have no errors left.

first error wrote:
error: 'Public' does not name a type

The first problem you need to fix is that you have spelled public with an upper case 'P'.
My professor rights on a chalkboard and he spells public with an uppercase P so thank you for that correction i would have known what was wrong and thank you so much to mbpzzi for the code and the annotations are so nice dude thank you and thank you to jlb and everyone else who helped.
one more question do i have to include the this-> and what does that mean
I used this-> in a function which looked sort of like this:
1
2
3
4
class A { 
  int a;
  A(int a) { a = a; }
};

The problem is on line 3 -- a = a. You intend to say that the class member variable named a should be assigned the parameter named a, but that actually assigns the parameter a to the parameter a. One fix looks like this:
A(int a) { this->a = a; }
because this->a is exactly the member variable A::a, whereas inside the constructor a is exactly the parameter named a.

You can either add this-> or change the name of the parameters so there's no collision with the member variables, or use a member-initializer list where the this-> is implicit, or use the class name A::a instead of or in addition to this->

A search for "what is the this pointer" will yield plenty of results. You just have to make sure the names mean what you want.

I expanded on my example previously:
http://coliru.stacked-crooked.com/a/a676440314b66d88
Last edited on
Pages: 12