JoBe 36 Posting Pro in Training

Hello ladies and gents,

I wanted to ask if any of you could help me out here, Ive got this program:

//Test programma voor Accelerated C++.

#include <fstream>
#include <iostream>
#include <string>

int main(int argc, char** argv)
{
	int fail_count = 0;
	//for each file in the input list
	for (int i = 1; i < argc; ++i)
	{
		std::ifstream in(argv[i]);

		//if it exists, write its contents, otherwise generate an error message
		if (in)
		{
			std::string s;
			while (getline(in, s))
				std::cout << s << std::endl;
		}
		else
		{
			std::cerr << "cannot open file " << argv[i] << std::endl;
			++fail_count;
		}
	}
	return fail_count;
}

The program should copy the content of any file that I give as arguments to main.

What I did is, I copied the Test.exe to my C harddrive, I then created a .txt file named Tekstfile and stored that also on my C harddrive.

I then use the cmd command and enter cd c:\
I then type Test and Tekstfile.

I thought this would make the program read what is written in Tekstfile and write this in Test, but, I'm getting the error message every time saying "cannot open file" , apparantly I'm doing something wrong :?:

Could any of you help me out, thanks.

JoBe 36 Posting Pro in Training

Exactly the same problems I went through while I installed Visual Studio 2005 for the first time. Try setting Embed Manifest to No. See attachment.

Pffffffff, man oh man, finally, it's working, Narue and especially WolfPack, thanks for your patience, really appreciated :!: :!:

Can I ask WolfPack, do you have a manual from Visual C++ 2005? If not, how do you know these solutions, or is it experience with VC++ 2005?

JoBe 36 Posting Pro in Training

To enable it follow the steps in the link I provided in the previous post.

Yep, that did the trick WolfPack, I can now compile it without getting any errors.

There's only one problem now, when I want to execute with F5, I get a message that says the following:

This exe file can't be run because the MSVCR80D.dll couldn't be found. Reinstalling this will solve the problem.

Found it on my computer:
C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c

Question is, where do I have to put it????
Thanks for all the assistance allready :!:

JoBe 36 Posting Pro in Training

>error LNK2019: unresolved external symbol _main referenced in function
You're using a console application?

Euhm, I'm using Win32 Console Application :o

I'm presuming that isn't right :-|

If that's not correct, which one do I have to use then, and also, how do I create this with an empty proj. ????

Options are:
- Class Library
- CLR Console Application
- CLR Empty Project
- Makefile Project
- Win32 Console Application
- Windows Forms Application
- Empty Project

JoBe 36 Posting Pro in Training

... and run this code and tell me what happens.

I get the following error message:

WinApi.obj : error LNK2019: unresolved external symbol __imp__MessageBoxW@16 referenced in function _WinMain@16
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Visual Studio 2005\Projects\WinApi\Debug\WinApi.exe : fatal error LNK1120: 2 unresolved externals

Just the same as before :o

JoBe 36 Posting Pro in Training

Byte Character Set" or "Not Set", and compile. It should work.

Neither worked WolfPack, I get three errors eachtime:

WinApi.obj : error LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function _WinMain@16
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Visual Studio 2005\Projects\WinApi\Debug\WinApi.exe : fatal error LNK1120: 2 unresolved externals

I did add them into the separate include, lib and executable files and changed the character set to those two options you said, it didn't help.

JoBe 36 Posting Pro in Training

I'm inclined to think that your Visual Studio setup is conflicting with your needs somewhere...

Think that could be it, when installing the SDK, I had to add three directories:

$(ProgramFiles)\Microsoft Platform SDK\lib
$(ProgramFiles)\Microsoft Platform SDK\bin
$(ProgramFiles)\Microsoft Platform SDK\include

The way it was showed on the video, it seemed that these three had to be added to the executable files, but, when I did that, I got an error message saying that <windows.h> could not be found.

When I added the "$(ProgramFiles)\Microsoft Platform SDK\include" directory to the include files, that problem was gone, however, I'm now getting the mentioned error.

Do I have to include all three directories to the executable-include- and library files?

It was showed that I had to add them by pressing tools->options->Projects and Solutions->VC++Directories->there I found the several files, but as said, it seemed as if I had to add the three directories only into the executable file, but this gave me the error that <window.h> couldn't be found???

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I just wanted to see what this small programs does so I tried it in Dev-C++ and in VC++ 2005, thing was, it worked in Dev-C++ but not in VC++ 2005 in which I got an error stating the following:

error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

The program I tried was one which Ancient Dragon gave a link to in another thread:

// WinApi.cpp : Defines the entry point for the console application.

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
    return 0;
}

Could anyone tell me why it works in the one, but not in the other :?:

Thanks.

JoBe 36 Posting Pro in Training

Thanks for the additional info WolfPack, because of that, I read the instructions a bit better, turns out I only need to download the PSDK-x86 file, and this makes it possible for me to decide what I want to download and what not :o

Sorry for this, but, this is what you get when amateurs try something like this :mrgreen:

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive downloaded the first four PSDK-FULL files and wanted to install these, but I don't know how exactly to do so.

I tried to use winzip and extract them, but keep getting an error that says, failed to launch file: C\Documents and Settings\Johan Berntzen\Local Settings\Temp\install\setup\PSDK-x86.msi

I don't understand what I'm doing wrong, I also downloaded the extract winzip and when extracting this, I get the Extract tool, but when I click on this, it comes up and goes down again, do I have to use this tool in a certain way?????

THe files that I downloaded can be found here:
http://www.microsoft.com/downloads/details.aspx?familyid=EBA0128F-A770-45F1-86F3-7AB010B398A3&displaylang=en

Do I actually need to download them all, because there are 16 files, do I need them all???? I think it's really confusing because nothing is written that says what is needed for what :o

JoBe 36 Posting Pro in Training

Well, euhm, it's working, but, how, .... don't know, I exited the program, reopened it, opened the project and I could use Step Into, Step Over, Show Next Statement, ...

The only thing that I can't see yet is, when I use for instance Step Into, in VC++ 6.0 I could see the variable being implemented into the program, how can I arrange this in VC++ 2005????

Anyway, thanks very much guys, I made the changes you instructed and apparantly, I can now atleast use the debugger :!:

JoBe 36 Posting Pro in Training

Oh, now I understand -- reading from keyboard not a data file. It makes sense now. But should use getline(), not read. And use record.name.

while (getline(cin, record.name))

Wait, so you're saying that I have to change the program just so that I could debug it ?

read(cin, record) is a function, so in main, I'm calling this function, by changing this, won't I deny passage to that function then?

JoBe 36 Posting Pro in Training

Another thing, can you tell me what is in the field "Generate Program Database File" that is located right below the "Generate Debug Info" field?

This is written there WolfPack:
$(TargetDir)$(TargetName).pdb

JoBe 36 Posting Pro in Training

And make sure "Solution Configurations" is set for Debug, not Release

Where can I see that this is correct AD?

JoBe 36 Posting Pro in Training

Although setting the breakpoint at the current position looks okay, for now set the breakpoint at

maxlen = max(maxlen, record.name.size());

and see if the program stops at that point.

Did that, same result.

If you already didnt know you have to press F10 to execute the code line by line. If it doesnt stop even then, there is someother problem, but I cant seem to get the cause.

When I hit F10, I get Assembler code, not the code I can follow to see how and what the code is doing.

Could it be that I can change the Assembler code into C++ code so that I can follow it?

JoBe 36 Posting Pro in Training

is record.name a std::string object?

Hi AD,

It's a struct which looks like this:

struct Student_info
{
	std::string name;
	double midterm, final;
	std::vector<double> homework;
};

The thing I want to do is, input values that are needed per Student like this:
Johan 80 80
75
79
80
and when this is written, go further into the program to see what the program is doing and how.
Ive tried to put breakpoints in other sections of the program but keep getting the same result as in the breakpoint changing from a full point to being just a circle with an exclamation '!' and saying the above sentence "The breakpoint will not currently be hit. No symbols have been loaded for this document."

JoBe 36 Posting Pro in Training

Sure, here it is:

vector<Student_info> students;
	Student_info record;
	string::size_type maxlen = 0;					//the length of the longest name

	//read and store all the students data
	//Invariant:	studentscontains all the student records read so far
	//				maxlen contains the length of the longest name in students
	while (read(cin, record))
	{
		//find length of longest name
		maxlen = max(maxlen, record.name.size());
		students.push_back(record);
	}

Ive putted the breakpoint on the

while (read(cin, record))

line.

JoBe 36 Posting Pro in Training

Thanks for the help WolfPack, that solved the first problem, but not the second problem wich is the breakpoint.

It still says: "The breakpoint will not currently be hit. No symbols have been loaded for this document." and when I enter the required info into the program so that it can get towards the breakpoint, the program just runs as it should and then exits succesfully, without giving me the opportunity to actually debug it and see every step beginning from the breakpoint.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I wanted to ask if any of you could tell me what I'm doing wrong when I want to debug any program that Ive written in VC++ 2005 Express Edition.

Reason is, after I put a break point to where the program has to run and then debug and push the debug button, I get a message going like this:

Debugging information for 'AccCppEx.exe' cannot be found or does not match. Binary was not build with debug information.

Do you want to continue debugging?

After that, the break point becomes a circle with an exclamation '!' and says that:

The breakpoint will not currently be hit. No symbols have been loaded for this document.

Can someone help me out here, because I'd like to see what my program is doing so I can make alterations to it.

Thanks for any help.

JoBe 36 Posting Pro in Training

Hi Jobe,
I have just started learning c++ after going through c.
I am still learning the concepts so at this moment I can't answer your problem. I am a learning so I am just looking for different types of problems so I asked you for your one.. :o Thanks for posting such a huge reply.. sorry I couldn't help..

No problem HW, have fun in learning C++, I know I have ;)

but i will see if someone else helps..
bye and sorry again!!!!

LOL, Ive noticed it, and though I think it's nice, it really isn't necessary, I'm sure if anyone can help, they'll do so. Reading your reply in Comwizz his thread towards Narue, she isn't obligated to help, if she wants to, she will, apparantly for a certain reason, she feels that it's better not to, whatever that might be, I respect that decission.

Anyway, if any have usefull tips, don't be shy and please tell, I'm at a stand still. Though not giving up yet ;)

JoBe 36 Posting Pro in Training

Hi guys,

Just thought that it would be easy for you if you could see the full version of this example, so, I included a zip file.

JoBe 36 Posting Pro in Training

Well seeing as noone replied with any answer to the question, I thought it best if I tried to find the solution one step at a time, afterall, I have to know it.

So, starting of with the Student_info structure:

struct Student_info
{
	std::string name;
	double midterm, final;
	std::vector<double> homework;
};

and looking at the exercise I decided to remove two double variables midterm, final and the vector homework and, adding another variable called finalGrade.

Reason for removing them is that all the grades have to be calculated in the functions themselves and you only have to save the final grade and for that I don't see a reason why I should keep the vector.

I was thinking of adding another variable int count to it, so, I could count the amount of grades I have entered, but, then I thought, this could be a local variable aswell because the calculation happens in the function.

So, my structure looks like this now:

struct Student_info
{
	std::string name;
	double finalGrade;
};

Going to the read-functions:

istream& read(istream& is, Student_info& s)
{
	is >> s.name >> s.midterm >> s.final;

	read_hw(is, s.homework);	//read and store all the student's homework grades

	return is;
}

istream& read_hw(istream& in, vector<double>& hw)
{
	if (in)
	{
		//get rid of previous contents
		hw.clear();

		//read homework grades
		double x;
		while (in >> x)
			hw.push_back(x);

		//clear the stream so that input will work for the next student
		in.clear();
	}

	return in;
}

The …

JoBe 36 Posting Pro in Training

Hello,

I'm wanting to know whats in the string and where: I've got this code

string s1="HelloWorld";

if(s1[5] = "W")

now then if i want to know if World is in the string. I've tried doing:

the if....

but it errors:

#include <iostream>
#include <string>

int main()
{
	std::string ex = "Hello World";

	if (ex[6] == 'W')
		std::cout << "W exists in the string." << std::endl;
	std::cout << "Press enter to exit." << std::endl;
	std::cin.get();

	return 0;
}
JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive been trying to find a solution for this exercise wich goes as follows:

Rewrite the Student_info structure, and the 'read' and 'grade' functions, so that they calculate each student's grades as part of reading the input, and store only the final grade.

Because the exercise is 'rather large', I won't post it, but was wondering if any of you, who have Accelerated C++ have the solution for this exercise. I have searched threw Google but came up with nothing and it's the last exercise from chapter four I have to do. But, thing is, though I'm moving on to the next chapter, I would love to find out what it is actually the solution for this exercise.

So, if any of you guys or girls have it, I would appreciate it if you could share it, I'm just dying to know how I should have solved it.

Thanks.

JoBe 36 Posting Pro in Training

That it returns a pointer to a double...

Darn, should have seen it, because of f() [n] :confused: It couldn't have been a double.

double *f()

Possible usage:

Thanks for the example Dave. :!:

JoBe 36 Posting Pro in Training

Hello ladies and gents,

There's an exercise, well, more a question that goes like this in Accelerated C++:

If the following code is legal, what can we infer about the return type of f?

double d = f() [n];

What I can gather is that the return type will be a double variable, but, I don't know wether this way of writing is actually correct?

Ive tried to use it, but failed in getting anything to work with this kind of function.

Could someone tell me wether this is a legal piece of code.

Thanks.

JoBe 36 Posting Pro in Training

Thanks for the c/c jwenting, Ive changed those know.

I used the median function name because I wanted to use the median to find the average, didn't succeed in using it though, so, had to find another way.

Changed the return from vec to void.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Had to make the following exercise in Accelerated C++

Write a program to calculate the average of the numbers stored in a vector<double>.

THis was my solution:

#include <iostream>
#include <iomanip>
#include <vector>

using std::cin;			using std::istream;
using std::cout;		using std::setprecision;
using std::endl;		using std::vector;				

istream& vec(istream&, vector<double>&);
double median(vector<double>&);

int main()
{
	vector<double> numbers;

	cout << "Fill the vector with double values. To quit, press Ctrl 'z'." << endl;
	vec(cin, numbers);

	cout << "The average of these numbers is " << setprecision(2) << median(numbers) << endl;

	cin.ignore(1, '\0');
		
	return 0;
}

istream& vec(istream& is, vector<double>& number)
{
	double numb;

	while (is >> numb)
		number.push_back(numb);
	cin.clear();

	return is;
}

double median(vector<double>& nr)
{
	double average = 0.0;
	
	for (int i = 0; i < nr.size();++i)
		average += nr[i];

	return average/nr.size();
}

Would you change anything in it, and if so, why?

Thanks.

JoBe 36 Posting Pro in Training

It looks very similiar to the last problem you posted. Seeing as you turned out a solution for the last one, I see no reason why your solution for this one would be incorrect.

Hi iamthewee,

Well, the thing is, it's not about the solution of finding the exact number or how manny of each words there are, like you said, that was from a previous exercise. It's about wether I interpreted the exercise correctly in that I had to write ONE function and that with this function I had to search for the two other required items.

Thing is, those remaining two, should they have been written in seperate functions or as I did, included in the main function?

That is actually the question.

I know that it does what it was supposed to do, the doubt I have is wether it was this way it was ment to be done.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I had to do the following exercise:
Write a (1)function that reads words from an input stream and stores them in a vector. Use that function both to write programs that (2)count the number of words in the input, and to (3)count how many times each word occured.

My solution for this was the following:

Write the header file for the function:

#ifndef GUARD_WordLibrary_h
#define GUARD_WordLibrary_h

//WordLibrary.h

#include <string>
#include <vector>

void readWords(std::istream&, std::vector<std::string>&);

#endif

Write the .cpp file for the function:

//(1)WordLibrary.cpp file

#include "WordLibrary.h"

using std::istream;				using std::vector;
using std::string;

void readWords(istream& input, vector<string>& aWord)
{
	if (input)
		aWord.clear();

	string str;
	while (input >> str)
		aWord.push_back(str);
}

Write the main.cpp file in wich I call the function and then present the amount of words and the word count.

//Main file

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "WordLibrary.h"

using std::cin;				using std::sort;
using std::cout;			using std::string;
using std::endl;			using std::vector;

int main()
{
	vector<string> word;
	int amount = 1;
	char ch = 'n';

	while (ch != 'q' && ch != 'Q')
	{
		cout << "Read several words into a vector threw use of a function." << endl;
		readWords(cin, word);
		
		//(2)Show the amount of words in the vector
		cout << "The amount of words in the vector are " word.size() << " ." << endl;

		sort(word.begin(), word.end());
		
		//(3)Show how manny times each word appears in the vector
		for (int i = 0; i < word.size()-1; ++i) …
JoBe 36 Posting Pro in Training

@ campkev, darn, I tried to do something like you but dividing by two. Thanks.

@ winbatch, I tried to use a string in which I thought I could use the string.length() to see how much numbers there where. It did give me the correct amount of numbers, but didn't lend itself of being used in combination with the iteration of the loop and I couldn't get it to be incremented either.

So, thanks for your solution gents, here's my exercise with both your examples integrated.

int squares (const int&, const int &);
int maxNumber (int);
int maxWidth (int);

int main()
{
	int maxlen;
	cout << "Give an integer number, bigger then 0 for wich \n"
		    "you wish to calculate it's squares." << endl;

	do
	{
		cin >> maxlen;
		if(maxlen <= 0)
			cout << "You've entered a wrong number, please try again." << endl;
	}
	while (maxlen <= 0);

	int widthNumbers = maxNumber(maxlen);
	int widthMaxNumbers = maxWidth(maxlen);

	cout << "The results are:" << endl;

	for (int i = 1; i <= maxlen; ++i)
		cout << setw(widthNumbers) << i << setw(widthMaxNumbers)  << squares(i, i) << endl;
	

	std::cout << "Press enter to exit." << std::endl;
	std::cin.ignore(2, '\0');

	return 0;
}

int squares (const int& x, const int& y)
{
	int result = x * y;

	return result;
}

int maxNumber (int maxLength)
{
	int count = 1;

	while (maxLength > 0)
	{
		maxLength /= 10;
		count++;
	}

	return count;
}

int maxWidth(int maxLength)
{
	ostringstream dig;

	int maxwidth = …
JoBe 36 Posting Pro in Training

Hello ladies and gents,

Was wondering if any of you could help me out, how can I count the amount of numbers(digits) there are in one integer, for example:

int integer = 123;

integer has three digits.

int secInteger = 10;

secInteger has two digits.

The reason for asking is, that I want to use this number to actually use "setw(integer)" so that it takes on the width of this number.

I'm trying to create two columns, one wich has an index of i from any number that I enter, the second will have the square of each value of i.

It's for this exercise in Accelerated C++

#include <iostream>
#include <iomanip>

using std::cin;				using std::setw;
using std::cout;			using std::string;
using std::endl;

int squares (const int&, const int &);

int main()
{
	int maxlen;
	cout << "Give an integer number, bigger then 0 for wich "
		    "you wish to calculate it's squares." << endl;

	do
	{
		cin >> maxlen;
		if(maxlen <= 0)
			cout << "You've entered a wrong number, please try again." << endl;
	}
	while (maxlen <= 0);

	int width = maxlen * maxlen;	//this will give me the maximum value, but, I need
									//the amount of digits that it contains.

	cout << "Calculate the squares of " << maxlen << " sequential values." << endl;

	for (int i = 1; i < maxlen; ++i)
		cout << setw(?) << i << setw(?)  << squares(i, i) << endl;
	

	std::cout << "Press …
JoBe 36 Posting Pro in Training

Thanks for the extended info Lerner, that really made it much clearer :!:

JoBe 36 Posting Pro in Training

Ah, I see.
Thanks AD

So for the Pro version you're getting the entire Visual Studio (including C#, VB, J#). That's why the extra cost.
In addition to the standard version you're getting tools for developing Windows CE applications, remote debugging support, XSLT debugging, and "visual tools for Microsoft Office" (whatever that is).
All unrelated to C++ development unless you want to write for PDAs.

Thanks jwenting,
So, as a hobbiest, could I buy the Express Edition then and be sure I could use it for those things that I want to do.

- Write plug-ins for Lightwave?
- Make window programs?
- Write code in C instead of C++?

Or, would I be better of with buying the Standard Edition?

JoBe 36 Posting Pro in Training

Since you want to spend your money, buy Pro edition. The Express is FREE -- you can't buy it.

Hi AD,

Then why is it €54.90 for the Express Edition on the site I gave the link to? Is this just because you get a CD with a manual then?

Ive looked at the Pro Edition, that really is expensive, think the Standard Edition would do then.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive recently downloaded VC++ 2005 EE, but, would prefer to buy a version of this. Now, the problem is that there are so manny versions. Since I'm a hobbiest I was wondering wich of these two would suit me the best.

Visual Studio 2005 Express or Visual STudio 2005 Standard?
http://www.hcw.be/hcwcs/msdn.asp

What I want to do with it, once I have a descent understanding is to:

- create plug-Ins for a 3D app. called Lightwave.
- write small programs for windows with a variety on purposes.

Also, with one of those editions, is it possible to write code in C aswell, or do I need another IDE for that?

Thanks for your help.

JoBe 36 Posting Pro in Training

Thanks Lerner,

I made the exercise with the quartiles, it works, but was wondering wether you could tell me what I could improve in it.

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> myNumbers;
	int number;

	while (cin >> number)
		myNumbers.push_back(number);
	cin.clear();

	sort(myNumbers.begin(), myNumbers.end());

	int qrt = myNumbers.size()/4;
	int quarter = qrt;
	int j = 0;
	int count = myNumbers.size()/qrt;

	for (int i = 0; i < count; ++i)
	{
		cout << "Quartiles are [" << i << "]:" << endl;
		while  (j < qrt)
		{
			cout << myNumbers[j] << " ";
			++j;
		}
		qrt += quarter;

		cout << endl;
	}

	cout << endl;
	cout << "Press enter to exit." << endl;

	cin.get();

	return 0;
}

Also, regarding to your remark on those arrays, is that made because of a mistake I made in the first exercise I talked about in this thread?

Also, I have downloaded the VC++ 2005 EE IDE and when debugging a program, I allways get a message like this:

Debugging information for 'UsingVectors.exe' cannot be found or does not match. Binary was not built with debug information.

Do you want to continue debugging?

Yes No

But then I also have the opportunity to not show this dialog again.

Could you tell me what is happening and how I can prevent this message from appearing?


Thanks for your help.

JoBe 36 Posting Pro in Training

Adding another question to this, exercise 3-2 ask the following:

Write a program to compute and print the quartiles (that is, the quarters of the numbers with the largest values, the next highest quarter, and so on) of a set of integers.

Am I correct that I have to do the following:
Enter certain numbers like 13, 25, 39 and then calculate what the values are for 1/4, 1/2 and 3/4 for each integer?

If this is the case, then what is the relation towards using vectors for this???

Thanks.

JoBe 36 Posting Pro in Training

if you read the book correctly you'll find out that the grade you're working with here is a single double per student, which is calculated from the total series of grades before being stored in the vector.

Ah, I see, thanks. Think I thought it was more complicated then it really was.

Keeping 2 vectors in synch with each value in one relating to exactly one value in the other shouldn't be a problem.

Is this a decent solution:

while (ch != 'Q')
	{
		cout << "First students name: " << endl;
		while (cin >> name)
		{
			names.push_back(name);
			double totGrade = 0.0;

			cout << "Enter the grades.\n";
			for (int i = 0; i < 3; ++i)
			{
				cin >> grade;
				totGrade += grade;
			}
			grades.push_back(totGrade/3);
			
			cin.clear();
			cout << "Ad another student or press Ctr 'z'." << endl;
		}

		short count = names.size();
		
		for (int i = 0; i < count; ++i)
		{
			cout << "The median grade for " << names[i] << " equals " << grades[i] << ".\n";
		}

		cout << "Press Q to quit.\n";

		cin.clear();
		
		cin >> ch;
		ch = toupper(ch);
	}

Thanks for the help.

JoBe 36 Posting Pro in Training

Ok, thanks for the help.

JoBe 36 Posting Pro in Training

Hello ladies and gents, in Accelerated C++ I have an exercise wich goes as follows:

Write a program that will keep track of grades for several students at once. The programcould keep two vectors in sync. The first should hold the student's names, and the second the final grades that can be computed as input is read. For now, you should assume a fixed number of homework grades. We'll see in 4.1.3/56 how to handle a variable number of grades intermixed with student names.

What Ive got so far is this:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

int main()
{
	vector<string> names;
	string name;
	vector<double> grades;
	double grade;
	char ch = 'n';

	while (ch != 'Q')
	{
		cout << "First students name: " << endl;
		while (cin >> name)
		{
			names.push_back(name);
			cout << "Enter the three grades.\n";
			for (int i = 0; i < 3; ++i)
			{
				cin >> grade;
				grades.push_back(grade);
			}
			cin.clear();
			cout << "Ad another student or press Ctr 'z'." << endl;
		}

		short count = names.size();
		
		cout << setw(10) <<"Names:" << setw(10) << "Result 1:" << setw(10) << "Result 2:" 
			 << setw(10) << "Result 3:" << endl;
		for (int i = 0; i < count; ++i)
		{
			cout << setw(10) << names[i];
			for(int j = 0; j < amount; ++j)
			{
				cout << setw(10) << grades[j];
			}
			cout << endl;
		}

		cout << "Press Q to quit.\n";

		cin.clear();
		
		cin >> ch;
		ch = toupper(ch);
	}

	return 0;
}
JoBe 36 Posting Pro in Training
string ponter = new string;

Shouldn't that be

string* ponter = new string;

:?:

JoBe 36 Posting Pro in Training

Got it:

One solution is:

int main()
{
	vector<string> theWords;
	string aWord;
	short count, amount = 1;
	
	while(cin >> aWord)
		theWords.push_back(aWord);
	
	count = theWords.size();

	sort(theWords.begin(), theWords.end());

	for (int i = 0; i < count-1; ++i)
	{
		if (theWords[i] == theWords[i+1])
		{
			amount++;
		}
		else
		{
			cout << theWords[i] << " appears " << amount << " times." << endl;
			amount = 1;
		}
	}

	cout << theWords.back() << " appears " << amount << " times." << endl;

	cout << "Amount of words is " << count << endl;
			
	std::cout << "Press enter to exit.\n";
	std::cin.ignore(2, '\0');

	return 0;
}

I was just wondering what I could change this to make it better?

Meaning for instance, I have to use the count -1 in the loop, because, otherwise, I would go out of bound. Is there another way of solving that?

Thanks

JoBe 36 Posting Pro in Training

Its actually a theoritical question. You are not required to write any code to solve it.

However, it can also be proven with actual code & appropriate data.

You need to be able to explain how discarding any value read in (before input is complete) could actually be discarding the value that would have end up being the median.

Ok, thanks for the additional info :!: I think I could do this by putting an if statement in it, for example when the while loop is at N°3 it should enter 0 into the vector, this way the amount of vectors is correct, but the total amount isn't.

JoBe 36 Posting Pro in Training

Thanks for the help iamthewee, but, the count isn't the problem I have, it's putting the several words in the vector without the doubles.

from memory, that exercise is in the chapter on associative arrays.

If you think about how a map (or 2) might be useful, you will find the task much simpler than using vectors.

The exercise is in part 3, in wich you start working with batches of data, there is no explanation whatsoever about using maps, queus, ... The only example you get is a program in wich student grades are calculated.

Still haven't found the solution though.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I'm trying to do another exercise from Accelerated C++ in wich I have to write a program to count how many times each distinct word appears in its input.

What I have untill now is this,

int main()
{
	vector<string> theWords;
	string aWord;
	short count;
	short amount = 0;

	while(cin >> aWord)
		theWords.push_back(aWord);
	
	count = theWords.size();

	cout << "Amount of words is " << count << endl;

	for (int i = 0; i < count; ++i)
		for (int j = 0; j <= i; ++j)
			if (theWords[i] != theWords[j])
			{
				theWords[i] = theWords[j];
				++amount;
			}

	cout << "Different amount of words is " << amount;

	for (i = 0; i < amount; ++i)
		cout << theWords[i] << "\n";
			
	cout << "\n";

	std::cout << "Press enter to exit.\n";
	std::cin.ignore(2, '\0');

	return 0;
}

I'm getting the correct amount of different words, but, I'm having a problem in getting them displayed, I only get the first word in the list.

I know it has to do with this theWords = theWords[j];
but don't know how to get it right?

Could someone help me out. Thanks

JoBe 36 Posting Pro in Training

... see what happens, does the median change?

Well, if I enter a value but don't include it in the vector but add one to the amount of numbers that are entered, then the median won't be correct, thing is, I don't understand what the actual thing is I have to do in this exercise.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Chapter 3 exercise 3-1:

Suppose we wish to find the median of a collection of values. Assume that the we have read some values so far, and that we have no idea how many values remain to be read. Prove that we cannot afford to discard any of the values that we have read. Hint: One proof strategy is to assume that we can discard a value, and then find values for the unread--and therefore unknown--part of our collection that would cause the median to be the value that we discarded.

I don't understand it completly, do I have to enter values and then enter one value wich isn't incorporated into the vector, this then proves that by discarding a value will leave you with a wrong median?

JoBe 36 Posting Pro in Training

Thanks for the additional help guys, but, I seem not to be able to grasp how to write this copy constructor or assignement operator for the program. No worries though, it was just an exercise I was trying to make from a book.

Why I wanted to do it the way I described is because it was written that way in that book and with that code, I had to write them. Suppose like you've said, there are better ways to solve this, though, would have like to be able to solve it the way it was asked in that book.

Darn, so frustrating from time to time when you can't seem to grasp how the hell that piece of code is supposed to fit into that program :rolleyes: :D

I'll put it aside and return to it again when I understand the meaning of it all abit better because for instance, to me, it seems a copy constructor and an assignement operator are the same thing. Difference I seem to see is that a copy constructor make a deep copy in wich it gives the object it's own adress and an assignement operator gives one object the value of another object, wich is the same as copying right :?:

Pfff, so confusing from time to time :o

JoBe 36 Posting Pro in Training

Thanks for the help Lerner, but how do I call the copy constructor in main when I want to copy the last name that was entered then?

And also, could the copy constructor be written for the Lobby class aswell?

Thanks.