The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-22-2007, 03:54 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon13 Help! What's wrong with my code?

Here's the code:

GradeBook.h

PHP Code:
#include <iostream>;

#include <string>;

class GradeBook {
public:
    
GradeBook(string); // constructor
    
void setCourseName(string);
    
string getCourseName();
    
void displayMessage();
    
void inputGrades();
    
void displayGradeReport();
    
int maximum(intintint);
private:
    
string courseName;
    
int maximumGrade;

Recursion.cpp (don't worry about the title, no connection with recursion)
PHP Code:
#include "stdafx.h"

#include <iostream>;
using std::endl;
using std::cout;
using std::cin;

#include <string>;
using std::string;
using std::getline;

#include "GradeBook.h";

GradeBook::GradeBook(string name) {
    
setCourseName(name);
    
maximumGrade 0;
}

void GradeBook::setCourseName(string name) {
    if (
name.length() <= 25) {
        
courseName name;
    } else {
        
courseName name.substr(025);
        
cout << "Name \"" << name << "\" exceed maximum length of 25.\n"
            
<< "Only gets the first 25 characters";
    }
}

string GradeBook::getCourseName() {
    return 
courseName;
}

void GradeBook::displayMessage() {
    
cout << "Welcome to " << getCourseName() << "!\n" << endl;
}

void GradeBook::inputGrades() {
    
int grade1grade2grade3;
    
cout << "Grade 1: ";
    
cin >> grade1;
    
cout << "Grade 2: ";
    
cin >> grade2;
    
cout << "Grade 3: ";
    
cin >> grade3;

    
maximumGrade maximum(grade1grade2grade3);
}

int GradeBook::maximum(int xint yint z) {
    
int max x;
    if (
max) {
        
max y;
    }
    if (
max) {
        
max z;
    }

    return 
max;
}

void GradeBook::displayGradeReport() {
    
cout << "Max grade: " << maximumGrade << endl;
}

int main() {
    
GradeBook myGradeBook("C++ PROGRAMMING");
    
myGradeBook.displayMessage();
    
myGradeBook.inputGrades();
    
myGradeBook.displayGradeReport();
    return 
0;

They said that there's something wrong with the constructor, I have no clue why...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 07-22-2007 at 03:57 PM.
Reply With Quote
  #2 (permalink)  
Old 07-22-2007, 04:28 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 109
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Firstly, there should be no semicolons in #include directives
Code:
#include <iostream>;

#include <string>;
Secondly, i'm not sure why you've limited your 'name' to 25 characters, since a string is able to hold as many as you need. (Although perhaps you have your reasons)

Lastly, you're missing a semicolon at the end of your class declaration

Code:
class GradeBook {
public:
    GradeBook(string); // constructor
    void setCourseName(string);
    string getCourseName();
    void displayMessage();
    void inputGrades();
    void displayGradeReport();
    int maximum(int, int, int);
private:
    string courseName;
    int maximumGrade;
} ;

The reason it gave you an error message about your constructor, is that you had written
Code:
#include "GradeBook.h"
on the line before your constructor's definition. The result of which, after the Preprocessor had glued your various #includes together, the compiler ended up thinking you intended your class, GradeBook (which was missing a semicolon), to be the return type of the constructor. Of course, constructors aren't allowed a return type, so the compiler threw up an error.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 07-22-2007 at 04:36 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (07-22-2007)
  #3 (permalink)  
Old 07-22-2007, 04:33 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
DARN IT! I always forgot to put semi colon right after the class!!! OMG!!! I'm used with Java, that's why lol.. there's no semi colon after class XD

Thanx a lot Bench, it's fixed because of the semi colon. I need to get used with this.. For the string thing, I just want to try substr

BTW, I didn't remove the semi colon right on my include statement, and it seems that it ignores it...?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-22-2007, 04:38 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Quote:
Originally Posted by HelloWorld View Post
BTW, I didn't remove the semi colon right on my include statement, and it seems that it ignores it...?
Maybe it is something to do with your compiler? It may see that includes closing arrow ">" and not compile anything on that line after that arrow.
Reply With Quote
  #5 (permalink)  
Old 07-22-2007, 04:41 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 109
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Some compilers may ignore the semicolon after #include directives, however, its not in the standard, so some will throw up an error about it. (I've noticed that MSVC++ ignores semicolons here - I can only imagine the reason is for some MS language extension)

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 07-22-2007 at 04:43 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (07-22-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:12 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50