C++ program help

program that displays days of the week and other things

Page 1 of 1

13 Replies - 1096 Views - Last Post: 13 September 2009 - 10:12 PM Rate Topic: -----

#1 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

C++ program help

Post icon  Posted 13 September 2009 - 01:11 PM

I have to write a program that displays certain days of the week and then displays the next day and the previous day. I have some of the program started, but I keep getting a syntax error saying," error C2061: syntax error : identifier 'weekDay' ". How do i fix this problem?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;

   

private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << day; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout
   int weekDay = 0;
   if weekDay = currentDay
	   dayNum = 0;
   else if weekDay = currentDay 
	   dayNum = 1;
   else if weekDay = currentDay 
	   dayNum = 2;
   else if weekDay = currentDay
	   dayNum = 3;
   else if weekDay = currentDay
	   dayNum = 4;
   else if weekDay = currentDay
	   dayNum = 5;
   else if weekDay = currentDay
	   dayNum = 6;
	   



   // print out the value of the monday object using the printDay() function
   cout << "The value of the Monday object is still";
   monday.printDay();
   cout << "." << endl << endl;


   
   system("pause");

   return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ program help

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: C++ program help

Posted 13 September 2009 - 01:13 PM

Two things:

//first if conditionals have the following syntax:
if(condition)
	 //stuff
else if (condition
	 //stuff
else
	 //stuff



Also, you're using the assignment operator instead of the equality operator:

== //this
= //not this



Also, why are you comparing weekday? You set it to zero immediately before the test, it'll always be zero.
Was This Post Helpful? 1
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: C++ program help

Posted 13 September 2009 - 01:23 PM

Dear OP, you have declared variable "weekDay" as of int type, right? So it's impossible for you to compare it with "currentDay", which is of String type.
Was This Post Helpful? 0
  • +
  • -

#4 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: C++ program help

Posted 13 September 2009 - 01:26 PM

Ok, thanks for the help. But now I get the error message "illegal else without matching if"

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;

   

private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << day; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout
  
  string weekDay;
  string nextDay;
  int dayNum;

   if (weekDay == currentDay)
	   dayNum = 0;
	   nextDay = ("Monday");

   else if (weekDay == currentDay)
	   dayNum = 1;
	   nextDay = ("Tuesday");

   else if (weekDay == currentDay)
	   dayNum = 2;
	   nextDay = ("Wednesday");

   else if (weekDay == currentDay)
	   dayNum = 3;
	   nextDay = ("Thursday");

   else if (weekDay == currentDay)
	   dayNum = 4;
	   nextDay = ("Friday");

   else if (weekDay == currentDay)
	   dayNum = 5;
	   nextDay = ("Saturday");

   else if (weekDay == currentDay)
	   dayNum = 6;
	   nextDay = ("Sunday");

   cout << "The day after " << currentDay << "is ";
	   



   // print out the value of the monday object using the printDay() function
   cout << "The value of the Monday object is still";
   monday.printDay();
   cout << "." << endl << endl;


   
   system("pause");

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: C++ program help

Posted 13 September 2009 - 01:28 PM

Alright, I did some tweaks on your code...not sure if this is how you want it or not though.
// get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout
   int dayNum;
   string weekDay = "Sun";
   if (weekDay == sunday.getDay())
	   dayNum = 0;
   else if(weekDay == monday.getDay())
	   dayNum = 1;
   else if (weekDay == tuesday.getDay())
	   dayNum = 2;
   else if (weekDay == wednesday.getDay())
	   dayNum = 3;
   else if (weekDay == thursday.getDay())
	   dayNum = 4;
   else if (weekDay == friday.getDay())
	   dayNum = 5;
   else if (weekDay == saturday.getDay())
	   dayNum = 6;


Was This Post Helpful? 1
  • +
  • -

#6 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: C++ program help

Posted 13 September 2009 - 01:33 PM

That works for me AW, but now i get this error message:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 8\vc\include\ostream(656): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(703): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(741): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(788): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(912): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(919): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(926): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(933): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(174): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(180): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(187): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(194): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(214): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(247): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(267): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 int)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(292): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(312): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(332): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(353): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(373): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(394): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(414): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(434): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(454): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ostream(474): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, void)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
Was This Post Helpful? 0
  • +
  • -

#7 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: C++ program help

Posted 13 September 2009 - 01:45 PM

You got all these errors after compiling the code I posted? Or did you modify your code and got all these errors? If so, please post your current code up.

This post has been edited by AntonWebsters: 13 September 2009 - 01:46 PM

Was This Post Helpful? 1
  • +
  • -

#8 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: C++ program help

Posted 13 September 2009 - 01:53 PM

After, it was one error. Here is the code, also here is the expected output of the finished code:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;

   

private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << day; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout
  
  string weekDay;
  string nextDay;
  int dayNum;

   if (weekDay == sunday.getDay())
	   dayNum = 0;
	   nextDay = ("Monday");

   else if(weekDay == monday.getDay())
	   dayNum = 1;
	   nextDay = ("Tuesday");

   else if (weekDay == tuesday.getDay())
	   dayNum = 2;
	   nextDay = ("Wednesday");

   else if (weekDay == wednesday.getDay())
	   dayNum = 3;
	   nextDay = ("Thursday");

   else if (weekDay == thursday.getDay())
	   dayNum = 4;
	   nextDay = ("Friday");

   else if (weekDay == friday.getDay())
	   dayNum = 5;
	   nextDay = ("Saturday");

   else if (weekDay == saturday.getDay())
	   dayNum = 6;
	   nextDay = ("Sunday");

   cout << "The day after " << currentDay << "is " << nextDay << endl;

   cout << "The day before " << currentDay << "is " << prevDay << endl;
	   



   // print out the value of the monday object using the printDay() function
   cout << "The value of the Monday object is still" << monday.printDay() << "." << endl;


   
   system("pause");

   return 0;
}



OUTPUT:

The value of the Monday object is Monday.
The day after monday is Tuesday.
The day before monday is Sunday.
Monday + 3 = Thursday
The value of the monday object is still Monday.
Monday - 3 = Friday

Sorry that last response was the wrong code. Here is the code that I wanted to post:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;

   

private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << day; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout
  
  string weekDay;
  
  int dayNum;

   if (weekDay == sunday.getDay())
	   dayNum = 0;
	   

   else if(weekDay == monday.getDay())
	   dayNum = 1;
	   

   else if (weekDay == tuesday.getDay())
	   dayNum = 2;
	  

   else if (weekDay == wednesday.getDay())
	   dayNum = 3;
	   

   else if (weekDay == thursday.getDay())
	   dayNum = 4;
	   

   else if (weekDay == friday.getDay())
	   dayNum = 5;
	  

   else if (weekDay == saturday.getDay())
	   dayNum = 6;
	   

   //cout << "The day after " << currentDay << "is " << nextDay << endl;

   //cout << "The day before " << currentDay << "is " << prevDay <<  endl;
	   



   // print out the value of the monday object using the printDay() function
   cout << "The value of the Monday object is still" << monday.printDay() << "." << endl;


   
   system("pause");

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: C++ program help

Posted 13 September 2009 - 01:59 PM

Did some edits on your code again..hope this helps.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;



private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << "The value of the Monday object is still " << day << "." << endl; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout
  string prevDay;
  string weekDay;
  string nextDay;
  int dayNum;

   if (weekDay == sunday.getDay()){
	   dayNum = 0;
	   nextDay = "Monday";
	   prevDay = "Saturday";
   }
   else if(weekDay == monday.getDay()){
	   dayNum = 1;
	   nextDay = "Tuesday";
	   prevDay = "Sunday";
   }
   else if (weekDay == tuesday.getDay()){
	   dayNum = 2;
	   nextDay = "Wednesday";
	   prevDay = "Monday";
   }
   else if (weekDay == wednesday.getDay()){
	   dayNum = 3;
	   nextDay = "Thursday";
	   prevDay = "Tuesday";
   }
   else if (weekDay == thursday.getDay()){
	   dayNum = 4;
	   nextDay = "Friday";
	   prevDay = "Wednesday";
   }
   else if (weekDay == friday.getDay()){
	   dayNum = 5;
	   nextDay = "Saturday";
	   prevDay = "Thursday";
   }
   else if (weekDay == saturday.getDay()){
	   dayNum = 6;
	   nextDay = "Sunday";
	   prevDay = "Friday";
   }
   cout << "The day after " << currentDay << "is " << nextDay << endl;

   cout << "The day before " << currentDay << "is " << prevDay << endl;




   // print out the value of the monday object using the printDay() function
   monday.printDay();



   system("pause");

   return 0;
}


This post has been edited by AntonWebsters: 13 September 2009 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#10 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: C++ program help

Posted 13 September 2009 - 02:20 PM

That works and I do not get that weird error anymore, but the nextDay and prevDay do not print to the output screen. Thanks for all of your help.
Was This Post Helpful? 0
  • +
  • -

#11 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: C++ program help

Posted 13 September 2009 - 02:24 PM

Well, now it shows. ;)

#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;



private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << "The value of the Monday object is still " << day << "." << endl; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout
  string prevDay;
  string weekDay = currentDay;
  string nextDay;
  int dayNum;

   if (weekDay == sunday.getDay()){
	   dayNum = 0;
	   nextDay = "Monday";
	   prevDay = "Saturday";
   }
   else if(weekDay == monday.getDay()){
	   dayNum = 1;
	   nextDay = "Tuesday";
	   prevDay = "Sunday";
   }
   else if (weekDay == tuesday.getDay()){
	   dayNum = 2;
	   nextDay = "Wednesday";
	   prevDay = "Monday";
   }
   else if (weekDay == wednesday.getDay()){
	   dayNum = 3;
	   nextDay = "Thursday";
	   prevDay = "Tuesday";
   }
   else if (weekDay == thursday.getDay()){
	   dayNum = 4;
	   nextDay = "Friday";
	   prevDay = "Wednesday";
   }
   else if (weekDay == friday.getDay()){
	   dayNum = 5;
	   nextDay = "Saturday";
	   prevDay = "Thursday";
   }
   else if (weekDay == saturday.getDay()){
	   dayNum = 6;
	   nextDay = "Sunday";
	   prevDay = "Friday";
   }
   cout << "The day after " << currentDay << " is " << nextDay << endl;

   cout << "The day before " << currentDay << " is " << prevDay << endl;




   // print out the value of the monday object using the printDay() function
   monday.printDay();



   system("pause");

   return 0;
}



It didn't show anything because you did not assign "currentDay" to "weekDay".
Was This Post Helpful? 1
  • +
  • -

#12 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: C++ program help

Posted 13 September 2009 - 03:07 PM

Setting the weekDay equal to the currentDay by itself didn't work, but I separated the nextDay and prevDay and that worked for me.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;



private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << "The value of the Monday object is still " << day << "." << endl; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl;

   // get the value of the day after the current day and print it on the console using cout

  string prevDay;
  string weekDay = currentDay;
  string nextDay;
  int dayNum;

   if (weekDay == sunday.getDay()){
	   dayNum = 0;	   
   }

   else if(weekDay == monday.getDay()){
	   dayNum = 1;	   
   }

   else if (weekDay == tuesday.getDay()){
	   dayNum = 2;	   
   }

   else if (weekDay == wednesday.getDay()){
	   dayNum = 3;	   
   }

   else if (weekDay == thursday.getDay()){
	   dayNum = 4;	   
   }

   else if (weekDay == friday.getDay()){
	   dayNum = 5;	  
   }

   else if (weekDay == saturday.getDay()){
	   dayNum = 6;	   
   }

   //Set the nextDay value to obtain the value of the day after the currentDay
   

   if (dayNum == 0){
	   nextDay = ("Monday");
   }
   else if (dayNum == 1){
	   nextDay = ("Tuesday");
   }
   else if (dayNum == 2){
	   nextDay = ("Wednesday");
   }
   else if (dayNum == 3){
	   nextDay = ("Thursday");
   }
   else if (dayNum == 4){
	   nextDay = ("Friday");
   }
   else if (dayNum == 5){
	   nextDay = ("Saturday");
   }
   else if (dayNum == 6){
	   nextDay = ("Sunday");
   }

   cout << "The day after " << currentDay << " is " << nextDay << endl;

   //Set the prevDay value to obtain the value of the day before the currentDay
   

   if (dayNum == 0){
	   prevDay = ("Saturday");
   }
   else if (dayNum == 1){
	   prevDay = ("Sunday");
   }
   else if (dayNum == 2){
	   prevDay = ("Monday");
   }
   else if (dayNum == 3){
	   prevDay = ("Tuesday");
   }
   else if (dayNum == 4){
	   prevDay = ("Wednesday");
   }
   else if (dayNum == 5){
	   prevDay = ("Thursday");
   }
   else if (dayNum == 6){
	   prevDay = ("Friday");
   }

   cout << "The day before " << currentDay << " is " << prevDay << "." << endl;




   // print out the value of the monday object using the printDay() function
   monday.printDay();



   system("pause");

   return 0;
}



Was This Post Helpful? 0
  • +
  • -

#13 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: C++ program help

Posted 13 September 2009 - 04:16 PM

Ok, so I finally figured out how to write the code properly and display all of the elements that it is supposed to display. Thanks to AntonWebsters for all of your help, it was greatly appreciated and I don't think I would have been able to do this project without it.
Here is my finished code:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class DayOfTheWeek
{
public:
   // setDay(string) takes a string parameter
   // and stores the value in the day attribute.
   void setDay(string);

   // printDay() prints the value of the day attribute on console output (cout).
   void printDay() const;

   // getDay() returns the value of the day attribute
   string getDay() const;



private:
   // this is where the value of the day attribute is stored.
   string day;
};

// implementation of the DayOfTheWeek class member functions
void DayOfTheWeek::setDay(string newDay) { day = newDay; }

string DayOfTheWeek::getDay() const { return day; }

void DayOfTheWeek::printDay() const { cout << day; }


int main()
{
   // instantiate the DayOfTheWeek objects using the default constructor
   DayOfTheWeek monday;
   DayOfTheWeek tuesday;
   DayOfTheWeek wednesday;
   DayOfTheWeek thursday;
   DayOfTheWeek friday;
   DayOfTheWeek saturday;
   DayOfTheWeek sunday;


   // set the values of the objects
   monday.setDay("Mon");
   tuesday.setDay("Tues");
   wednesday.setDay("Wed");
   thursday.setDay("Thurs");
   friday.setDay("Fri");
   saturday.setDay("Sat");
   sunday.setDay("Sun");


   // get the value of the monday object and print it on the console using cout
   string currentDay = monday.getDay();
   cout << "The value of the monday object is " << currentDay << "." << endl << endl;

   // get the value of the day after the current day and print it on the console using cout

  string prevDay;
  string weekDay = currentDay;
  string nextDay;
  int dayNum;

   if (weekDay == sunday.getDay()){
	   dayNum = 0;	   
   }

   else if(weekDay == monday.getDay()){
	   dayNum = 1;	   
   }

   else if (weekDay == tuesday.getDay()){
	   dayNum = 2;	   
   }

   else if (weekDay == wednesday.getDay()){
	   dayNum = 3;	   
   }

   else if (weekDay == thursday.getDay()){
	   dayNum = 4;	   
   }

   else if (weekDay == friday.getDay()){
	   dayNum = 5;	  
   }

   else if (weekDay == saturday.getDay()){
	   dayNum = 6;	   
   }

   //Set the nextDay value to obtain the value of the day after the currentDay
   

   if (dayNum == 0){
	   nextDay = ("Monday");
   }
   else if (dayNum == 1){
	   nextDay = ("Tuesday");
   }
   else if (dayNum == 2){
	   nextDay = ("Wednesday");
   }
   else if (dayNum == 3){
	   nextDay = ("Thursday");
   }
   else if (dayNum == 4){
	   nextDay = ("Friday");
   }
   else if (dayNum == 5){
	   nextDay = ("Saturday");
   }
   else if (dayNum == 6){
	   nextDay = ("Sunday");
   }

   cout << "The day after " << currentDay << " is " << nextDay << endl << endl;

   //Set the prevDay value to obtain the value of the day before the currentDay
   

   if (dayNum == 0){
	   prevDay = ("Saturday");
   }
   else if (dayNum == 1){
	   prevDay = ("Sunday");
   }
   else if (dayNum == 2){
	   prevDay = ("Monday");
   }
   else if (dayNum == 3){
	   prevDay = ("Tuesday");
   }
   else if (dayNum == 4){
	   prevDay = ("Wednesday");
   }
   else if (dayNum == 5){
	   prevDay = ("Thursday");
   }
   else if (dayNum == 6){
	   prevDay = ("Friday");
   }

   cout << "The day before " << currentDay << " is " << prevDay << "." << endl << endl;

//Add days to the currentDay
   int valueToBeAdded;
   int dayValue;
   dayNum = 0;
   cout << "Please input the amount of days you wish to add." << endl;
   cin >> valueToBeAdded;
   cout << endl;
   
   dayNum = (dayNum + valueToBeAdded) % 7;
   dayValue = dayNum;

   if (dayValue== 6){
	   weekDay = ("Sunday");
   }
   else if (dayValue == 0){
	   weekDay = ("Monday");
   }
   else if (dayValue == 1){
	   weekDay = ("Tuesday");
   }
   else if (dayValue == 2){
	   weekDay = ("Wednesday");
   }
   else if (dayValue == 3){
	   weekDay = ("Thursday");
   }
   else if (dayValue == 4){
	   weekDay = ("Friday");
   }
   else if (dayValue == 5){
	   weekDay = ("Saturday");
   }

   cout << currentDay << " + " << valueToBeAdded << " = " << weekDay << endl << endl;
   

   // print out the value of the monday object using the printDay() function
   cout << "The value of the monday object is still ";
   monday.printDay();
   cout << "." << endl << endl;


   //Subtract days from the currentDay
   int valueToBeSubtracted;
   dayValue = 0;
   dayNum = 0;
   
   cout << "Please input the amount of days you wish to subtract." << endl;
   cin >> valueToBeSubtracted;
   cout << endl;
   
   dayNum = (dayNum + valueToBeSubtracted) % 7;
   dayValue = dayNum;

   if (dayValue== 1){
	   weekDay = ("Sunday");
   }
   else if (dayValue == 0){
	   weekDay = ("Monday");
   }
   else if (dayValue == 6){
	   weekDay = ("Tuesday");
   }
   else if (dayValue == 5){
	   weekDay = ("Wednesday");
   }
   else if (dayValue == 4){
	   weekDay = ("Thursday");
   }
   else if (dayValue == 3){
	   weekDay = ("Friday");
   }
   else if (dayValue == 2){
	   weekDay = ("Saturday");
   }

   cout << currentDay << " - " << valueToBeSubtracted << " = " << weekDay << endl << endl;


   system("pause");

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#14 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: C++ program help

Posted 13 September 2009 - 10:12 PM

You're welcome sir. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1