| bananas95 (2) | |||
|
Hello, yesterday i started to learn c++ by myself. I wrote a simple script, it should download a setup from link i added into my pc. The main problem is that, i want to type a word and get a setup downloaded. But my "char input" can't be that long, i want a word like from 6 chars, i can only add 4. How to do that? Can you explain my error and how to solve it?
| |||
|
|
|||
| theranga (100) | |
Your error is on line 16: it says if (input=='download').There's 2 problems here: input represents a single character: it's declared as a char, which is a single character.The second problem is that when you enclose something in single quotes (' '), it means that whatever's in the quotes is a single character, again. So it's a syntax error to write 'download' because that's not a single character. To fix the problems, you need to do a few things: Change input to an array of char's: declare it like this: char input [100];.Insert this line at the top of your code: #include <cstring> Change the if statement to be: if (strcmp(input,"download")==0)
| |
|
Last edited on
|
|
| bananas95 (2) | |
|
Thanks, i don't understand one place. how does if (strcmp(input,"download")==0) work. Can you explain what is strcmp and what ==0 does? | |
|
|
|
| TheIdeasMan (1753) | |||
|
And, for now forget that that C++ has goto's. There are a couple of situations where they are valid for C code, and shouldn't be needed at all for C++ (return a value or use exceptions) Use loops or functions to achieve your goals. You are doing OK, (It's your second day). If I could recommend anything, I would say stick to C++ data structures such as string (and the associated algorithms & functions), rather than the C style data structures such as char arrays, and functions such as strcmp. Take a look at all the reference & article info at the top left of this page. Some more picky things - just to break bad habits early: Don't use using namespace std; - put std:: before each thing in the std namespace (for things not used so much), or you can do this for things used frequently:
Don't use system - there is an article why not in the articles section. It is also a good thing if you don't mix C++ and C styles - I guess you don't really know the difference yet. Hope all goes well & have fun!! | |||
|
|
|||
| Catfish3 (276) | ||||
http://cplusplus.com/reference/cstring/strcmp/ std::strcmp() returns the value 0 if the two C strings are "equal". Use C++ strings (std::string) instead. http://cplusplus.com/reference/string/
| ||||
|
Last edited on
|
||||