debug it

i am not gettin where am i wrong??

Page 1 of 1

12 Replies - 578 Views - Last Post: 17 February 2010 - 11:12 AM Rate Topic: -----

#1 zartasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-February 10

debug it

Posted 15 February 2010 - 07:28 AM

hello all,here is a code which ask the user to enter password...it displays "*" instead of password..then check the password.if it is a numeric value then prints"valid password otherwise "invalid password"
but this program is not running correctly.can someone debug it and tell me where am i wrong.thnx



# include <iostream>
# include <cstring>
# include <conio.h>
using namespace std;
int main()
{
	char password[20];
	bool check= true;
	int length;
	cout<<"Enter password not more then 20 numbers"<<endl;
	do
	{
		password = char(getch());        //Returns characters input without displaying them.
 
        if(char (password) !=13 )     // if the input is not enter.
		{ 
                cout << '*'; // Prints an asterisk.
               
        }
		cout<<endl;
	
	}
	 length = strlen(password);
	for(int i=0; i < length; i++)
	{
		if(password[i] >= '0' && password[i] <= '9')
		{
		}
		else
		{
		check = false;
		break;
		}
	}
	if(check)
		cout<<"Valid password"<<endl;
	else if(!check)
	     cout<<"Invalid password"<<endl;

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: debug it

#2 Keerigan  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 04-February 10

Re: debug it

Posted 15 February 2010 - 07:40 AM

Well for one, please place your code in the [ code] [/code ] blocks.

And when I ran your code,
password = char(getch());

is what fails. That is because getch() only gets ONE character and password wants 20, a for loop should fix that for you quite nicely
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: debug it

Posted 15 February 2010 - 07:43 AM

I do not recommend getch(). It's non-standard and it reads char-by-char. cin >> will do the trick here. Also, you are using "do", so the compiler expects a "do-while" loop. The problem is that your "while" is missing. Read here about this loop: http://cplus.about.c...essonfive_4.htm
Was This Post Helpful? 0
  • +
  • -

#4 zartasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-February 10

Re: debug it

Posted 15 February 2010 - 07:50 AM

View PostKeerigan, on 15 February 2010 - 06:40 AM, said:

Well for one, please place your code in the [ code] [/code ] blocks.

And when I ran your code,
password = char(getch());

is what fails. That is because getch() only gets ONE character and password wants 20, a for loop should fix that for you quite nicely






how to use a for loop in this code??
Was This Post Helpful? 0
  • +
  • -

#5 Keerigan  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 04-February 10

Re: debug it

Posted 15 February 2010 - 07:51 AM

also, at the end of your do loop, you forgot a "while" clause.

I am guessing you want that to be while the user has not hit enter.

You might want to introduce a single char called ch, then instead of a do{}while loop do a while(} loop that may look like this.
while( ch != 13)
{
    ch = getch();
    ...code...
}


This of course would mean to take out the for loop I just said to do
Was This Post Helpful? 0
  • +
  • -

#6 zartasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-February 10

Re: debug it

Posted 15 February 2010 - 08:13 AM

View PostKeerigan, on 15 February 2010 - 06:51 AM, said:

also, at the end of your do loop, you forgot a "while" clause.

I am guessing you want that to be while the user has not hit enter.

You might want to introduce a single char called ch, then instead of a do{}while loop do a while(} loop that may look like this.
while( ch != 13)
{
    ch = getch();
    ...code...
}


This of course would mean to take out the for loop I just said to do



sorry keerigan em stil not able to get ur point..i will try it again..thnx anyway
Was This Post Helpful? 0
  • +
  • -

#7 Keerigan  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 04-February 10

Re: debug it

Posted 15 February 2010 - 08:20 AM

Sorry, I am at work right now, and am trying to do 2 things at once.

Anyways, what I did was instead of "do", I put "while( ch != 13 )".
I add char ch for this to work.

Then in the loop you have ch = getch() to get each character.

I also changed you if statement to the following:
if(ch != 13 ) // if the input is not enter.
		{
			password[count] = ch; //Returns characters input without displaying them.
                        // int count = 0; is added
			cout << '*'; // Prints an asterisk.
			count++;
		}


this will check that the user has not hit enter, and adds the number they pressed to the password array.
I am using count so that I can keep track of where to add each character aswell.
Was This Post Helpful? 0
  • +
  • -

#8 zartasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-February 10

Re: debug it

Posted 15 February 2010 - 08:43 AM

View PostKeerigan, on 15 February 2010 - 07:20 AM, said:

Sorry, I am at work right now, and am trying to do 2 things at once.

Anyways, what I did was instead of "do", I put "while( ch != 13 )".
I add char ch for this to work.

Then in the loop you have ch = getch() to get each character.

I also changed you if statement to the following:
if(ch != 13 ) // if the input is not enter.
		{
			password[count] = ch; //Returns characters input without displaying them.
                        // int count = 0; is added
			cout << '*'; // Prints an asterisk.
			count++;
		}


this will check that the user has not hit enter, and adds the number they pressed to the password array.
I am using count so that I can keep track of where to add each character aswell.



thnx...but after making the changes compiler is showing following errors
"c:\users\zartasha\documents\visual studio 2010\projects\lab work\lab work\password.cpp(15): error C2440: '=' : cannot convert from 'char' to 'char [20]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
Was This Post Helpful? 0
  • +
  • -

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: debug it

Posted 15 February 2010 - 08:45 AM

char password[20];


Change this to:
char password;


I told you, getch() returns a SINGLE character!
Was This Post Helpful? 0
  • +
  • -

#10 zartasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-February 10

Re: debug it

Posted 15 February 2010 - 08:52 AM

View Postsarmanu, on 15 February 2010 - 07:45 AM, said:

char password[20];


Change this to:
char password;


I told you, getch() returns a SINGLE character!



i have to use cstrings to write this code..this is specified by our professor
Was This Post Helpful? 0
  • +
  • -

#11 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: debug it

Posted 15 February 2010 - 09:45 AM

        do
        {
                password = char(getch());        //Returns characters input without displaying them.
 
        if(char (password) !=13 )     // if the input is not enter.
                { 
                cout << '*'; // Prints an asterisk.
               
        }
                cout<<endl;
        
        }


The correct structure is:
do {
    //statements
} while(/* some condition */);


And instead of:
                if(password[i] >= '0' && password[i] <= '9')
                {
                }
                else
                {
                check = false;
                break;
                }


You can use the logical NOT operator, like:
        if (!password[i] >= '0' && !password[i] <= '9')
        {
            check = false;
            break;
        }



The modified version of your code would be:
# include <iostream>
# include <cstring>
# include <conio.h>
using namespace std;
int main()
{
    char password[20];
    bool check= true;
    int length;
    cout<<"Enter password not more then 20 numbers"<<endl;
    char temp;
    for (int i = 0; i < 20; i++)
    {
        temp = getch(); //reads a single character and stores in temp (the user don't need to hit enter)
        if (temp == 13 || i == 19) //if a the user hited the return key or if it is the last character of the array
        {
            password[i] = '\0'; //terminates the string (C strings are null terminated)
            break; //breaks the loop
        }
        else
        {
            password[i] = temp; //stores the character that the user has typed in the i position
            cout << '*'; //prints the asterisk
        }
    }
    cout << endl;
    length = strlen(password);
    for (int i=0; i < length; i++)
    {
        if (!password[i] >= '0' && !password[i] <= '9')
        {
            check = false;
            break;
        }
    }
    if (check)
        cout<<"Valid password"<<endl;
    else if (!check)
        cout<<"Invalid password"<<endl;

    return 0;
}


This post has been edited by anonymouscodder: 15 February 2010 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#12 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: debug it

Posted 17 February 2010 - 08:00 AM

To fix my previous code add:
#include <iostream>

For the cout usage.

And change:
        if (!password[i] >= '0' && !password[i] <= '9')
        {
            check = false;
            break;
        }


To:
        if (password[i] < '0' || password[i] > '9')
        {
            check = false;
            break;
        }


And please zartasha, next time post any doubt in the topic.
Was This Post Helpful? 0
  • +
  • -

#13 Keerigan  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 04-February 10

Re: debug it

Posted 17 February 2010 - 11:12 AM

View Postanonymouscodder, on 17 February 2010 - 07:00 AM, said:

To:
        if (password[i] < '0' || password[i] > '9')
        {
            check = false;
            break;
        }


Will this not ALWAYS return true?
would it not have to be
        if (password[i] < '0' && password[i] > '9')//changed to and
        {
            check = false;
            break;
        }

This post has been edited by Keerigan: 17 February 2010 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1