Does not compile :(

My this program works fine on visual studio 2013, but does not compile on Code::Blocks 13.12 and Dev C++ latest version
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
38
39
40
41
  #include <iostream>
#include <conio.h>
#include <string>
using namespace std;
void password ();
int main ()
{
	cout<<"Login Menu\n";
	char *uName;
	uName=new char[];
	cout<<"Enter Username: ";
	cin>>uName;
	password();
	cout<<"Logged in successfully.\n";
	getch();
	return 0;
}
void password ()
{
	const string key="49217k";
	string _key;
	int counter=4;
	while (true)
	{
		cout<<"\nEnter Password: ";
		cin>>_key;
		counter--;
		if (counter==0)
		{
			cout<<"Too many wrong entries program is exiting\n";
			exit(true);
		}
		if (_key==key)
		{
			return;
		}
		cout<<"Wrong password\n";
		cout<<counter<<" Attempts remaining\n";
	}
}

I get this error
D:\C++ Project\Code\Code.cpp|10|error: expected primary-expression before ']'
Last edited on
What do you expect line 10 to even do? C++ is not magic.

Also, conio.h is a non-standard header and is deprecated:
https://en.wikipedia.org/wiki/Conio.h
What is happening on line 10? You are allocating an array of ??? chars...?
I'm trying to allocate the array of character, the size of array will be determined at run time, dynamically..
this program compiles fine on Visual C++
The size of the array will indeed be determined at runtime, dynamically, but you have to write the code that determines the size. C++ is not magic.

Just because code compiles in one compiler does not mean it is valid code.
How can i correct this ?
Use a std::string as you do later in the code, lines 21 and 26.
Thanks for that.. and how can I take input in string with spaces ?
Last edited on
Also in my this program, how can i terminate my program if the number of attempts exceeds 3 ?
It looks like your code already does that, is it not working? You should post your current code.
Last edited on
I was not including #include <stdlib.h> in my code, but now it is working fine PS I've sent a PM to you @LB
Topic archived. No new replies allowed.