Solved

code a c++ program

Posted on 2007-04-09
16
272 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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