GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Dec-2007, 15:41
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Finding how to get the combinations right?


Hello, I need some help on my code.

I have to create a program that reads in characters until the user enters the correct two-character sequence= (cs) to open door.

Sample Data:
cs = Door opens

ccs=Door opens

fkcds=Door doesn't open

kasfcsgd=Door opens

I have to use While Loop. but I need HELP what to put in right.

MY CODE:

CPP / C++ / C Code:
#include <iostream.h>
void main()
{
	char mem,
		first = 'cs';

	cout<<"Enter the Password: ";
	cin>>mem;

	while(mem!= first)
	{
		cout<<"Access Denied!!\n";

		cout<<"Enter the Password: ";
		cin>>mem;
	}

	cout<<"Access Accepted!!\n\n";

}

***The code doesn't really go that great. When I enter fgscs. It says Access Denied then Access Accepted. When I enter fsggs, It says Access Accepted. I dont get it. PLZZ HELP***
  #2  
Old 06-Dec-2007, 16:18
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Re: Finding how to get the combinations right?


Sorry I got it working, but when I put the Sample Data like: asfgdcs. It says Access Denied like 7 or 6 times. then it says Access Accepted.

The New code I made is right but It says Access Denied and then, like if you entered afscs. then it says Access Accepted, If you put sdgsf: then it would say Access Denied then Enter Password again until you get it.

It works, but I need help getting the Access Denied removed.

MY NEW CODE

CPP / C++ / C Code:
#include <iostream.h>
 void main() 
 { 
	 char mem, first = 'c', 
		 second = 's'; 
	 
	 cout<<"Enter the Password: "; 
	 cin>>mem; while(mem != first && second) 
	 { 
		 cout<<"Access Denied!!\n"; 
		 
		 cout<<"Enter the Password: "; 
		 cin>>mem; 
 
	} 
	 
	 cout<<"Access Accepted!\n\n"; 
 
 }
  #3  
Old 06-Dec-2007, 17:22
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 347
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Finding how to get the combinations right?


CPP / C++ / C Code:
#include <iostream.h>
 void main() 
 { 
	 char mem, first = 'c', 
		 second = 's'; 
	 
	 cout<<"Enter the Password: "; 
	 cin>>mem; while(mem != first && second) 
	 { 
		 cout<<"Access Denied!!\n"; 
		 
		 cout<<"Enter the Password: "; 
		 cin>>mem; 
 
	} 
	 
	 cout<<"Access Accepted!\n\n"; 
 
 }
You define mem to be of type 'char'. 'char' stands for 'character', and it quite means it. It is just one character. You can't store for example "dude" in a char.

So when you enter something like "sfsdfsdio", here's what happens:

Your program reads one character at a time and tests whether that character equals first. If it does not, it reads the second and so on. It never reads the entire line and thus cannot compare the entire line.

And the test in your while () works like this:

IF mem NOT first
AND
IF second

Since second is non-zero, it is always true, but your program doesn't test mem against second at any point. If you want to do it with chars, you need two of them. Then you need to could make up some pretty compicated structure for testing etc.

Or, you could strings, since such a facility is there for you to freely use. I would do it with something as follows:

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string guess;
    string correct = "cs";

    cout << "enter the, umm, password: ";
    getline(cin, guess);    // read the whole line
    while (guess.find(correct) == string::npos)    // find returns npos if string not found
    {
        cout << "Wrong wrong wrong\nTry again: ";
        getline(cin, guess);
    }

    cout << "Great." << endl;

    return 0;
}

Not sure if it compiles on your compiler though, since you are using iostream.h and void main() in your code.. If it doesn't compile, you need a more recent compiler.
  #4  
Old 06-Dec-2007, 17:57
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Re: Finding how to get the combinations right?


Thanx alot!

I got it now!!!

  #5  
Old 07-Dec-2007, 04:56
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 347
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Finding how to get the combinations right?


Quote:
Originally Posted by fomi101
I got it now!!!
Mind also posting your solution here for the world to see?
  #6  
Old 07-Dec-2007, 16:15
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Re: Finding how to get the combinations right?


Quote:
Originally Posted by Kimmo
Mind also posting your solution here for the world to see?

The code I got is the code you posted ???

So, I don't really need to put mine ?
  #7  
Old 07-Dec-2007, 18:30
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 347
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Finding how to get the combinations right?


Quote:
Originally Posted by fomi101
The code I got is the code you posted ???

So, I don't really need to put mine ?
Then I really don't see how "you got it now", since my solution doesn't even work on the same principles as yours.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
combinations cpit C++ Forum 5 14-Oct-2006 11:31
Need help in finding adsense ID Paramesh AdSense Forum 3 19-Feb-2006 14:56
Finding mode in array of numbers Snowcat C++ Forum 3 18-Jan-2006 07:57
[c++] number combinations balusss C++ Forum 8 11-Dec-2005 22:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:10.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.