English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

this c++ program is supposed to read in information about students from a file, calculate gpa and stuff and print it out, when i run the program i have all i get are weird outputs, help me figure this out plz

anyway heres the program

#include
#include
#include

using namespace std;

class Course{
char grade;
int hours;
public:

Course(){
name = "";
hours = 0;
}
Course(string n, char g, int h);

void setName(string n){name=n;}
void setGrade(char g){grade=g;}
void setHours(int h){hours=h;}

string getName(){return name;}
char getGrade(){return grade;}
int getHours(){return hours;}

void print(){
if (name != ""){
cout.width(10); cout<< name;
cout.width(4); cout << grade;
cout.width(4); cout << hours << endl;
}
else
cout << "....." << endl;
}
};
class Student{
string last,first,street,city,state,z...
int id;
int num_classes;
Course classes[15];

2007-04-23 19:17:53 · 3 answers · asked by Alex P 2 in Computers & Internet Programming & Design

int hours_att, hours_earned, grade_points;
double gpa;

public:
Student(){
last = first = street = city = state = zip = major = minor = "";
id = num_classes = hours_att = hours_earned = grade_points = 0;
}
Student(string f,string l,string s,string c,string st,string z, string ma, string mi,
int i, int ha, int he, int gp){
last=l;first=f;street=s;city=c...
id=i;hours_att=ha;hours_earned...
num_classes = 0; // Don't set number of classes to anything but zero.
}
void setLast(string l){last = l;}
void setFirst(string f){first = f;}
void setStreet(string s){street = s;}
void setCity(string c){city = c;}
void setState(string s){state = s;}
void setZip(string z){zip = z;}
void setMajor(string m){major = m;}
void setMinor(string m){minor = m;}
void setID(int i){id = i;}
void setHoursAtt(int h){
if (h > 0)
hours_att = h;
else
hours_att = 0;
}

2007-04-23 19:18:16 · update #1

void setHoursEarned(int h){
if (h > 0)
hours_earned = h;
else
hours_earned = 0;
}void setGradePoints(int g){
if (g > 0)
grade_points = g;
else
grade_points = 0;
}
void setGPA(double g){
if (g > 0)
if (g > 4.0)
gpa = 4.0; // > 4.0 is not reasonable
else
gpa = g;
else
gpa = 0; // < 0 is not reasonable
}

string getLast(){return last;}
string getFirst(){return first;}
string getStreet(){return street;}
string getCity(){return city;}
string getState(){return state;}
string getZip(){return zip;}
string getMajor(){return major;}
string getMinor(){return minor;}
int getID(){return id;}
int getHoursAtt(){return hours_att;}
int getHoursEarned(){return hours_earned;}
int getGradePoints(){return grade_points;}
double getGPA(){return gpa;}

void calculate_hours(char grade, int hours){
hours_att = hours_att+hours;
if (grade=='A'||grade=='B'||grade...
hours_earned = hours_earned+hours;

2007-04-23 19:18:43 · update #2

hours_earned = hours_earned;
if (grade=='A')
grade_points = (grade_points + hours*4);
if (grade=='B')
grade_points = (grade_points + hours*3);
if (grade=='C')
grade_points = (grade_points + hours*2);
if (grade=='D')
grade_points = (grade_points + hours*1);
gpa = (grade_points/hours_att);


}


void SetAll(string l,string f,string s,string c,string st,string z,string M,string m,int i){
cout<<"from SetAll"< last=l;first=f;street=s;city=c...
}
void label(){
cout< cout< cout< }

void print(){
cout << "Student printout:" < cout << street << ", "<< city << ", "<< state << ", "<< zip << endl;cout << major << ", "<< minor << "; "<< hours_att<< ", "<

2007-04-23 19:19:05 · update #3

int load(Student x[], int count){
ifstream data("student.txt");
string l,f,s,c,st,z,M,m;
int i, ha,he, g;
int ct;
for(ct=0;ct
getline(data,l);
getline(data,f);
getline(data,s);
getline(data,c);
getline(data,st);
getline(data,z);
getline(data,M);
getline(data,m);
data>>i;
data>>ha;
data>>he;
data>>g;
data.ignore();
'


cout<<"Record #"< < x[ct].SetAll(l,f,s,c,st,z,M,m,...
}
return --ct;
}
int main(){
Student list[20];
int count = load(list, 20);
cout<<"'load' read in" << count <<" student." < for (int i=0; i list[i].print();
cout< ifstream data("courses.txt");
string a,b,c,d;
int count1=20,ct;
for(ct=0;ct getline(data, a);
getline(data, b);
getline(data, c);
getline(data, d);

}


}

2007-04-23 19:20:03 · update #4

3 answers

You are not trying to debug this much, are you?

Isolate EACH of the modules. Add COUT statements to check the progress of EACH module, before and after. Is it getting the right input? Is it producing the right output?

If you're sure this module is working, but the module calling this module is not working, then the problem is with the caller.

Isolate and identify the problem one step at a time.

2007-04-23 19:25:33 · answer #1 · answered by Kasey C 7 · 0 0

First find a compiler that is compatible with your operating system, and decide whether you want to run an Integrated Development Environment (IDE) or if you want to edit C files manually through an editor like Notepad and compile from the command line. If you're a Window user, try using Visual C++ Express 2005 which is available for download for free. If you're a Unix user, you might just want to use gcc.
Learn how to compile and run a basic program, this will be your first program, typically it will just print "Hello World" to the screen and exit. Don't worry about all the minor details of the syntax, just become comfortable with compiling and running.
Learn about variable types, such as the difference between char, int, float, double, etc.
Learn about the concept of variables, arrays and functions. Variables are where information is stored, functions are pieces of code that can be executed and arrays are groups of
Learn pointers. Pointers are very important in C since you can directly access memory contents through pointers, unlike Java. The drawback to this is that if your program isn't thoroughly tested, it can crash.
Learn conditional statements, such as the "if" and "switch" statements. The "if" statement will be one of your most frequently used statements, you can execute code based on whether a condition is true or not (e.g. whether the color the user provided was red).
Learn loops. Learn the difference of the "for" loop and the "while" loop - make sure to avoid infinite loops! Learn the continue and break statements.
Learn data structures. Although data structures are not directly related to programming, but for an advanced user, knowledge of basic concepts in Computer Science is essential.
Start with small programs. When you are making your own code, try to identify the most essential part of the problem - is it the data input or the calling of the functions, the structure of the loop (these are some very elementary examples) and start from there. Then build upon that in small increments.



Tips
Remember, C is a programming language. Learning a programming language may not necessarily lead to learning to program, which is more about problem solving than about compiling and running a program in a specific language.
When encountering a syntax error when compiling, if you're stumped, search Google (or another search engine) with the error you received. Chances are someone has already experienced the same problem and posted a solution.
Find a good C programming book. A recommendable C resource book is "The C Programming Language" by Brian W. Kernighan, Dennis Ritchie (ISBN 0131103628). Find a book that has tutorials and projects to facilitate your exposure to C.

2007-04-24 02:29:08 · answer #2 · answered by Anonymous · 0 0

Going to need more info than this, particularly the input and the output that is mangled. Better yet, since you know this inside and out, you should debug this either by carpet-bombing your code with cout statements or using a debugger, either gdb (if you're on UNIX) or an IDE like MSVC++ Express.

2007-04-24 10:13:56 · answer #3 · answered by icnintel 4 · 0 0

fedest.com, questions and answers