if i have 4 intergers for example :
1378 2496 3587 5389
how can i use the modulus operator to disect them so i can get the first number of each integer
what i mean is how can i take 1 form 1378 and 2 from 2496 etc...
int Value = 137;
if(Value < 100 || Value > 999)
{/*Error*/}
else
{
int Votes[3] = {0};
Value -= (Votes[2] = (Value % 10) / 1);
Value -= (Votes[1] = (Value % 100) / 10);
Value -= (Votes[0] = (Value % 1000) / 100);
}
I haven't tested but this should work.
Votes[0] will have the first number (1)
Votes[1] will have the second number (3)
Votes[2] will have the third number (7)
To receive multiple inputs edit line 2 to switch "ranges", set the Size of the Votes array to the required number of votes and add more lines to the "Value -= (Votes[x] = ..." who follow those logics.
Take the first integer number 1378%10==8(do something) now divide by 10 and you have 137(integer division)
137%10=7(do something) now divide 137 by 10 and you have 13.
13%10 = 3(do something) now divide by 10 and you have 1.
Put the above in a loop and when you isolate each individual digit call a function to carry out each simple calculation.
If you place these results in a container such as a vector or even an array element [0] would be contestant 1, element [1] would be contestant 2. ... and son. It would then be a simple task to print the five individual results and that of the winner.
- They way your problem is presented, i would just have them enter the number as a string, then cast each character into an array or something along those lines.
int Value = 137;
if(Value < 100 || Value > 999)
{/*Error*/}
else
{
int First = 0;
int Second = 0;
int Third = 0;
Value -= (Third = (Value % 10) / 1);
Value -= (Second = (Value % 100) / 10);
Value -= (First = (Value % 1000) / 100);
}
here is what i get at first but the instructor told me the number should be inmputs ...and i should use modulus :
# include <iostream>
using namespace std;
int main()
{
int c=1; // candidate number
int a,b,d,s; // a:grade for beauty b:grade for elegance d: grade for education s:result for candidates
int max_result=0;
int winner=0;
while (c<=5)
{
cout<<"enter the grade for beauty:";
cin>>a;
cout<<"enter the grade for elgance:";
cin>>b;
cout<<"enter the grade for education:";
cin>>d;
cout<<c;
c++;
cout<<a;
cout<<b;
cout<<d<<endl;
a=a*5;
b=b*2;
d=d*3;
s=a+b+d;
cout<<"result of the candidate is\t"<<s<<endl;
if(s>max_result)
{
max_result=s;
winner=c-1;
}
}
cout<<"candidate\t"<<winner<<" \t win with score of "<<max_result<<endl;
}
You are receiving each number in a different input...
This had nothing to do with the question, you asked how to take an arbitrary digit from a number.