Can anyone help me with c++ programming? For this assignment you are to write a
ID: 3850963 • Letter: C
Question
Can anyone help me with c++ programming? For this assignment you are to write a C++ program that will read in UPC numbers and check them for correctness. The data includes numbers that are ok, two numbers that are wrong, and a number that is scanned backwards. data to be used for input upc 9-10679-87078-4 upc 7-93190-02004-9 upc 0-41100-33077-2 upc 0-76281-70322-0 upc 0-41270-87455-4 upc 0-41270-88000-8 upc 0-41163-41030-5 upc 0-44300-12392-9 upc 0-36000-28590-1 upc 8-00088-07214-0 sample output upc 9-10679-87078-4 ok upc 7-93190-02004-9 ok. upc 0-41100-33077-2 ok. upc 0-76281-70322-0 ok upc 0-41270-87455-4 error upc 0-41270-88000-8 ok upc 0-41163-41030-5 ok upc 0-44300-12392-9 error upc 0-36000-28590-1 ok upc 8-00088-07214-0 ok backwards Some Logic you may want to use: total e multiplier 3 loop 20 times incrementing i by 1 pick off the i'th character if the character is a digit then convert the character to a number total total number multiplier change the multiplier end loop if the total is a multiple of ten then write "ok else do the same check as above but go through the loop backwards if the total is a multiple of ten then write 'ok backwards else write 'errorExplanation / Answer
Solution:
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
int total=0;
int multiplier=3;
string val="upc 0-44300-12392-9 ";
for(int i=0;i<20;i++)
{
char c=val.at(i);
int v;
if(isdigit(c))
{
v=c-48;
total=total+v*multiplier;
multiplier=v;
}
}
if(total%10==0)
cout<<" Ok"<<endl;
else
{
total=0;
multiplier=3;
for(int i=19;i>=0;i--)
{
char c=val.at(i);
int v;
if(isdigit(c))
{
v=(int)c;
total=total+v*multiplier;
multiplier=v;
}
}
if(total%10==0)
cout<<" Ok-backwards"<<endl;
else
cout<<" error"<<endl;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.