Solved

code a c++ program

Posted on 2007-04-09
16
269 Views
Last Modified: 2010-04-01
hello guys.i would like to code this program. i just got confused how to do it. any help?. thanks

input a four digit integer

while integer > 0

               

                divide integer by 1000 to get temp

                add 7 to temp

                divide (modulus) temp by 10 to get new first digit

 

                divide (modulus) integer by 1000 to get temp

                divide temp by 100

                add 7 to temp

                divide (modulus) by 10 to get new second digit

 

                divide (modulus) integer by 1000 to get temp

                divide (modulus)temp by 100

                divide temp by 10

                add 7 to temp

                divide (modulus) by 10 to get new third digit

 

                divide (modulus) integer by 1000 to get temp

                divide (modulus) temp by 100

                divide (modulus) temp by 10

                add 7 to temp

                divide (modulus) by 10 to get new fourth digit

 

                set temp to new first digit                                                                  

                multiply new third digit by 1000 to get first

                multiply temp by 10 to get third

 

                set temp to new second digit                                                        

                multiply new fourth digit by 100 to get new second digit

                set fourth digit to temp

 

                add first, second, third, fourth to get encrypted number

 

output encrypted number

 

                input four digit integer
0
Comment
Question by:FRANCOIS_DUQUESNE
  • 8
  • 7
16 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18880289
to divide use operator '/' and for mod use operator '%'
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18880783
You pretty much laid out in pseudo code what you want to do. Now it's just a matter of transforming it into code.

Are there any specific pseudo code lines you have a problem with ?

Did you give this a try yet ? If so, can you show us ?
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18887643
hi guys. i was reaaly confuse on this problem. i didn't know how to start. especially with the modulus things made it more confusing to me. here all what i have done

# include <iostream.h>
int main()
{
input a four digit integer

while integer > 0

               

                divide integer by 1000 to get temp

                temp= 7 +temp;//add 7 to temp

                divide (modulus) temp by 10 to get new first digit

 

                divide (modulus) integer by 1000 to get temp

                temp/100;//divide temp by 100

                temp= 7+ temp;//add 7 to temp

                divide (modulus) by 10 to get new second digit

 

                divide (modulus) integer by 1000 to get temp

                divide (modulus)temp by 100

                temp/100; //divide temp by 10

                temp= 7+ temp; //add 7 to temp

                divide (modulus) by 10 to get new third digit

 

                divide (modulus) integer by 1000 to get temp

                divide (modulus) temp by 100

                divide (modulus) temp by 10

                temp= 7 + temp;//add 7 to temp

                divide (modulus) by 10 to get new fourth digit

 

                set temp to new first digit                                                                  

                multiply new third digit by 1000 to get first

                multiply temp by 10 to get third

 

                set temp to new second digit                                                        

                multiply new fourth digit by 100 to get new second digit

                set fourth digit to temp

 

                encrypted_number= first + second + third + fourth;// get encrypted number

 

cout<<"\n encrypted_number= " <<encrypted_number;// output encrypted number
   return 0;
}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18887769
That's a good start. Just a small comment on the code you have : <iostream.h> is deprecated. You should use <iostream>

Now, some hints to continue :

1) for "input a four digit integer", check out this tutorial on basic input/output in C++ :

        http://www.cplusplus.com/doc/tutorial/basic_io.html

    there are several examples and explanations on that page that should help you forward.


2) for "while integer > 0", check out this tutorial on control structures in C++ :

        http://www.cplusplus.com/doc/tutorial/control.html

    (scroll down to "the while loop"). Again lots of examples and explanations.


3) integer division is achieved with the operator /, and modulo with the operator %. More information on this page :

        http://www.cplusplus.com/doc/tutorial/operators.html



These three hints should allow you to convert more of the pseudo code into real code. Give it a go, and post your results here.

btw, I would advise you to read the rest of that tutorial too : it's a very good read when you're starting out in C++ :

        http://www.cplusplus.com/doc/tutorial/
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18887880
thanks infinity08 for your help.. here what i have so far
# include <iostream>
using namespace std;
int main()
{
int temp, first, second, third, fourth;
int new_first_digit, new_second_digit;
int new_third_digit, new_fourth_digit;
int a,b,c,d;
cin>>a>>b>>c>>d; // input a four digit integer
while (a,b,c,d > 0)
{

               

                Temp=a/1000; //divide integer by 1000 to get temp

                temp= 7 +temp; //add 7 to temp

                new_first_digit= temp % 10; // divide (modulus) temp by 10 to get new first digit

 

                temp= b % 1000; // divide (modulus) integer by 1000 to get temp

                temp= temp/100;// divide temp by 100

                temp= 7+ temp;//add 7 to temp

               new_second_digit= b %10;// divide (modulus) by 10 to get new second digit

 

                temp= c%1000; // divide (modulus) integer by 1000 to get temp

                temp= temp %100;//divide (modulus)temp by 100

                temp= temp/100; //divide temp by 10

                temp= 7+ temp; //add 7 to temp

                new-third_digit=c %10;//divide (modulus) by 10 to get new third digit

 

                temp= d%1000; //divide (modulus) integer by 1000 to get temp

                temp= temp % 100;//divide (modulus) temp by 100

                temp= temp%10;//divide (modulus) temp by 10

                temp= 7 + temp;//add 7 to temp

                new_fourth_digit= d%10; // divide (modulus) by 10 to get new fourth digit

 

               temp=new_first_digit;// set temp to new first digit                                                                  

                first= new_third_digit * 1000;// multiply new third digit by 1000 to get first

               third= temp * 10;// multiply temp by 10 to get third

 

               temp=new_second_digit;// set temp to new second digit                                                        

                new_second_digit= new_fourth_digit*100;// multiply new fourth digit by 100 to get new second digit

               fourth=temp; // set fourth digit to temp

 

                encrypted_number= first + second + third + fourth;// get encrypted number

       }

cout<<"\n encrypted_number= " <<encrypted_number;// output encrypted number
   return 0;
}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18887996
1) for the input :

        cin>>a>>b>>c>>d; // input a four digit integer

    This will input 4 integers, not a 4-digit integer.
    You'll need to let the user input one integer, and then you'll have to verify that it's a 4-digit one.

    Notice that this change will mean that all uses of a, b, c and d should become a.


2) variable names in C++ are case sensitive. For example, "Test" is not the same as "test". Check this line for example :

        Temp=a/1000; //divide integer by 1000 to get temp


3) This line :

        temp= temp/100; //divide temp by 10

    look closely for a difference between the pseudo code and the C++ code you used.


4) - is not the same as _. Check this line :

        new-third_digit=c %10;//divide (modulus) by 10 to get new third digit


5) the lines where you extract the digits need to work on temp. I'm talking about these lines :

        new_second_digit= b %10;// divide (modulus) by 10 to get new second digit
        new-third_digit=c %10;//divide (modulus) by 10 to get new third digit
        new_fourth_digit= d%10; // divide (modulus) by 10 to get new fourth digit

    the one you made for the first digit was correct :

        new_first_digit= temp % 10; // divide (modulus) temp by 10 to get new first digit


6) this line :

        encrypted_number= first + second + third + fourth;// get encrypted number

    is most likely not correct. But we'll work on that later, once the rest of the program is working.


Please make the above modifications.
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18888075
thanks buddy. i made all the modifications that you told me about. i feel embarassed that i didn't pay attention about the four digit integer.
# include <iostream>
using namespace std;
int main()
{
int temp, first, second, third, fourth;
int new_first_digit, new_second_digit;
int new_third_digit, new_fourth_digit;
int a;
cout<< "\n enter a four digit integer \n";
cin>>a; // input a four digit integer
while (a > 0)
{

               

                temp=a/1000; //divide integer by 1000 to get temp

                temp= 7 +temp; //add 7 to temp

                new_first_digit= temp % 10; // divide (modulus) temp by 10 to get new first digit

 

                temp= a% 1000; // divide (modulus) integer by 1000 to get temp

                temp= temp/100;// divide temp by 100

                temp= 7+ temp;//add 7 to temp

               new_second_digit= temp %10;// divide (modulus) by 10 to get new second digit

 

                temp= a%1000; // divide (modulus) integer by 1000 to get temp

                temp= temp %100;//divide (modulus)temp by 100

                temp= temp/10; //divide temp by 10

                temp= 7+ temp; //add 7 to temp

                new_third_digit=temp %10;//divide (modulus) by 10 to get new third digit

 

                temp= a%1000; //divide (modulus) integer by 1000 to get temp

                temp= temp % 100;//divide (modulus) temp by 100

                temp= temp%10;//divide (modulus) temp by 10

                temp= 7 + temp;//add 7 to temp

                new_fourth_digit= temp%10; // divide (modulus) by 10 to get new fourth digit

 

               temp=new_first_digit;// set temp to new first digit                                                                  

                first= new_third_digit * 1000;// multiply new third digit by 1000 to get first

               third= temp * 10;// multiply temp by 10 to get third

 

               temp=new_second_digit;// set temp to new second digit                                                        

                new_second_digit= new_fourth_digit*100;// multiply new fourth digit by 100 to get new second digit

               fourth=temp; // set fourth digit to temp

 

                encrypted_number= first + second + third + fourth;// get encrypted number

       }

cout<<"\n encrypted_number= " <<encrypted_number;// output encrypted number
   return 0;
}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18888091
                new_second_digit= new_fourth_digit*100;// multiply new fourth digit by 100 to get new second digit

I think the above line was a mistake in the pseudo code, and should have been :

                second= new_fourth_digit*100;// multiply new fourth digit by 100 to get new second digit
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 53

Expert Comment

by:Infinity08
ID: 18888099
And declare encrypted_number as an int. Then try to compile, and watch the result :)
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18888239
ok . i did compile and execute it. there was no errors at all. but when i execute =d it. the black box appeared and ask me to enter four digit number so i did and nothing happened then.
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18888244
that's what i have

# include <iostream>
using namespace std;
int main()
{



int temp, first, second, third, fourth;
int new_first_digit, new_second_digit;
int new_third_digit, new_fourth_digit;
int encrypted_number;
int a;
cout<< "\n enter a four digit integer \n";
cin>>a; // input a four digit integer
while (a > 0)
{

               

                temp=a/1000; //divide integer by 1000 to get temp

                temp= 7 +temp; //add 7 to temp

                new_first_digit= temp % 10; // divide (modulus) temp by 10 to get new first digit

 

                temp= a% 1000; // divide (modulus) integer by 1000 to get temp

                temp= temp/100;// divide temp by 100

                temp= 7+ temp;//add 7 to temp

               new_second_digit= temp %10;// divide (modulus) by 10 to get new second digit

 

                temp= a%1000; // divide (modulus) integer by 1000 to get temp

                temp= temp %100;//divide (modulus)temp by 100

                temp= temp/10; //divide temp by 10

                temp= 7+ temp; //add 7 to temp

                new_third_digit=temp %10;//divide (modulus) by 10 to get new third digit

 

                temp= a%1000; //divide (modulus) integer by 1000 to get temp

                temp= temp % 100;//divide (modulus) temp by 100

                temp= temp%10;//divide (modulus) temp by 10

                temp= 7 + temp;//add 7 to temp

                new_fourth_digit= temp%10; // divide (modulus) by 10 to get new fourth digit

 

               temp=new_first_digit;// set temp to new first digit                                                                  

                first= new_third_digit * 1000;// multiply new third digit by 1000 to get first

               third= temp * 10;// multiply temp by 10 to get third

 

               temp=new_second_digit;// set temp to new second digit                                                        

                second = new_fourth_digit*100;// multiply new fourth digit by 100 to get new second digit

               fourth=temp; // set fourth digit to temp

 

                encrypted_number= first + second + third + fourth;// get encrypted number

       }

cout<<"\n encrypted_number= " <<encrypted_number;// output encrypted number
   return 0;
}


               
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18888269
>> the black box appeared and ask me to enter four digit number so i did and nothing happened then.

Did you press ENTER after giving the number ?

If so, did the black window disappear ?

If the black window disappeared, just add this before the return 0; in your main :

        int main(void) {
            // your code

            system("PAUSE");
            return 0;
        }

and add this include at the top :

        #include <cstdlib>

Doing this will avoid that the program closes before you can see its output.
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18888281
yes i did press enter
the black box didn't disappear
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18888306
ok infinity 08. it 's almost 5 a.m . it is time to go to bed. thanks for everyting i will catch you later.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18888310
Ok, here are still a few problems with your code :

1) You have a space between # and include here :

        # include <iostream>

    remove that space !

2) You didn't implement your whole pseudo code, and because of that you got into an infinite loop. I'm talking about this part at the end :

                add first, second, third, fourth to get encrypted number

                output encrypted number

                input four digit integer

    Note that all of this has to be INSIDE the while loop !!
    You can end the program by entering 0 when asked to enter the number.
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18890607
hello infinity08. i put it inside the loop and it did work. that's great. thanks so much.so now we have one problem down.
FRANCOIS
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now