First post for me! I need help with this code!

Hi,

I'm a computer engineering student who happens to be taking his first programming course ever (and my first encounter with programming). I can't really see anything wrong with this code although I suspect that my (char day;) line doesn't make sense or is insufficient. I followed the logic that I use with integers, when I want to tell the program that I'll be going to an integer which belongs to time for example, I do this:

int time;
cout<<"Enter time";
cin>>time;

But I have a feeling this is not the case here:

#include <iostream>
using namespace std;
void main()
{
char day;
int temperature;

cout<<"enter temperature";
cin>>temperature;
cout<<"enter day";
cin>>day;

if ((temperature < -10) && (day == sunday))
{
cout << "Stay home.";
}
else if (temperature < -10) //and day != SUNDAY
{
cout << "Stay home, but call work.";
}
else if (temperature <= 0) //and temperature >= -10
{
cout << "Dress warm.";
}
else //temperature > 0
{
cout << "Work hard and play hard.";
}
}


I copied everything from the first if statement to the last else statement from my book, including commentary, when I compile, it tells me that sunday is an undeclared identifier.
You see, day is a single character. If you want to compare it with a string, you'll need to #include <string> at the top of your file, and change day to a string. This will also remedy a little problem with how you used cin. :)

Also, sunday is an undeclared identifier. You probably meant to put "sunday" there instead, which is a string literal. :)

Finally, it's int main(), not void main().

Have fun!

-Albatross

P.S: I like your username.
Last edited on
void main is an abomination and should not be used. You cin into a char, you probably want to cin into a char*. And then you use the comparison operator, which makes no sense for char*.

Oh, and sunday and SUNDAY are different things in C++- SUNDAY is probably a macro.
So I can't use char values in bool expressions ?!
Oh, you can. It's just that if you were to use a char variable you'd be comparing it with a char*, which you can't immediately do with ==, while you can compare an std::string and a char* with ==. Remember: a char stores a single character.

-Albatross
char* are pointers, so if you would compare two char*, you would check for identity, not for equality.
The thing is that in my class, we have yet to reach things such as char*. Also, for some ridiculous reason, my midterm will be on paper, including writing codes, so I'm worried about that.

If I would compare what I have to cover for my midterm in contrast with what you guys know, that would be the following at their utter-most basic levels:

-if else statements

-switch statments

-while and do-while

The text we are using is Absolute C++, and I don't like how the author doesn't write the entire code in his examples.
Must be some old text book if it tells you to use void main. Though on the other hand, at least it doesn't append a .h to iostream, maybe it's just bad rather than old? Anyways: In C++, you want to work with std::strings rather than with char* whenever possible. Frees you from a major error source you would have otherwise.
ahh the thing is, nothing was ever mentioned regarding std::strings or char*, both of these I'm seeing for the first time in this thread. If it helps in any way, we are coding with visual studio 9.0
1
2
3
4
"This is a string literal"
'c'/*<-- That is a char*/
char* a; /* is a pointer to char */
string b; /* this is a std::string */
Thank you!
The same program using strings, the important changes are preceded by
//LOOK AT ME
comment


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
#include <iostream>
//LOOK AT ME 
#include <string>

using namespace std;

int main(){
  //LOOK AT ME 
  string day                         ;
  int    temperature                 ;

  cout << "Enter the temperature \n" ;
  cin  >> temperature                ;
  cout << "Enter the day \n"         ;
  cin  >> day                        ;

  //LOOK AT ME ("sunday" not sunday)
  if ( (temperature < -10) && (day == "sunday") ){

    cout << "Stay home." ;

  //and day != SUNDAY
  } else if (temperature < -10){
    cout << "Stay home, but call work.";

  //and temperature >= -10
  } else if (temperature <= 0){
    cout << "Dress warm.";

  //temperature > 0
  } else {
    cout << "Work hard and play hard.";
  }

  return (0);
}


Works only for "sunday" entered in lowcases.
To put it simply:
char can keep a single character
string can keep a word, a sentence, a paragraph. Bunch of letters followed by Enter.

strings are basically identifiers for text.

Example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main(){
  string text;
  text = "I tried to write something wise, but it turned out to be as always.";
  cout << text;
  
  //char* is working in simillar fashion, but, if you can, stay with strings.
  char* text;
  text = (char*)"For the second time I tried to write something wise and I failed again";
  cout << text;
  
  return (0);
}






Last edited on
madred, you char* part contains an implicit conversion of const char* to char*.
Edited, one more reason to not use char*
Probably you don't like my casting now...
Last edited on
Making the conversion explicit is not a solution. A string literal is ALWAYS constant, using it as a non constant char* can lead to anything from subtle bugs like weird results of string operations to serious things like crashes. The real fix would have been to make text constant.
I'm really sorry if this is against the rules but I want to post another code over here and I'm confused regarding what it generates. I pulled this code off my book as well.

#include <iostream>
using namespace std;
int main( )
{
int x = 10;
while (x > -1)
{
cout << x << endl;
x=x-3;
}
return 0;
}



Okay so the output for this would be

10
7
4
1

I am confused as to why it wouldn't also generate -2 at the end ??? isn't 1 still larger than -1 ? isn't it supposed to put the new x value of 1 through the expression x=x-3 ? Please explain.



EDIT!!!!:

So after thinking about it, it does turn the x value to -2 but upon repeating the lopp statement, -2 fails to satisfy the while(x>-1) line thus not printing out -2, am I right ?
Last edited on
Yes
Topic archived. No new replies allowed.