13 Replies - 878 Views - Last Post: 09 October 2014 - 05:05 PM Rate Topic: -----

#1 Iggles8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 14

Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 06:36 AM

I'm getting an error in my second cout line and cant figure our what I'm doing wrong. I thought I declared it right but i'm getting this as my error 23 F:\Leap Year.cpp invalid operands of types `const char[8]' and `<unknown type>' to binary `operator<<' I know its crowded right now I'm working to simplify it this morning though.
using namespace std;
int leapYear;
int Year;
int a;
int num;



int main()
{
// intro to the program.

cout << " Enter a 2 digit month:" << endl;
cin >> a;

switch (a = 01)
{

[b]cout << num = "January" << endl;[/b]

cin >> a;
}
while ( num = 01);

{ 
}
cout << "January" << endl;
    // asking for the year.
    cout << "Please enter a four digit year" << endl;
    // user enters the year.
    cin >> Year;
    leapYear;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;


//int num1 = 01;02;03;04;05;06;07;08;09;10;11;12;

}
while ( num = 02);
else (a = 02);

//while (num = 02)
//
//cout << "February" << endl;
//else if (num = 03)
//
//cout << "March" << endl;
//cout << "April" << endl;
//cout << "May" << endl;
//cout << "June" << endl;
//cout <<"July" << endl;
//cout <<"August" << endl;
//cout <<"September" << endl;
//cout << "October" << endl;
//cout << "November" << endl;
//cout << "December" << endl;


// calculating if the year is a leap-year
int leapYear();
{

    if ((( Year % 4 == 0) && (! ( Year % 100 == 0))) || (( Year % 4 == 0) && (! ( Year % 100 == 0))&&( Year % 400 == 0))) 
{    //return true;
       cout << "It is a Leap-year!" << endl;
  
}
    else
{
        //return false;
       cout << "Sorry... it is not a Leap-year..." << endl;


//cout <<  << "is an invalid selection. \n\n";
     
      }
}
}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Error in my second Cout statement (Beginner)

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,929
  • Joined: 08-August 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 06:39 AM

Assignment: =
Comparison: ==

What are you trying to do here cout << num = "January" << endl;?
Maybe you want this cout << (num == "January") << endl;?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 06:39 AM

05	int num;
[...]
19	 cout << num = "January" << endl; 


What do you think you are doing with this chunk: " num = "January" "?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,929
  • Joined: 08-August 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 06:40 AM

Don't use globals!! They make debugging difficult.
Was This Post Helpful? 0
  • +
  • -

#5 Iggles8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 14

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 06:51 AM

View Postmodi123_1, on 09 October 2014 - 06:39 AM, said:

05	int num;
[...]
19	 cout << num = "January" << endl; 


What do you think you are doing with this chunk: " num = "January" "?


Trying to get the user input to correspond to a print out of January
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,929
  • Joined: 08-August 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 06:56 AM

What does that mean?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 07:03 AM

Okay.. so that is not what is going on there.
cout <<  // tells the system we are about to print something out.
num = "January" // variable 'num' gets the string 'January'.  
<< endl; // print out an 'end of line' character.


There is a problem with that middle bit. 'num' is an integer. It holds *only* integers. The value 'January' is not an integer, right? So trying to assign an integer variable a string of characters does not make sense. Big problem right there.

If you are looking to check a user's input (and output some text) you would look into using IF statements or a SWITCH condition. I see you are trying to use a SWITCH a few lines above it, but that is also incorrect.

http://www.cplusplus...torial/control/
http://msdn.microsof...y/k0t5wee3.aspx

Your switch has a single variable that it uses for input.
16	switch (a = 01)

That is not what you are doing here.

It should look like:
switch (a)


From there the CASES are possible options that the input variable can be.

switch (a)
{
CASE 1: // when a equals 1 then stop here
// do stuff like print out the string 'January'
break; // end of this case
CASE 2:// when a equals 2 then stop here
// do stuff like print out the string 'February'
break; // end of this case
default: // when none of your cases match.
// do stuff
}


Was This Post Helpful? 0
  • +
  • -

#8 Iggles8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 14

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 07:33 AM

View PostCTphpnwb, on 09 October 2014 - 06:56 AM, said:

What does that mean?


User enters 01
Screen prints January
Was This Post Helpful? 0
  • +
  • -

#9 Iggles8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 14

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 08:42 AM

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;
int leapYear;
int Year;
char a;
int num;



int main()
{
// intro to the program.

cout << " Enter a 2 digit month:" << endl;
cin >> a;

while ( num = 01)
{
switch (a)
{
case 01 :
     
cout << a == "January" << endl;

cin >> a;
}



Is this being redundant?
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 08:45 AM

Quote

Is this being redundant?

No, it is being wrong. See my comments.


20	while ( num = 01) // why are you still doing this?  Why are you having a loop check if an integer variable is 1?
21	{
22	switch (a)
23	{
24	case 01 :
25	      
26	cout << a == "January" << endl; // why are you still doing this?  Why are you trying to test if 'a' (an integer variable) is equal to "January" (string of characters)?  
27	 
28	cin >> a;// why are you asking for input here?
29	}

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is online

  • member icon

Reputation: 5737
  • View blog
  • Posts: 17,583
  • Joined: 25-December 09

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 11:41 AM

And you don't seem to understand that there is a difference between the assignment operator= and the comparison operator==.

And be careful about using leading zeros on your numeric constants. You may be surprised with the value of numbers like 077, 023, and by the error messages generated by numbers like 08. A leading zero signifies Octal representation, not decimal.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,929
  • Joined: 08-August 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 12:51 PM

View Postjimblumberg, on 09 October 2014 - 02:41 PM, said:

And you don't seem to understand that there is a difference between the assignment operator= and the comparison operator==.

True. See post #2.
Was This Post Helpful? 0
  • +
  • -

#13 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 03:58 PM

Quote

Maybe you want this cout << (num == "January") << endl;?


Why would they want this? This tries compares an integer value to the memory location of the C string (char*)

This post has been edited by jjl: 09 October 2014 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,929
  • Joined: 08-August 08

Re: Error in my second Cout statement (Beginner)

Posted 09 October 2014 - 05:05 PM

Yeah, I must have been thinking num was an std::string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1