Jump to content

C++ assignment help

- - - - -

  • Please log in to reply
10 replies to this topic

#1
maria536

maria536

    Newbie

  • Members
  • PipPip
  • 21 posts
Can someone please help me with this code,

Screen should output this:

Enter Five Digit number: 12345
Ones digit is: 5
Tens digit is: 4
Hundreds digit is: 3
Thousands digit is: 2
Ten-thousands digit is: 1
Press any key to continue



#include <iostream> // required to perform C++ stream I/O

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution
int main()
{
    //define variables
    int num; //stores five digit numbers
    int onesplace;
    int tensplace;
    int hundredsplace;
    int thousandsplace;
    int ten_thousandsplace;

    //prompt user to input a five digit number
    cout <<"Input a five_digit number:";
    cin >> num; //read user input
        
    onesplace= num%10; //returns digit in ones place
    
    cout <<"\nOnes digit is:"<< onesplace;

    num =num/10; //returns digit in tens place
    tensplace= num;
    
    cout <<"\nTens digit is:"<< tensplace;

    num =num/10/10; //returns digit in hundreds place
    hundredsplace= num;
      
    cout <<"\nHundreds digit is:"<< hundredsplace;

    num =num/10/10/10; //returns digit in thousands place
    thousandsplace= num;

    cout <<"\nThousands digit is:"<< thousandsplace;

    num =num/10/10/10/10; //returns digit in ten_thousands place
    ten_thousandsplace= num;

    cout <<"\nTen_Thousands digit is:"<< ten_thousandsplace;

   return 0; // indicate that program ended successfully

} // end function 
 
But it is not printing this way.

Thanks

Edited by WingedPanther, 15 September 2011 - 07:07 AM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Moved to the correct forum.
code tags added to make code readable.

You need to use the % operator as well as / for your output.
For each digit, only divide by 10 once, since you're storing the results in num again.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
maria536

maria536

    Newbie

  • Members
  • PipPip
  • 21 posts
Not sure what you mean.

---------- Post added at 07:49 AM ---------- Previous post was at 07:38 AM ----------

Disregard last message, figures it out. Thanks for your help!!!:rolleyes:

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
What if a user enters more than 5 digits? There's a challenge for you. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
maria536

maria536

    Newbie

  • Members
  • PipPip
  • 21 posts
I am a newbie and C++ is very confusing to me, but if you would like you could help me another assignment that I have.

#6
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts

WingedPanther said:


You need to use the % operator as well as / for your output.
As said by WingedPanther -- you can apply a little bit effort,

#include <iostream>


using namespace std;


int main()

{

	int num, a1, a2, b1, b2, c1, c2, d1, d2;


	cout << "Enter Five digit number";

    cin >> num;


    a1 = num / 10000;

    a2 = num % 10000;


    b1 = a2 / 1000;

    b2 = a2 % 1000;


    c1 = b2 / 100;

    c2 = b2 % 100;


    d1 = c2 / 10;

    d2 = c2 % 10;


    cout << "Ones digit is: " << d2

    		<< "\nTens digit is: " << d1

    		<< "\nHundreds digit is: " << c1

    		<< "\nThousands digit is: " <<  b1

    		<< "\nTen-thousands digit is: " << a1;


    return 0;

}


Output:

Enter Five Digit number: 12345

Ones digit is: 5

Tens digit is: 4

Hundreds digit is: 3

Thousands digit is: 2

Ten-thousands digit is: 1


perhaps there might be a better way. Hope this helps!
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#7
maria536

maria536

    Newbie

  • Members
  • PipPip
  • 21 posts
This did help, I just wish I could understand how to put all this together, it doesn't really explain how to exactly write code when your programming, I guess you just have to figure out how to put it all together, which is the problem I have, I don't know how to put it all together, any suggestions!!!! Write now I have to write an Encryption application, where I have to prompt the user to enter 4 digit integers, bollowed by a statement using cin to input the value that the user enters into the numb variable.

Define a variable to cotain user input
Extracting the digit from the user input
Encrypting each digit and diplay the encrypted results.

and I don't even know where to begin, the only thing I have so far is;


#include <iostream> // required to perform C++ stream I/O

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution
int main()
{
int five_digit number
int digit1
int digit2
int digit3

cout <<"enter four digit number"<<

return 0; // indicate that program ended successfully

} // end function main

#8
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
As far you were concerned with splitting the number so it's done. Now you have to encrypt it. Your encryption algorithm might be like, split the number get each digit add x to it get it's remainder etc etc. Then you can display the encrypted value. You can decrypt it back as you have encrypted it.
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#9
maria536

maria536

    Newbie

  • Members
  • PipPip
  • 21 posts
can I use an if...else

#10
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
A simple example might be,

A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data are transmitted as four-digit integers. They have asked you to write a program that ecrypts their data so that it can be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. Then, swap the first digit with the third, swap the second digit with the fourth and print the encrypted integer.

The code might look like,


#include <iostream>


using namespace std;


void swaps(int f1, int s2, int t3, int t3mod)

{

	int swap1, swap2;


	swap1 = f1;

	f1 = t3;

	t3 = swap1;


	swap2 = s2;

	s2 = t3mod;

	t3mod = swap2;


    cout << f1 << s2 << t3 << t3mod;


}


void split(int n)

{

	int f1, f1mod, s2, s2mod, t3, t3mod;


	f1 = n / 1000;

	f1mod = n % 1000;


	s2= f1mod / 100;

	s2mod = f1mod % 100;


	t3 = s2mod / 10;

	t3mod = s2mod % 10;


	f1 += (f1 + 7) % 10;

	s2 += (s2 + 7) % 10;

	t3 += (t3 + 7) % 10;

	t3mod += (t3mod + 7) % 10;


	swaps(f1, s2, t3, t3mod);

}


int main()

	{

   	int num;


      cout<<"Enter a four digit code to Encrypt it :";

      cin>>num;


      split(num);


   return 0;


   }


I think i'm able to write a code for printing "Hello, World!". Proud of that!

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
For most programming problems, start by figuring out what you would have to do by hand. Anything you need to remember, write down on paper. Note calculations, and results. Once you've done that, you can write code to replicate that.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users