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;
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. :)
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.
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.
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
#include <iostream>
//LOOK AT ME
#include <string>
usingnamespace 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
} elseif (temperature < -10){
cout << "Stay home, but call work.";
//and temperature >= -10
} elseif (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>
usingnamespace 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);
}
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 ?