• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

I'm complete lost with this assingment!!!!

This is my homework programming class c++ intermedia:
i need to write a program that will check the validity of an account number that has been sent to the bank as part of its daily transactions, so the account number willbe nine digits long and the ningth digit (rightmost) can be calculated by either adding, subtracting, or multiplying all of the preceding digits and finding the rigthmost digit of the result. if the two rigthmost digits match, the account number is valid. In the operation code (first digit), 1 is addition, 2 is subtraction, and 3 is multiplication. any other digit in this position is incorrect.
0
jaimvatr
Asked:
jaimvatr
  • 5
  • 2
1 Solution
 
rajeev_devinCommented:
Post some code.
0
 
AndyAinscowCommented:
First - this is homework, so the experts here are *forbidden* to do it for you.

I'm puzzled by your question as the details you give are the answer you want.
You have the nine digit number.
First digit.   1 is addition, 2 is subtraction, and 3 is multiplication. any other digit in this position is incorrect.  (You now have a valid or invalid number)
Now you know what you need to do to get the total (add, subtract, multiply).
You now have the total - is the right most digit the same as the rightmost in the code.  So you know if the code is valid or not.
0
 
AndyAinscowCommented:
Don't forget when str="149" that str[0] = '1', str[1] = '4', str[2] = '9' and also that character '4' -  character '0' is the number 4
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
jaimvatrAuthor Commented:
this is the code that I did it , but my professor don't like it. he said is poor logic and code!!!! somebody can help me

#include <iostream>
#include <string>
#include <iomanip>

#include <cctype> // for isdigit

using namespace std;

int main()
{
      int i; // auxiliar variable to check digit by digit
      int j; // stop to show results
      int baddata; // flag to identify if account is valid or invalid
      int totalsum; // total sum if operation is sum or substraction
      int totalmul; // total if operation is multiplication
      int totalcontrol; // variable used to truncate the total of control
      int finaltotal; // auxiliar variable to check the validity of the check// digit
      string accountno; // account number
      string message; // returned message
      cout << "Enter Account No" << '\n';
      cin >> accountno;
      message = "*** VALID ACCOUNT No ***";
      i=0;
      baddata = 0;
      totalsum = 0;
      totalmul = 1;
      totalcontrol = 0;
      finaltotal = 0;
      // check length of the account number
      if (accountno.length() != 9 )
      {
            message = "*** INVALID ACCOUNT No. ***";
            baddata = 1;
      }
      // check operation code
      if (baddata == 0)
      {
            if (accountno[0]=='1' || accountno[0]=='2' || accountno[0]=='3')
                  baddata = 0;
            else
            {
                  baddata = 1;
                  message = "***OPERATION CODE ERROR***";
            }
      }
      // check digit by digit and calculate total sum or total multiplication
      i = 0;
      if (baddata == 0)
      {
            do
            {
                  if (isdigit(accountno[i])==0)
                  {
                        message = "*** INVALID ACCOUNT No.***";
                        baddata = 1;
                        i = 10;
                  }
                  else
                  {
                        if (accountno[0]=='3')
                              totalmul = totalmul * (accountno[i]-'0');
                        else
                              totalsum = totalsum + (accountno[i]-'0');
                  }
                  i = i + 1;
            }while (i < 8);
      }
      // truncate the total sum or total multiplication to be able to validate the control digit
      if (baddata == 0)
      {
            if (totalsum > 0)
            totalcontrol = totalsum /10;
            else
                  totalcontrol = totalmul /10;
            
      }
      
      finaltotal = (totalcontrol * 10) + (accountno[8]-'0');
      if (baddata == 0)
      {
            if (finaltotal == totalmul || finaltotal == totalsum)
                  baddata = 0;
            else
                  message = "*** INVALID ACCOUNT CONTROL DIGIT ***";
      }
      cout << "Account No" << '\t' << accountno << '\t' << message << '\n';
      
      return 0;
}
0
 
rajeev_devinCommented:
Post some valid and invalid account numbers as an example.
0
 
AndyAinscowCommented:
use functions for example, rather than one big function.

if(NUMBER_FAILS_CHECK)
{
  PrintMessage("FAILED BECAUSE OF..");
  return 0;
}
0
 
AndyAinscowCommented:
You only require one totals variable.  Why do you need a separate one for multiplication?
0
 
AndyAinscowCommented:
I think my comments helped in solving the original question and I provided relevant comment to improve the code in response to the subsequent question.
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.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now