I read in a tutorial that at the end of every line in the int main() {}. But then I had a bunch of problems coming up with this code and one of the main problems was the fact that I put semicolons at the end of every line in the int main function. Here is the code:
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "Enter the number 1 if you want the color blue \n";
cout << "Enter the number 2 if you want the color red \n";
cout << "Enter the number 3 if you want the color green \n";
cout << "Enter the number 4 if you want the color orange \n";
cout << "Enter the number 5 if you want the color yellow \n";
cout << "Enter the number 6 if you want the colow purple \n";
cin >> choice;
if (choice == 1){
cout << "You picked the color blue!";
}
else if (choice == 2){
cout << "You picked the color red!";
}
else if (choice == 3){
cout << "You picked the color green!";
}
else if (choice == 4){
cout << "You picked the color orange!";
}
else if (choice == 5){
cout << "You picked the color yellow!";
}
else if (choice == 6){
cout << "You picked the color purple!";
}
else {
cout << "You didnt pick one of the options... Try again.";
}
}
So the problem I had was I put a semicolon at the end of EVERY line in the int main {}. Why is this wrong

; after every line in int main() {}
Kdean6869
I would not have pegged that for a beginnier book. My first book was 'teach yourself C++ in 24 hours'. YOu could do a lot worse. Just don't buy 'teach yourself Visual C++ in 24 hours', that's an MFC book and you need to learn C++ first ( plus MFC won't work in the Express Edition )
thief_1
A switch statement loooks like this
switch(arg)
{
case firstArg:
break;
case secondArg:
break;
default:
break;
}
arg is the value being checked, and each case specifies the code to execute if arg is that value. The default will run if no match occurs in the case statements. The break is required, without it, execution will flow to the next case statement ( which is sometimes a good thing, which is why it's supported ).
Damian
David X Huang
JCLaguna
cblaze22
You've asked this in the C# forum but posted C++ code, I'll move it for you.
I suspect you've read something and misunderstood it. ALL C++ ( and C# for that matter ) requires a ; at the end of nearly every statement of code. However, a line of code does not have to end on a single line. The code you've posted is correct, I assume at one point you had more semicolons which caused you grief.
I'd actually space this differently, I like braces like this:
if (choice == 1)
{
cout << "You picked the color blue";
}
My point is not that either way is wrong, but rather than in this case, there is only one semicolon, an if statement does not have a semicolon ( although that would be valid ), because in that case, the if statement would end there, and the cout statement would always occur, being in the code AFTER the if statement.
Also, you can do this more nicely using switch:
cin >> choice;
switch(choice)
{
case 1:
cout << "You picked the color blue!";
break;
case 2:
cout << "You picked the color red!";
break
// etc
default:
cout << "You didnt pick one of the options... Try again.";
}
Also, to make this loop again, do something like this :
int choice = 0;
while (choice < 1 || choice > 7)
{
cin >> choice;
switch (choice)
// etc
}
Some other points - first of all, declaring variables all at the top of a function is bad form. C requires it, C++ does not, and it's better to declare just before you use. In addition, any variable you declare should be given a default value on the same line, as I did with choice above. Otherwise, you have junk values that can cause hard to find bugs.
Hope that helps.
StarBrand123
daniblind
Jay23
As has already been stated on another thread you started it's time to hit the books. You can't use these forums to learn the basics of C and C++ - you need to go through a good book and really understand how the language works.
Here's a thread that has links to recommendations of good C++ books
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=107484&SiteID=1
fatcat1111
I was programming computers at age 14, and I had no access to books, let alone the web. So, age is irrelevant. The book I recommended is a good beginner book, if you can't follow it, you should perhaps try VB, or just give up and come back to it in a few years. I'm sure there is also a C++ for dummies book, but I've not read it to know if it's any good.
I don't mean that as an insult, a book from the dummies or 'in 24 hours' series is a good choice simply because it assumes no prior knowledge.