Thursday, July 9, 2009

Can someone please help me with a c++ program?

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%26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;fstream%26gt;





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%26lt;%26lt; name;


cout.width(4); cout %26lt;%26lt; grade;


cout.width(4); cout %26lt;%26lt; hours %26lt;%26lt; endl;


}


else


cout %26lt;%26lt; "....." %26lt;%26lt; endl;


}


};


class Student{


string last,first,street,city,state,z...


int id;


int num_classes;


Course classes[15];

Can someone please help me with a c++ program?
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.
Reply: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.
Reply: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.


No comments:

Post a Comment