-2

How can I get specified number?

For example, the number that a user enters is 4568962358.

I want to save the "96" in a variable; how do I get just the 96 or 23?

This is the real problem:

An important shipping company is making the annual inventory of the goods they have in their warehouse, for which they have implemented a system of codes that are assigned to each good that is in said warehouse. This code, which consists of 16 digits, contains the following information: unique number, indicator of whether it is fragile or not, place of origin of the property and expiration date of the property.   The assigned code structure is UUUFPPPPDDMMAAAA where:

  • UUU: unique number of the good.
  • F: digit that indicates if the good is fragile or not. If it is 0 it means that it is fragile.
  • PPPP: ASCII codes of the two letters that identify the country of origin of the good.
  • DD: Good's due date.
  • MM: Month of the expiration of the good.
  • AAAA: Year of expiration of the good.

You are asked to create a C ++ program that receives the assigned code as data and then prints the following data as shown in the example.

Enter the code: 1120677212042015

Then the program must print: Unique number: 112. Fragile(N: No; S: Yes): S . Country of origin: CH . Day, month and year of expiration: 04-12-2015 . Well it is outdated to date (N : No; S: Yes): S.

In the solution of the problem you will not be able to make use of selective structures.

Version 1

#include "stdafx.h"
#include "conio.h"
#include "iostream"


using namespace System;
using namespace std;

int main()
{
    int code;
    int UUU;
    int F;
    int PP1,PP2;
    int DD;
    int MM;
    int AAAA;


    cout << "Enter the code: "; cin >> code; // 1120677212042015

    // ...code needed here

    UUU = int(code / 10000000000000);

    cout << "\nRESULTS" << endl;
    cout << "\nUnique number: " << UUU << endl; // prints 112

    _getch();
    return 0;
}

Version 2

I'm not able to do the next parts; please help!

#include "stdafx.h"
#include "conio.h"
#include "iostream"

using namespace System;
using namespace std;

int main()
{
    double code;
    double UUU;
    double F;
    double PP1,PP2;
    double DD;
    double MM;
    double AAAA;


    cout << "Enter the code: "; cin >> code; // 1120677212042015 


    UUU = int(code / 10000000000000);
    PP1 = int(code / 10000000000) % 100;
    PP2 = int(code / 100000000) % 100;
    DD = int(code / 1000000) % 100;


    cout << "\nRESULTS" << endl;
    cout << "\nUnique number: " << UUU << endl; // 112
    cout << "Country of origin: " << (char)PP1 << (char)PP2<<endl; //CH
    cout << "Day, Month and Year of expiration: " << DD; // 12/../....


    _getch();
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Get the whole "string" and do what you want with the string. – Stan Apr 16 '18 at 03:36
  • How do you decide that you want `96`? What is the basis for choosing that number. Ditto `23`? You can post more of the problem than you have. If the whole problem is going to take many paragraphs, then it is not appropriate to post it all. If it is a single non-monstrous paragraph, it may well be sensible to post it all. You should show what you've tried. You should also decide which language you're using. The answer for C++ will not work in C and the answer for C will probably not be the best way to do it in C++. It might help if you show your code; it would help us know which language. – Jonathan Leffler Apr 16 '18 at 03:39
  • strings is not permitted. i think is just with descomposicions using % – Ernesto Pacheco Apr 16 '18 at 03:40
  • https://stackoverflow.com/questions/4261589/how-do-i-split-an-int-into-its-digits?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – A.B. Apr 16 '18 at 03:41
  • 1
    It is important to state in the question that using strings is not allowed. That complicates life. You need to specify how you know that it is the `96` that you want — and what tells you when you've found it. – Jonathan Leffler Apr 16 '18 at 03:42
  • i posted the problem. – Ernesto Pacheco Apr 16 '18 at 03:48
  • First of all, no program *directly* reads a float/int number from the standard input. It reads a string first, and then converts it to a specified storage format. So actually it'd be easier to deal with the string at the first place. – Stan Apr 16 '18 at 03:51
  • You mention `AAAA` in the outline structure of the number, but mention `YYYY` in the text. Are these the same? – Jonathan Leffler Apr 16 '18 at 03:53
  • You may want to use `scanf()` or `sscanf()` for this. – Sid S Apr 16 '18 at 04:00
  • Why are `code` and `UUU` of type `double`? They should be `int` types. Second, if you can't use string, then you need to use a 64-bit integer type and do the math to extract the information. Write out on paper what math operations are needed before writing one line of code. – PaulMcKenzie Apr 16 '18 at 04:01
  • @ErnestoPacheco The math would use division and modulus -- but I get the feeling this is what *you* are being tested on with this assignment -- whether *you* can figure out how to extract certain parts of an integer. Giving you the answer would be considered cheating, IMO. – PaulMcKenzie Apr 16 '18 at 04:07
  • @PaulMcKenzie Im a just starting to learn how to code. im learning the basics data types, logical operator ( && , || , !) logical expressions ( 5 > 2 = 1 which is true). Also in my last class i learned if - else sentences. I think the way to solve this problem is with divisons and modules. i will try. – Ernesto Pacheco Apr 16 '18 at 04:17
  • @ErnestoPacheco -- If given a 16-digit number, how would you use division and modulus to extract certain digits? There isn't one word of C++ or any computer language in that question, just math. For example, how would you obtain the rightmost 4 digits of a 16-digit number using division and mod? It's a test of your logic, not really a test of knowing C++ syntax. Once you know how to extract those digits, *then* you use C++ syntax to emulate the math. – PaulMcKenzie Apr 16 '18 at 04:20
  • @PaulMcKenzie yes, you are right – Ernesto Pacheco Apr 16 '18 at 04:23

3 Answers3

1

One way is using substr concept.

For example, if user enters input as 1120677212042015, then read it into a string and divide the string into sub-strings according to the format UUUFPPPPDDMMAAAA .

std::string sUserCode = "1120677212042015";
std::string sUniqueNumber = str.substr (0,3);
int iUniqueNumber = stoi(sUniqueNumber);
Srikanth
  • 131
  • 8
1

This is one of those occasions where the C library comes in handy :

    int UUU, F, PP1, PP2, DD, MM, AAAA;
    cout << "Enter the code: "; // 1120677212042015
    scanf("%3d %1d %2d %2d %2d %2d %4d", &UUU, &F, &PP1, &PP2, &DD, &MM, &AAAA);

This captures all the values. The spaces in the format string are not necessary, but makes it easier to read.

Sid S
  • 6,037
  • 2
  • 18
  • 24
0

u can use % and / to do this 4568962358 / 10000 = 456896 %100 = 96 or you can put it in a array and show the part of integer that u want.

int n = 4568962358;
int a[10];
for (int i = 0; i<10 ; i++)
{
    a[i] = n%10;
    n /= 10;
}

search your question in stack over flow. these questions have good answers.

moinmaroofi
  • 359
  • 1
  • 12