Learning C++ - Is this good to begin with?

  • Thread starter Thread starter Boy12
  • Start date Start date
  • Views Views 7,693
  • Replies Replies 63
There are your errors... two equal signs is what you want :)

EDIT: Some explanations:
if(Choice = 1) is code for the following text:
"If the following statement returns a true value: the variable Choice is successfully assigned the value 1"
Compare this to:
if(Choice == 1):
"If the following statement returns a true value: the variable Choice has the value 1"

In the first (the ones you posted) you assign the value 1 inside the if-statement instead of checking if the value is 1. Therefore it will ALWAYS be true for that first if-statement, and will skip the "else".

This can be useful for more advanced stuff sometimes, if there's a problem assigning a value to something it will return false. But when just using normal variables and values, it will always return true.

Thanks for helping man :yay: , i was allmost about to say "f*ck it"!
 
no, after a option is chosen (say: option 1), it instantly closes.
It depends on the option. If you're talking about a single decimal number (integer), it just has to be in the brackets, if it's a string, you should close it in " " quotation marks (althought string::compare would be the way to go here for a comparison of two strings), if it's a single character (char) then you'll want to use ' ' apostrophes. You'll have to post the whole snippet for us to examine this in detail.

Judging by your previous problems, you should also refresh your knowledge about operators in C/C++. ;)
 
  • Like
Reactions: Boy12
It depends on the option. If you're talking about a single decimal number (integer), they it just has to be in the brackets, if it's a string, you should close it in " " quotation marks (althought string::compare would be the way to go here for a comparison of two strings), if it's a single character (char) then you'll want to use ' ' apostrophes. You'll have to post the whole snippet for us to examine this in detail.

Judging by your previous problems, you should also refresh your knowledge about operators in C/C++. ;)

That's right, haha.
But i just started out (last week friday),so yeah.
But so far it's pretty fun just to see the simplest things working.
Also, 1 question that is completely unrelated to my first one: is there actually a difference between using cout and printf for printing text on the screen?
Or is just printf for C.
and the std:: that some use in their code?
 
  • Like
Reactions: Boy12
*hint* *hint*

https://www.edx.org/course/harvardx/harvardx-cs50x-introduction-computer-1022

I'm still going to suggest this. If you want to actually understand C and not make simple mistakes such as mistaking '=' used for assigning things to variables for '==', you really should work on learning the fundamentals.

And, while I can't speak for everyone, since I don't think looking stuff up as you go (the different operators, printing text, etc.) is the best method, you really should try out CS50. They go from the very beginning teaching you the basics of programming in Scratch and the logic side of things, and then quickly move on to more complex programs that you will actually be able to understand.
 
Keep in mind that if you have a substantial amount of "choices", creating a massive if/else if tree isn't necessary or even a good idea, use a case switch instead - it's more readable and easier to manage.

http://en.cppreference.com/w/cpp/language/switch

For instance:

Code:
switch(Choice) {
 case 1:
  std::cout << "Choice 1";
  break;
 case 2:
  std::cout << "Choice 2";
  break;
 case 3:
  std::cout << "Choice 3";
  break;
 case 4:
  std::cout << "Choice 4";
  break;
 default:
  std::cout << "Default Message, this part is not necessary";
  break;
}
Sorry if the indentations got screwed up - the post parser is quirky like that.
 
Keep in mind that if you have a substantial amount of "choices", creating a massive if/else if tree isn't necessary or even a good idea, use a case switch instead - it's more readable and easier to manage.

http://en.cppreference.com/w/cpp/language/switch

For instance:

Code:
switch(Choice) {
case 1:
  std::cout << "Choice 1";
  break;
case 2:
  std::cout << "Choice 2";
  break;
case 3:
  std::cout << "Choice 3";
  break;
case 4:
  std::cout << "Choice 4";
  break;
default:
  std::cout << "Default Message, this part is not necessary";
  break;
}
Sorry if the indentations got screwed up - the post parser is quirky like that.

hmm... i'm gonna take a look at that.
i'll probally post a Alpha of this little project on GBATemp in 1-2 weeks (maybe earlier) when i got something a little stable, so you guys can also look at the code and maybe see on what i can improve.
 
Am I wrong or also the "break;" statement in every case is not necessary? I might be wrong but I remembered that you could use it only to end a loop with the cases. Keep in mind that I didn't program something in months
 
Am I wrong or also the "break;" statement in every case is not necessary? I might be wrong but I remembered that you could use it only to end a loop with the cases. Keep in mind that I didn't program something in months
The break; statement is a signal for the program to exit the switch, if it's not within the case, the program will continue working - most often you don't want that, but sometimes you do. For instance:
Code:
switch(Variable) {
   case 1: std::cout << "Print first message...\n";
   case 2: std::cout << "...and then print the second message.";
}
In this case, if Variable == 1, the program will print both messages. If Variable == 2, it will only print the second.
 
C and C++ are very powerful languages, but they are also very difficult to start with. I started out with BASIC. When I started college and took CS1, I learned SCHEME (a very difficult and limited language) because it teaches you to stretch beyond the intended purposes of a function and write creative code. After that I took Java, which was a good next step, although often looked down. Now I program in C for homebrew, although it's entirely self-taught based on the previous mentioned languages.


I started with BASIC on the Apple IIe, one thing I missed when I moved to C/C++ and now attempting Java... I miss the line numbers.

For me writing something with line numbers just seemed so structured and orderly. Then I could cause a jump in my programs GOTO 10 just seems more elegant to me than trying to store everything in a block of code and naming it. Then again I also enjoyed ASM on the 6502 so maybe I am just a masochist.

*If I am lucky some one will chime in and tell me "Oh you can use line numbers in Android studio you idiot!!!" that would just make my day... hmmm off to google lol


Oh and to the OP, my personal opinion is to start with something you will use. Yes there are easier things to start with but you wouldn't be using them so they may just end up being detrimental to your learning.... Also I am a huge fan of jumping off the deep end or biting off more than you can chew. That which does not kill you and all that.
 
The break; statement is a signal for the program to exit the switch, if it's not within the case, the program will continue working - most often you don't want that, but sometimes you do. For instance:
Code:
switch(Variable) {
  case 1: std::cout << "Print first message...\n";
  case 2: std::cout << "...and then print the second message.";
}
In this case, if Variable == 1, the program will print both messages. If Variable == 2, it will only print the second.


Yeah, you're right. I opened a piece of game code I wrote for university and I noticed that every case had a break
 
Personally I would go with a switch in such a circumstance.

Code:
switch (Choice) {

     case 1:

          //stuff for option 1

     break;

     case 2:

          //stuff for option 2

     break;

     //add more options here

     default:

          //do nothing because no valid option was selected

     break;

}
 
I started with BASIC on the Apple IIe, one thing I missed when I moved to C/C++ and now attempting Java... I miss the line numbers.

For me writing something with line numbers just seemed so structured and orderly. Then I could cause a jump in my programs GOTO 10 just seems more elegant to me than trying to store everything in a block of code and naming it. Then again I also enjoyed ASM on the 6502 so maybe I am just a masochist.

*If I am lucky some one will chime in and tell me "Oh you can use line numbers in Android studio you idiot!!!" that would just make my day... hmmm off to google lol


Oh and to the OP, my personal opinion is to start with something you will use. Yes there are easier things to start with but you wouldn't be using them so they may just end up being detrimental to your learning.... Also I am a huge fan of jumping off the deep end or biting off more than you can chew. That which does not kill you and all that.
Goto statements are extremely inefficient. Just break your program up into lots of sub-functions.
 
  • Like
Reactions: Psionic Roshambo
Yes, goto is rarely documented in books and tutorials (other than don't use it) because of all the problems it can cause.

To the OP:
I forgot to add to steer clear of hardcore *nix programming boards at least for asking questions or you will get the RTFM STFW treatment. :lol:
 

Site & Scene News

Popular threads in this forum