5 Replies - 2559 Views - Last Post: 07 February 2010 - 04:07 AM Rate Topic: -----

#1 john2223322  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 10

Combination lock

Posted 06 February 2010 - 06:57 PM

You will write a program that will determine a lock combination given one single integer. The
integer will be an 8-digit, positive value. Any value without 8-digits will produce an error message. All input
values will be in the format NNDCNNDC, where N stands for a number, D stands for direction, and C stands
for count. Your program will print the date in the format “NN to the D C times”. D should be the direction
right or left, not the numerical value. For D, 1 will be right and 0 will be left.

 #include <iostream>

using namespace std;

int main(){
    
    cout << "Please input your selection: " << endl;
    
    
    int code;
    int n2;
    int c2;
    int d2;
    int c1;
    int d1;
    int n1;
    string rstring = " to the right ";
    string lstring = " to the left ";
    cin >> code;
    
    if (code > 99999999 || code < 00000000)
       cout << "Error" << endl;
    
    c2 = code % 10;
    
    d2 = code % 100;
    d2 = d2 / 10;
       cout << d2 << endl;

             
    n2 = code % 10000;
    n2 = n2 / 100;
    
    
    c1 = code % 100000;
    c1 = c1 / 10000;

    
    d1 = code % 1000000;
    d1 = d1 / 100000;
      
    
    n1 = code % 100000000;
    n1 = n1 / 1000000;
    
    cout << n1 << " to the " << d1 << " " << c1 << " times." << endl;
    cout << n2 << " to the " << d2 << " " << c2 << " times." << endl;
    
system ("pause");     

return 0;

}



I'm stuck on how to check to see if the number is and 8-digit number, and how to determine and output the correct direction...any help would be great. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Combination lock

#2 john2223322  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 10

Re: Combination lock

Posted 06 February 2010 - 07:15 PM

and I meant to take out the strings...
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Combination lock

Posted 06 February 2010 - 07:58 PM

You're close. You're on the wrong track here, though: "n1 = code % 100000000;"

Think about it like this:
//  NNDCNNDC
v = 12345678;

c = v % 10; // c == 8
v = v / 10; // v == 1234567

d = v % 10; // d == 7
v = v / 10; // v == 123456

nn = v % 100; // nn == 56
v = v / 100; // v == 1234



Make a function so you can do it twice.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 john2223322  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 10

Re: Combination lock

Posted 06 February 2010 - 08:07 PM

I don't think I understand....what do I need to do twice?
Was This Post Helpful? 0
  • +
  • -

#5 john2223322  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 10

Re: Combination lock

Posted 06 February 2010 - 08:53 PM

got it.
#include <iostream>

using namespace std;

int main(){
    
    cout << "Please input your selection: " << endl;
    
    
    int code;
    int n2;
    int c2;
    int d2;
    int c1;
    int d1;
    int n1;

    cin >> code;
    
    if (code > 99999999 || code < 10000000)
       cout << "Error" << endl;
       
    c1 = code % 100000;
    c1 = c1 / 10000;
      
    n1 = code % 100000000;
    n1 = n1 / 1000000;
    
    d1 = code % 1000000;
    d1 = d1 / 100000;
       if (d1 == 1)
          cout << n1 << " to the right " << c1 << " times." << endl;
       if (d1 == 0)
          cout << n1 << " to the left " << c1 << " times." << endl;
    
    c2 = code % 10;
            
    n2 = code % 10000;
    n2 = n2 / 100;
    
    d2 = code % 100;
    d2 = d2 / 10;
       if (d2 == 1)
            cout << n2 << " to the right " << c2 << " times." << endl;
       if (d2 == 0)
            cout << n2 << " to the left " << c2 << " times." << endl;
        
   
    
system ("pause");     

return 0;

}


Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Combination lock

Posted 07 February 2010 - 04:07 AM

View Postjohn2223322, on 06 February 2010 - 09:07 PM, said:

I don't think I understand....what do I need to do twice?


The same thing.

You have to do an identical operation on a four digit number. You should be able to use that to your advantage. Since you have your solution, I'll offer you mine.

#include <iostream>

using namespace std;

void processNNDC(int v) {
	int c = v % 10; v /= 10;
	int d = v % 10; v /= 10;
	int nn = v % 100;
	
	cout << nn << " to the " << ((d==1) ? "right " : "left ");
	cout << c << ((c==1) ? " time." : " times.") << endl;
 }


int main(){
	cout << "Please input your selection: " << endl;

	int code;
	cin >> code;

	if (code > 99999999 || code < 10000000) {
		cout << "Error" << endl;
		return 1;
	}
	processNNDC(code / 10000);
	processNNDC(code);
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1