Hello C++ programmers. Silly question.

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <tchar.h>
#include <urlmon.h>
#include <iostream>
#include <windows.h>
using namespace std;
#pragma comment(lib, "urlmon.lib")
int main()
{
char input;
Start:
system("TITLE Title ");
cout << "Type a download to begin download" << endl;
cout << "Enter value :  ";
cin >> input;

if ( input == 'download')
    {
        system("cls");
        goto Down;
    }
		else
		{
		cout << "You typed wrong wrong word.";
                system("pause");
		goto Start;
		
		}
Down:
cout << "Downloading setup...";
	HRESULT hr = URLDownloadToFile ( NULL, _T("http://download.piriform.com/ccsetup233.exe"), _T("D:/setup.exe"), 0, NULL );
	cout << "   Done!" << endl;
	system("PAUSE");
	system("cls");
	goto Start;

return 0;
}
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
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?
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
//include statements

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

//function declarations

//main program

//function definitions 


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!!
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?


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/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>
#include <string>

int main()
{
    const std::string expected("EUROPE");
    std::string input;

    std::cout << "Type this:\n\t" << expected << "\n >>> ";

    if (!(std::cin >> input)) // just in case
        std::cerr << "Input has failed.\n";
    else
    if (input == expected)
        std::cout << "You entered " << expected << " correctly!\n";
    else
        std::cout << input << " is not the same as " << expected << '\n';

    std::system("PAUSE");
}
Last edited on
Topic archived. No new replies allowed.