The Mother Board

Motherboards.org forums. Free tech support, motherboard ID, and more.
It is currently Wed Sep 26, 2018 5:29 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Apr 13, 2005 9:45 am 
Offline
Black Belt 1st Degree
Black Belt 1st Degree

Joined: Sun May 11, 2003 8:47 pm
Posts: 1486
Location: Omaha, NE
Howdy all, I've been reading up on using classes in C++ for over a week now so that I might actually know what I'm doing fairly well. My C++ class has finally started working with them. My teacher saved a class file for us to look at (it is in our W: drives and in our textbook) and I thought that I might try my hand at modifying it because it has a main() statement in the class file and I wanted to make the class executable from a client program. Everything has been going ok, I guess, execpt that now I have 1 error, and I can't figure out how to fix it. I will post the original, and then my modified version to see if you can help me:

*Original*

#include <iostream>

using std::cout;
using std::endl;


class Time
{
public:
Time(); // constructor
void setTime(int, int, int);
void printMilitary();
void printStandard();

private:
int hour;
int minute;
int second;

};


//Constructor which initializes each data member to zero
//Ensures all Time objects start in a consistent state
Time::Time()
{
hour = minute = second = 0;
}


// Set a new Time value using military time
// Sets invalid values to zero

void Time::setTime (int h, int m , int s)
{
hour = (( h >= 0 && h < 24) ? h : 0);

minute = (( m >= 0 && m <60) ? m : 0);

second = (( s >= 0 && s <60) ? s : 0);

}

// print time in military format

void Time::printMilitary()
{
cout << (hour < 10 ? "0" : "") << hour << ":"
<< (minute < 10 ? "0" : "") << minute;

}

// print time in standard format

void Time::printStandard()
{
cout << (( hour == 0 || hour == 12) ? 12 : hour %12)
<< ":" << ( minute < 10 ? "0" : "") << minute
<< ":" << ( second < 10 ? "0" : "") << second
<< (hour < 12 ? " AM" : " PM");

}

//Test our class

int main()
{

Time t; //initializing an object of type time

cout << "The intitial military time is ";
t.printMilitary();

cout << "\nThe intitial standard time is ";
t.printStandard();

t.setTime(13, 27, 6);

cout << "\n\nMilitary time after set time ";
t.printMilitary();

cout << "\nStandard time after set time ";
t.printStandard();

cout << endl;

return 0;

}


*Modified*

#include <iostream>

using std::cout;
using std::endl;


class Time
{

private:
int hour;
int minute;
int second;

public: Time()
{
cout << "Time set constructor" << endl;
hour = minute = second = 0;
}


public:
Timecalc();
void printMilitary();
void printStandard();

public: void setTime(int h, int, m, int s)
{
hour = (( h >= 0 && h < 24) ? h : 0);
minute = (( m >= 0 && m <60) ? m : 0);
second = (( s >= 0 && s <60) ? s : 0);
}

};


void Time::printMilitary()
{
cout << (hour < 10 ? "0" : "") << hour << ":"
<< (minute < 10 ? "0" : "") << minute;

}


void Time::printStandard()
{
cout << (( hour == 0 || hour == 12) ? 12 : hour %12)
<< ":" << ( minute < 10 ? "0" : "") << minute
<< ":" << ( second < 10 ? "0" : "") << second
<< (hour < 12 ? " AM" : " PM");

}


*Error for Modified*
W:\C++\time.cpp(27) : error C2061: syntax error : identifier 'm'

I know it's big, but does anyone have any ideas?

_________________
Tono Sien


Love is the greatest conspiracy, by the most loving genius.


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 13, 2005 3:17 pm 
Rarou11 wrote:
public: void setTime(int h, int, m, int s)


You have an extra comma in there. The line should read:

public: void setTime(int h, int m, int s)

:)


Top
  
Reply with quote  
 Post subject:
PostPosted: Wed Apr 13, 2005 9:10 pm 
Offline
Black Belt 2nd Degree
Black Belt 2nd Degree

Joined: Mon Aug 09, 2004 7:33 pm
Posts: 2741
Location: Milwaukee - Wisconsin
:mb_yeahthat::

-Roach

_________________
Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
EVGA GeForce GTX 1070 FTW GAMING ACX 3.0
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
DBPOWER RGB LED Keyboard
Logitech G402 Hyperion Fury


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Apr 14, 2005 10:10 am 
Offline
Black Belt 1st Degree
Black Belt 1st Degree

Joined: Sun May 11, 2003 8:47 pm
Posts: 1486
Location: Omaha, NE
Otay, that fixed the problem in the class file, and yet I'm still getting an error in my client program. Spot the problem and you win some joy of helping:

*Main Program*

#include <iostream>

using namespace std;

#include "time.cpp";

int main()
{

Time t;

cout << "The intitial military time is ";
t.printMilitary();

cout << "\nThe intitial standard time is ";
t.printStandard();

t.setTime(13, 27, 6);

cout << "\n\nMilitary time after set time ";
t.printMilitary();

cout << "\nStandard time after set time ";
t.printStandard();

cout << endl;

return 0;

}

*Error when compiling*

w:\c++\time main.cpp(5) : warning C4067: unexpected tokens following preprocessor directive - expected a newline


Well?

_________________
Tono Sien


Love is the greatest conspiracy, by the most loving genius.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed May 04, 2005 3:20 pm 
Rarou11 wrote:
*Main Program*

#include <iostream>

...

#include "time.cpp";


No semicolon after any statement that starts with '#'. Those statements are preprocessor directives and are delimited (finished) with a newline character (hitting enter). Conversely, the compiler expects the semicolon as the delimiter, which is why nearly every line of code ends that way. The preprocessor has no idea what to do with the extraneous semicolon and so it pukes on you.

The preprocessor does its thing before the compiler even sees the code and those lines don't exist when the compiler gets its turn at bat. The preprocessor takes the #include statement, looks for the file named as an include. (i.e. iostream in the default include path(s) on your system - the carets around the name tell it to look in its default path(s) only. Also, time.cpp in the current working directory - the quotes tell it to use the current working directory.) Once it finds the file, it replaces the #include statement with the contents of the file, then passes all that to the compiler. The compiler has no idea that they are seperate files.


Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group