|
Thanks a ton. Your solution sounds a lot better (and simpler) than the solutions I was coming up with. Ignore the last bit you were confused over - it isn't important.
|
|
|
|
|
Have a name followed by 10 int's and it repeats for 15 names. Have a program that will input just the int into a 2d array if no string is involved. Need to somehow input the names from the file (every 11 lines) into a seperate array. Any suggestions?
|
|
|
|
|
Ok, so what part is troubling you? Be specific now. No teasing!
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
File i have the code for is like:
34
34
45
32
....
File i need to be able to get grades from is:
Name
35
54
34
44
34
54
34
54
34
54
Name
34
34
...
Need help on how to somehow get the name strings into an array somehow. As i have it now it is just inputFile >> data[students][tests] which is a declared 2d array with 15 students and 10 tests.
|
|
|
|
|
This is a starting place (it isn't complete, but it should get you started):
#include <string>
#include <vector>
#include <algorithm>
#include <interator>
#include <iostream>
#include <fstream>
class Student
{
public:
Student() : _Name("") {}
~Student() {}
void setName(const std::string& name) { _Name = name; }
std::string& getName() const { return _Name; }
void setGrades(const std::vector<int>& grades) { _Grades.assign(grades.begin(), grades.end()); }
std::vector<int>& getGrades() const { return _Grades; }
private:
std::string _Name;
std::vector<int> _Grades;
};
std::ostream& operator<<(std::ostream& os, const Student& s)
{
os << s.getName() << " ";
const std::vector<int> grades = s.getGrades();
std::copy(grades.begin(), grades.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}
std::istream& operator>>(std::istream& is, Student& s)
{
string name = "";
is >> name;
std::vector<int> grades;
std::copy(std::istream_iterator<int>(is), std::istream_iterator<int>(), std::back_inserter(grades));
s.setName(name);
s.setGrades(grades);
return is;
}
int main()
{
std::ifstream fin;
std::vector<Student> students;
fin.open("MyData.txt");
std::copy(std::istream_iterator<Student>(fin), std::istream_iterator<Student>(), std::back_inserter(students));
fin.close();
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
So in the operator>> method, the Student object that gets passed in is modified (i.e., its setName() and setGrades() methods are called), but the students vector in main() never gets any Student objects added to it. Isn't this what the call to copy() is supposed to be doing?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
It should be getting 1 student only (due to the previously mentioned bug in that example). Fixing the bug will allow it to copy all the students in the file to the vector. The easiest way to do this without having to modify the data file format is to modify the input stream extractor to get the id and then calling getline to store the rest of the line as a string. Then create a stringstream object and extract the grades (everything in the streamstream) into the grades vector using a copy operation. Basically, you add 2 lines of code and change the iterators for the grades-copy call.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I'm still learning this C++ stream stuff, so I don't think we are on the same page here, Zac. For an input file having a format of:
bob 1 2 3 4 5
mary 2 3 4 5 6 7
nathan 9 8 7 6
The first line of the file is read correctly by the operator>> method. The name is stored in the name variable, and the grades are stored in the grades vector. Those then get copied successfully to the Student object. The problem is that the students vector in main() does not get any Student objects added to it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Here is a working example with the data format you gave:
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Student
{
public:
Student() : _Name("") {}
~Student() {}
void setName(const string& name) { _Name = name; }
string getName() const { return _Name; }
void setGrades(const vector<int>& grades) { _Grades.assign(grades.begin(), grades.end()); }
vector<int> getGrades() const { return _Grades; }
private:
string _Name;
vector<int> _Grades;
};
std::ostream& operator<<(std::ostream& os, const Student& s)
{
os << s.getName() << " ";
const vector<int> grades = s.getGrades();
copy(grades.begin(), grades.end(), ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}
std::istream& operator>>(std::istream& is, Student& s)
{
string name = "";
is >> name;
string sGrades = "";
getline(is, sGrades);
vector<int> grades;
stringstream ss(sGrades);
copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(grades));
s.setName(name);
s.setGrades(grades);
return is;
}
int main()
{
ifstream fin;
vector<Student> students;
fin.open("data.txt");
copy(istream_iterator<Student>(fin), istream_iterator<Student>(), back_inserter(students));
fin.close();
std::copy(students.begin(), students.end(), ostream_iterator<Student>(cout, "\n\n"));
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks, Zac. So by having copy() read from a stringstream rather than a istream , how did that make a difference? The grades vector had the correct number of items added to it either way.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Putting the grades in a stringstream prevents the istream from trying to read the rest of the file. When the istream gets to the end of the file (that is, when the >> returns an eof()) it doesn't add anything to the vector (which is what you were seeing).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
That helps. Thanks.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I came accross this little issue with the a CPrintDialog dialog.
Using the debug build of my code when I display the print setup dialig with the DoModal() method, I get a dialog with the attached printers displayed in a ListView. But in my release build of my code I get a dialog showing the attached printers in a combo-box! Also, in the debug version of the code selecting one of the printers to be the default printer does actually set the selected printer as the default printer but doing the same using the release build the default selection isn't retained by the OS! Both the release and debug builds are using the exact same code and there are no debug defines!
I would appreciate anyones advice on why this is happening, and any way to overcome the problem with the release build not being able to set the default printer, whereas the correct functionality is obtained in the debug build.
|
|
|
|
|
Once again this blasted IDE has brought this neophyte to his knees. My question is: how do you set the system path in Visual Studio 2003. I have been toiling away for the better part of 3 hours to try and get it to see the DLL that's on my hard drive, but it can't see it. Truthfully, I feel like crying.
Alas, any help you can give me would be greatly appreciated - as always.
Peace,
BP
|
|
|
|
|
Here's the search order for DLLs, from the platform SDK
The directory from which the application loaded.
The current directory.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
Windows Me/98/95: This directory does not exist.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
|
|
|
|
|
How do I tell it which directory to look in? How do I add another directory to the PATH environment variable? I can't find where to set it.
Thanks,
BP
|
|
|
|
|
How To Manage Environment Variables in Windows XP[^]
From your code, if the dll is being implicitly linked (you are not calling LoadLibrary()) then
you'll need to keep the dll in the current search path.
If using LoadLibrary you can specify the path.
To set the PATH environment variable for the current process use SetEnvironmentVariable() API.
To change for all processes it's in the registry, I think at
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment (Path variable is
semi-colon delimited series of paths).
|
|
|
|
|
Wow! Thanks, it worked! You don't know how much you have helped me.
Thanks again,
BP
|
|
|
|
|
I am using MS Visual Studio 2005 and i have a problem with message events.
***TestApp.h***
class CTestApp : public CWinApp<br />
{<br />
public:<br />
CTestApp();<br />
virtual ~CTestApp();<br />
virtual BOOL InitInstance();<br />
afx_msg LRESULT OnMsg(WPARAM wParam, LPARAM lParam);<br />
DECLARE_MESSAGE_MAP()<br />
};
***TestApp.cpp***
#include "stdafx.h"<br />
#include "TestApp.h"<br />
<br />
#define WM_MY_EVENT (WM_APP + 100)<br />
<br />
BEGIN_MESSAGE_MAP(CTestApp, CWinApp)<br />
ON_MESSAGE(WM_MY_EVENT, OnMsg)<br />
END_MESSAGE_MAP()<br />
<br />
void FileWrite(char *data)<br />
{<br />
FILE *f;<br />
errno_t err;<br />
err = fopen_s(&f, "C:\\debug.txt", "a");<br />
fprintf(f, data);<br />
fflush(f);<br />
fclose(f);<br />
}<br />
<br />
CTestApp::CTestApp()<br />
{ <br />
FileWrite("Constructed\n");<br />
}<br />
CTestApp::~CTestApp()<br />
{<br />
FileWrite("Destructed\n");<br />
}<br />
<br />
CTestApp theApp;<br />
<br />
BOOL CTestApp::InitInstance()<br />
{<br />
FileWrite("Initialized\n");<br />
return false;<br />
}
This is an error message: error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CTestApp::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
how can i fix that?
Thank you in advance
|
|
|
|
|
Try ON_THREAD_MESSAGE in the app class.
Use ON_MESSAGE in a window class.
Mark
|
|
|
|
|
then it gives new error message
error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CTestApp::* )(WPARAM,LPARAM)' to 'void (__thiscall CWinThread::* )(WPARAM,LPARAM)'
|
|
|
|
|
Sorry about that...your function should return void, not LRESULT
|
|
|
|
|
dohh.. it is alive thank you
|
|
|
|
|
edvintas wrote: afx_msg LRESULT OnMsg(WPARAM wParam, LPARAM lParam);
Are you certain you definition of OnMsg match the declaration ?
|
|
|
|
|
now it matches
so finally changes are:
in *.cpp added
void CTestApp::OnMsg(WPARAM wParam, LPARAM lParam)<br />
{<br />
}
and in *.h
LRESULT to void and ON_MESSAGE to ON_THREAD_MESSAGE
hurahhh...
|
|
|
|