Apr 17, 2014 at 2:41am UTC
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 Apr 17, 2014 at 2:42am UTC
Apr 17, 2014 at 2:44am UTC
What is happening on line 10? You are allocating an array of ??? chars...?
Apr 17, 2014 at 2:58am UTC
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++
Apr 17, 2014 at 3:13am UTC
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.
Apr 17, 2014 at 3:18am UTC
Use a std::string as you do later in the code, lines 21 and 26.
Apr 17, 2014 at 3:25am UTC
Thanks for that.. and how can I take input in string
with spaces ?
Last edited on Apr 17, 2014 at 3:25am UTC
Apr 17, 2014 at 3:30am UTC
Also in my this program, how can i terminate my program if the number of attempts exceeds 3 ?
Apr 17, 2014 at 3:31am UTC
It looks like your code already does that, is it not working? You should post your current code.
Last edited on Apr 17, 2014 at 3:31am UTC
Apr 17, 2014 at 3:36am UTC
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