I’m very new to coding and i’ve been trying to figure this out for about an hour now, this is a program I made for practice, it asks you to pick a number between 1-25, I tried adding a statement if the user picks a number over 25 and I have no clue why it doesnt work. help would be much appreciated
Worth posting in hastebin.com or gist.github.com (edit, so we can see the code better, and make changed/comments on the code directly)
Looks like you mixed up =>, should be >= (line 28)
line 26. cant go there if i remember. move it after the loop or remove it.
line 32&34, while its not 17 print "you win" is that what you want it to do?
The second while loops looks like its causing an infinite loop if things don't go right.
But yeah do a pastepin so we can see/edit it easier
Good point, both second and third while loops are infinite unless the conditions are already true when it enters either of the loops (once the code works)
To add. I assume you are planning on using all those includes? If not and your program is only going to do what you described, you dont need any of them except that iostream one.
The first error the compiler describes on line 26 tells you that you did not follow the convention of a do-while loop. do-while loops must always have the following format:
do {
/* loop code here… */
} while(/* loop conditions here… */);
In other words, you must place the ‘while’ part right after finishing the ‘do’ portion. Remove line 26 and the first four errors should vanish.
@Eden already pointed out that that when describing ‘greater than or equal to’ or ‘less than or equal to’ comparisons like in line 28, you must have the ‘greater than’ or ‘less than’ symbol before the equal symbol.
Also you should check both the upper and lower bound of the guess range when displaying your message.
I have thrown together a corrected and annotated version of your file here (http://hastebin.com/nefuhatalo.cpp). Even though it will now compile and check both upper and lower bounds of input, there are still some things you may wish to still fix (one such thing would be when a user enters input that is not an integer, the program crashes...).
To answer why all the includes were there, I was watching a big tutorial on how to do a lot of things in c++ and the guy doing the tut had all those includes so I figured that I should too, And thank you all so much for your help, I'll analyze underscore's corrections and learn what I can. and post from hastebin next time. :p
Also, how would one make it so that if anything but a integer was placed the program crashed? As underscore suggested, or at least a good website to learn how to do such things
You could try to convert your input to std::stoi and catch invalid_argument exception and terminate if thrown.
std::stoi is part of C++11 so you gotta check your compiler
Velho
if you're interested in learning variations of the C language, I'd definitely recommend checking out the Microsoft Virtual Academy. Free classes and they're usually like 6-20 sessions long. Sometimes they have tests at the end that help keep that information in your short term memory. :)
I've been using codeblocks recently and it seems to be the least complicated thing to use that actually does what I want it to. and did you just mix standard C and C++ by putting down '\n'?? can you just do that? (n00b)
It's personal preference I guess, they could use
using namespace std;
then only use endl; if going through a lot of examples that ask to print all the time