[help]Arrays, Class, Really big variable? I have no idea

Well, As in my last cry for help I was telling you all how I am writing programs for a Advanced Programming class. The classes are merged witht the kiddies learning True-BASIC, the C++ kids learn basicly on our own and code the BASIC programs to C++. Being a **** at C++, I must inquire;

This week the BASIC kids were working on READ-DATA segments in thier class... But me and the other C++ kids thin these would be equivalent to arrays or strings or maybe even a class, we have no idea where to start. Here is a breif summery of the program we are supposed to write:

Write a program that has an internal datadase of 10 Colleges w/ mascots. Let the user input the number of colleges to display at one time... Here is what the output might look like;

Number of colleges to display: 2

College State Mascot:
---------------------------------------
UNL NE Huskers
Greenbay MN Packers

Here is my code so far if it helps;

#include <iostream.h>
#include <string.h>
using namespace std;
int main () {
int i;
int numofcol;
/* Where other variables should go

Inluding the colleges

*/

// INPUT
cout << "Input the number of colleges to display: ";
cin >> numofcol;
cout << "nn";




//"FOR" loop
for(i = 1; i <= numofcol; i++) {
cout << i << endl;

}

system("PAUSE");
return 0;
}

#263886

forgive me if i'm wrong but would you be better off writing for linux as its virtually the same as osx

bot with different interface plus its open source

#263925

If you want to go the easy way out, you could probably just use three arrays of strings, one for colleges, one for states, and one for mascots, and pre-load them with the right values.

#263967

Either what cerebral said or maybe consider using vectors or structs to hold the information of each college...

#263971

Use a class & the STL ( iostream operator<< and iterators).

Some sample code

#include <string>

#include <iostream>

#include <vector>

#include <iterator>

#include <algorithm>



// your College - class here, I just made this up

class College

{

private:

std::string name;

std::string state;

std::string mascotte;





public:



// c-tor

College(std::string name, std::string, state, std::string mascotte) :

name(name), state(state), mascotte(mascotte)

{



}



// get/setters





// iostream output operator

friend std::ostream& operator<<(std::ostream& os, const College& c)

{

os << c.name << "t" << c.state << "t" << c.mascotte;

return os;

}







// your 'database'

static std::vector<College> colleges;



int main()

{

int numToDisplay(0);



// ...

// ...

// ...



std::iterator firstIterator (colleges.begin());

std::iterator lastIterator (colleges.begin());

std::advance(lastIterator, numToDisplay);

std::copy(firstIterator,lastIterator,std::ostream_iterator<College> (std::cout, "n"));

}

You can output STL-containers by copying them to the an 'output iterator', std::ostream_iterator. Much easier than using a for-loop :).

#263977

Use a class & the STL ( iostream operator<< and iterators).

You can output STL-containers by copying them to the an 'output iterator', std:stream_iterator. Much easier than using a for-loop .

Uh... I don't know about "much easier than using a for-loop." You don't think that's a crazily excessive amount of hard-to-read code for a project like this? Using some kind of arrangement of an array, a loop, and strings/structs would be easier and, well, human-readable. I'm sure your code is efficient, but I'm not sure that it's going to help someone learning C++. That's barely going to help someone very familiar with C++.

#264076

Uhhmm... He said "Advanced Programming class" and it's an excercise. You could stick with arrays+structs+loops for the rest of your life, but then you will never advance. C++ has one of the most powerfull implementations of iterators, so why not try to learn it as early as possible?

#264102

Sorry to keep pleading you all for help, but, I decided to rewrite the program and get rid of all the internal things and put the data in a file called "college.txt". I already have 90% of it write too!!!! :)

I did this because:

1.
I understand file I/O much better than classes and strings, and vectors

2.
Much easier for the user to edit the datadase if he/she ever needed/wanted too.

3.
Can be a basis for other applications using files.

4.
I don't have to quess how many tabs to be put in... formatting is easier.

Problem: I can't get it to stop readin the database file (college.txt) at the lines end. In simpler terms I want the program to read as many lines of the file as the user specifies in the programs input. I have included my "college.txt" file as an attachment in this post.

#include <iostream>

#define BUFFER_SIZE 1000

//Using global variables incase needed in another fuction
FILE* fp;
char fname[256]; //Filename.
int i; // i and
int j; // j are variables for the for-loop coming up.
int many; // Number of colleges to display
char buff[BUFFER_SIZE+1];
char ch;

using namespace std;
int main () {
cout << "Enter the college database file (college.db is the one included): ";
cin.getline(fname,256,'n');
cout << "How many colleges would you like to display?: ";
cin >> many;


// File stuff
fp = fopen(fname, "r");

if(fp == NULL ){ // Check the file status
cout << "nn"
<< "Can't open the input file for reading..." << "n"
<< "Make sure the input file exists,"
<< "or and application is not using it.nn";
system("PAUSE");

return 1;
}

ch = getc(fp);
//Way of writing lines to the console
while(ch!=EOF){
cout << ch;
ch=getc(fp);
}

fclose(fp);

system("pause");

}

Thank you alll very much for your help! :)

college.txt

Attachment: college.txt

#264561

any ideas?

#264896

any ideas?

You might want to look into ifstreams (that are used just like cin except for files).

edit: that's three different links, by the way; one on each word. The forum removes the underlines, so it's not as clear.

#264916