Password C++

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

46 Replies - 8367 Views - Last Post: 06 October 2012 - 09:37 AM Rate Topic: -----

#31 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Password C++

Posted 01 October 2012 - 07:55 AM

Yeah, fairly close. Most of the C-style string functions are present, so a simple strcmp will do the job.

Source : I have to endure TC++ every single day at school!
Was This Post Helpful? 0
  • +
  • -

#32 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Password C++

Posted 01 October 2012 - 07:00 PM

dont forget


#include <iostream>
#include <string>

using namespace std;

//put that at the top, you shouldnt need anything else



Was This Post Helpful? 0
  • +
  • -

#33 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Password C++

Posted 01 October 2012 - 07:00 PM

dont forget


#include <iostream>
#include <string>

using namespace std;

//put that at the top, you shouldnt need anything else



Was This Post Helpful? 0
  • +
  • -

#34 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Password C++

Posted 01 October 2012 - 07:42 PM

Midi, we are talking TC++ here! There is no iostream or string, there is only iostream.h and string.h! Also, mo namespaces, no using namespace std is useless.
Was This Post Helpful? 0
  • +
  • -

#35 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,506
  • Joined: 19-February 09

Re: Password C++

Posted 01 October 2012 - 08:38 PM

Don't think string.h includes C++ strings. A string class could be created.

String Class
Was This Post Helpful? 0
  • +
  • -

#36 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Password C++

Posted 01 October 2012 - 09:35 PM

My bad must have misunderstood, just trying to help out
Was This Post Helpful? 1
  • +
  • -

#37 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Password C++

Posted 02 October 2012 - 05:51 AM

Thanks everyone for the suggestions. Today is October 2 i believe we are on different time zone. I will continue this probably on Saturday Oct 6. Im finishing 2 research paper :/ but thanks again for the help.

View Postsnoopy11, on 30 September 2012 - 11:30 PM, said:

Hi,

This is a bit convuluted but it should work in Turbo for your password routine

int main()
{

    char text[5],password[5];
    sprintf(text, "user");
    cout<<"Enter Password: ";
    cin>>password;
    int length1 =strlen(text);
    int length2 =strlen(password);
    if(strstr(password,text)&& (length1==length2))
        cout<<"Correct!\n";
    else
        cout << "Incorrect\n";
    return 0;
}




Also this is October so no help there.....:)

Also asking where you can stick your code is far too tempting :)

Only Kidding

Snoopy


Thanks i will study this.


View Postaresh, on 01 October 2012 - 12:49 AM, said:

Snoopy, first of all, let him do his own code. Another thing, why are you using strlen and strstr? It is better to use strcmp.

Untalented, it would be better if you read up something about functions first, since you are trying to define your functions inside main(), which is wrong. So, read up the links somebody here posted, and then tackle this problem again.


Thanks i will backread.

While inside the class im thinking about the while loop,

#include<iostream.h>
#include<string.h>

int function prototype;
int function prototype;
int function prototype;

int main()
{
//int or char or what should i use for my program to accept both number and letter. maybe string
cout<<"Enter Password: ";
cin>>password;
while(password!=password123){
cout<<"Incorrect";
}
else{
code code code // will my codes execute on this part?
}
return 0;
}


Is this the right sequence?


Thanks for giving me ideas. will be back on Sat. Thanks
Was This Post Helpful? 0
  • +
  • -

#38 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3477
  • View blog
  • Posts: 10,723
  • Joined: 05-May 12

Re: Password C++

Posted 02 October 2012 - 09:24 AM

You should try it yourself with a small test program. It's the best way to learn the language if you don't have a good book.

I think that you will discover that compiler won't let you have an else clause for while statement.
Was This Post Helpful? 0
  • +
  • -

#39 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Password C++

Posted 06 October 2012 - 04:41 AM

Im trying to make a smaller code as suggested but whenever i tried running and entering test as a password it always displays "Error" on the else part. It seems like it is skipping my first condition.


[
#include<iostream.h>
#include<string.h>
#include<conio.h>
int testprog();
int main()
{
	clrscr();
	char password[10];
	cout<<"Enter Password: ";
	cin>>password;
	if(password=="test"){
	testprog();
	}
	else{
	cout<<"Error";
	}
	cin.ignore();
	cin.get();
	return 0;
}
int testprog(){
	int n1,n2,sum;
	cout<<"Enter First number: ";
	cin>>n1;
	cout<<"Enter second number: ";
	cin>>n2;
	sum=n1+n2;
	cout<<"The sum is "<<sum;
	cin.ignore();
	cin.get();
	return 0;
}


Dev C++ [Working]
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main(){
	
   string pass ="";
   char ch;
  
   cout << "Enter pass\n";
   ch = _getch();
   while(ch != 13){
      pass.push_back(ch);
      cout << '*';
      ch = _getch();
   }
   if(pass == "test"){
      cout << "\n Correct! \n";
   }else{
      cout << "\n Wrong\n";
     
   }
   cin.get();
   return 0;
}



I tried converting to Old Turbo C++
Error
Line 6: Undefined symbol string
Line 6: statement missing ;
Line 10: function _getch()should have a prototype
Line 12: undefined symbol pass

#include <iostream.h>
#include <string.h>
#include <conio.h>
int main(){
	
   string pass ="";
   char ch;
  
   cout << "Enter pass\n";
   ch = _getch();
   while(ch != 13){
      pass.push_back(ch);
      cout << '*';
      ch = _getch();
   }
   if(pass == "test"){
      cout << "\n Correct! \n";
   }else{
      cout << "\n Wrong\n";
     
   }
   cin.get();
   return 0;
}


This post has been edited by Untalented: 06 October 2012 - 05:02 AM

Was This Post Helpful? 0
  • +
  • -

#40 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Password C++

Posted 06 October 2012 - 07:25 AM

You can't use the comparison operator== with C-strings(Turbo-C), you need to use strcmp().

And in Turbo-C it is getch() not _getch().

Jim

This post has been edited by jimblumberg: 06 October 2012 - 07:26 AM

Was This Post Helpful? 1
  • +
  • -

#41 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Password C++

Posted 06 October 2012 - 07:55 AM

[Dev C++]

This is my new code and finally it is working. the problem is i can only make it work using numbers as my password. As for characters i will try to use strcmp()

// is strcmp the most ideal to make the password program in turbo c++ (old one)?

#include<iostream.>
#include<string>
#include<conio.h>
#include <stdlib.h>
using namespace std;
int testprog();
int main()
{
	system("cls");
	int password;
	cout<<"Enter Password: ";
	cin>>password;
	if(password==321){
	testprog();
	}
	else{
	cout<<"Error";
	}
	cin.ignore();
	cin.get();
	return 0;
}
int testprog(){
	int n1,n2,sum;
	cout<<"Enter First number: ";
	cin>>n1;
	cout<<"Enter second number: ";
	cin>>n2;
	sum=n1+n2;
	cout<<"The sum is "<<sum;
	cin.ignore();
	cin.get();
	return 0;
}


[Turbo C++]
3 Errors
Line 6: Undefined symbol string
Line 6: statement missing ;
Line 12: undefined symbol pass

#include <iostream.h>
#include <string.h>
#include <conio.h>
int main(){
	
   string pass ="";
   char ch;
  
   cout << "Enter pass\n";
   ch = getch();
   while(ch != 13){
      pass.push_back(ch);
      cout << '*';
      ch = getch();
   }
   if(pass == "test"){
      cout << "\n Correct! \n";
   }else{
      cout << "\n Wrong\n";
     
   }
   cin.get();
   return 0;
}





Strcmp

Im not sure if i am right, the strcmp will compare password and pw right? and if it is equal it should be zero..

It is still skipping my first if condition. im not sure if i can put strcmp and add == 0 there. but obviously im wrong :/

#include<iostream.>
#include<string>
#include<conio.h>
#include <stdlib.h>
#include<cstring>
using namespace std;
int testprog();
int main()
{
	system("cls");
	char password[20];
	char pw[20]="untalented";
	cout<<"Enter Password: ";
	//gets(password);
	cin>>password;
	strcmp(password,pw);
	if(strcmp==0){
	testprog();
	}
	else{
	cout<<"Error";
	}
	cin.ignore();
	cin.get();
	return 0;
}
int testprog(){
	int n1,n2,sum;
	cout<<"Enter First number: ";
	cin>>n1;
	cout<<"Enter second number: ";
	cin>>n2;
	sum=n1+n2;
	cout<<"The sum is "<<sum;
	cin.ignore();
	cin.get();
	return 0;
}


This post has been edited by Untalented: 06 October 2012 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#42 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3477
  • View blog
  • Posts: 10,723
  • Joined: 05-May 12

Re: Password C++

Posted 06 October 2012 - 08:25 AM

You need to check the return value of strcmp:
if (strcmp(password, pw) == 0) {
    testprog();
}


Was This Post Helpful? 1
  • +
  • -

#43 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Password C++

Posted 06 October 2012 - 08:29 AM

Also, STOP TRYING TO USE STD::STRING WITH TC++!

I think someone has already mentioned it, it just doesn't work with TC++. So, use char[] to hold strings.
Was This Post Helpful? 1
  • +
  • -

#44 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Password C++

Posted 06 October 2012 - 08:29 AM

Turbo C has no header with the name of <string>. Quit confusing us, just post the code you are having trouble with, not several versions. Also the strcmp() function returns one of three different values 1, 0, -1.

Jim
Was This Post Helpful? 1
  • +
  • -

#45 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Password C++

Posted 06 October 2012 - 09:12 AM

Thank you everyone. My code is now working! :)

I will just need to mask the password by asterisk.

Im refering to this one.

Im getting error on line 12 structure required on left side of . or .*

What is the right syntax for that?

#include <iostream.h>
#include <string.h>
#include <conio.h>
int main(){

   char pass[20];
   char ch;

   cout << "Enter pass\n";
   ch = getch();
   while(ch != 13){
      pass.push_back(ch);
      cout << '*';
      ch = getch();
   }
   if(pass == "test"){
      cout << "\n Correct! \n";
   }else{
      cout << "\n Wrong\n";

   }
   cin.get();
   return 0;
}



Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4