PDA

View Full Version : Help with C++



IFS
03-17-2009, 09:29 AM
I was wondering if anyone here can maybe help me with c++ im busy with a project and im strugeling with some part of it if anyone can help please pm me

Eris
03-17-2009, 10:02 AM
I was wondering if anyone here can maybe help me with c++ im busy with a project and im strugeling with some part of it if anyone can help please pm me

I know C++, but before I help you, I must ask if this is your first programming experience?

If so, C++ isn't the ideal language to start out with. You are much better off with something like python, which is a much better beginner's language without being a neutered kindergarten language that shelters you from the scary programming: You can write very powerful software in python.

If you are adamant about learning C++, there is a book called Thinking in C++ (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html) available online for free that contains most of the stuff you need to know.

IFS
03-17-2009, 10:35 AM
this isn't my first programming experiance I did c# before c++, the thing im struggleing with isn't really a major issue its just something that my books not explaining to well im still busy with my course and the project im doing is for the course I just wan't someone to explain it to me and give me a proper example im not allowed to ask for help seeing as this counts for marks but I am allowed to use the net as refference

Eris
03-17-2009, 10:36 AM
Ask away then.

IFS
03-17-2009, 11:01 AM
okay like i said this is a dumb question but okay, this is the whole question just so you understand what im looking for.

write a program that stores a database of weigh-less members. the program must have a person class. Person must contain the following data members:

name string
surname string
phone string
age int
weight double

person must have all the relevant accessor functions, as well as amember function to read in data from a file, and antother to display its data members and their values.

use a two-dimensional map to stroe objects of the person class indexed by name and surname

your driver class must perform the following operations:

load in records form a file callde data.
display a menu with the following option:
1.display all records
2.display one record
3.add a record
4.update a record
5.delete a record
6.save records
7.calculate average age
8.calculate average weight
0.exit program
implement all the itmes in the menu.
save any changes int the database to the data file

option 4(the update option) should ask wich record to update, and then which field in that record to update (phone number, age, weight)

okay thats the question word for word the parts im struggling with is exactly how the header files will look like, I made them the way i think but im not sure, and the part im really struggling with is the data file it a normaly text file but i don't know how to read from it or write to it the book gives examples but it just confuses me

Eris
03-17-2009, 02:32 PM
Well, to read from a text file, you can use an ifstream (input file stream). I'll whip up an example. Say you have a text file with four columns of data, three with text (say a name) and one with a number, and they are separated by a newline.

So, the file may look like


foo bar baz 25
foo baz bar 10
foo far fib 100


Obviously, the most logical way of reading this file is to do it line by line. So to read a file line by line, you write a function like this:



void read() {
std::ifstream file_reader; // Create an ifstream and let it read the file
file_reader.open("test.data"); // Open the data file
if(!file_reader.is_open()) { // Print error messages if it wasn't possible to open
std::cerr << "Unable to open file" << std::endl;
return;
}

// The getline function reads lines from an ifstream and puts them
// in the second argument, and the .good() makes sure we only read
// while there is data to be read
std::string line;
while(getline(file_reader, line).good()) {
parse_line(line); // Pass the line to some function that figures out the columns of data
}

// Tidy up
file_reader.close();

}


And the column parser looks like this. It's using a stringstream, which allows you to read from a string as though it was a file.



void parse_line(std::string& line) {
// This is just debug output, you may omit it later
std::cout << "The line '" <<line << "' has data:" << std::endl;

// Define a string stream, tell it to allow both reading from the stream
// and writing to the stream
std::stringstream lstrstr( std::stringstream::out | std::stringstream::in );
// Input the line into the string stream
lstrstr << line;

// Loop while there is data in the string stream
while(lstrstr.good()) {
// Define a few dummy variables we can use to store
// the contents of the fields
std::string str;
int value;

// Read the fields one by one out of the stringstream
lstrstr >> str;
std::cout << "Field #1: " << str << std::endl;
lstrstr >> str;
std::cout << "Field #2: " << str << std::endl;
lstrstr >> str;
std::cout << "Field #3: " << str << std::endl;
lstrstr >> value;
std::cout << "Field #4: " << value << std::endl;
}

}


The code above requires the following headers to work


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


Does this help? Which part of this is it that you have trouble with?

IFS
03-18-2009, 09:15 AM
thanx for all the help with this, the examples are really going to help

Ωmega
03-19-2009, 03:01 PM
This belongs in your blog