Home Forums C Programming Question about my program code

Viewing 0 reply threads
  • Author
    Posts
    • #2023
      happyherlean
      Participant

      Hi!
      I’m a beginner in C++ The program below inputs sevaral positive numbers with decimal points, analyzes these numbers, and outputs the result. The program works fine. But I wanted to make sure that a user puts only positive numbers or -1 to end repetition loop. So, I created couple of functions that would check the numbers, return them in case if they are positive or equal -1. In case they are negative, the program would output warning message and ask to enter the number again. It also work fine, but if I enter negative number and than enter positive number, the program saves negatives number, although I return the value from the function. Can anyone please explain me where my error is. Thanks!

      P.S. I know that it’s possible to use template functions when you have a function that does the same operations, but with different types of variables. Can I use this concept with my problem when I have similar functions, but different names of variables? If yes, can anyone please show me how? Thanks!

      #include 
      #include
      #include
      using namespace std;


      int get_acc_number(int);
      float get_b_balance(float);
      float get_charges(float);
      float get_credits(float);
      float get_cr_limit(float);
      void text(void);



      int main(int argc, char *argv[])
      {
           int acc_number = 0;
           float b_balance, charges, credits, cr_limit, balance;
           
      while (acc_number != -1)
         {
           cout << "Enter account number (-1 to end): ";
           cin >> acc_number;
           get_acc_number(acc_number);
           if (acc_number != -1)
             {
              cout << setiosflags (ios::fixed | ios::showpoint | ios::left);
              cout << setprecision (2) << "Enter beginning balance: ";
                cin >> b_balance;
                get_b_balance(b_balance);
                cout << "Enter total charges: ";
              cin >> charges;
              get_charges(charges);
              cout << "Enter total credits: ";
              cin >> credits;
              get_credits(credits);
              cout << "Enter credit limit: ";
              cin >> cr_limit;
              get_cr_limit(cr_limit);
              balance = b_balance + charges - credits;
              cout << "n";
              if (balance > cr_limit)
                  {
                     cout << setiosflags (ios::fixed | ios::showpoint | ios::left);
                      cout << "Account: " << setw (15) << acc_number << endl;
                      cout << "Credit limit: " << setw (15) << cr_limit << endl;
                      cout << "Balace: " << setw (15) << balance << endl;
                      cout << "Credit Limit exeededn" << endl;
                   }
            
              }
         }

          system("PAUSE");
          return EXIT_SUCCESS;
      }

      int get_acc_number(int acc_number)
          {
           if (acc_number == -1 || acc_number >= 0)
                return acc_number;
                else
                {
                    text();
                    cout << "Enter account number (-1 to end): ";
                    cin >> acc_number;
                    return get_acc_number(acc_number);    
                }           
          }
      float get_b_balance(float b_balance)
          {
           if (b_balance >= 0)
                return b_balance;
                else
                {
                    text();
                    cout << "Enter beginning balance: ";
                     cin >> b_balance;
                    return get_b_balance(b_balance);    
                }          
             
          }
      float get_charges(float charges)
      {
           if (charges >= 0)
                return charges;
                else
                {
                    text();
                    cout << "Enter total charges: ";
                    cin >> charges;
                    return get_charges(charges);    
                }          
             
      }
      float get_credits(float credits)
      {
           if (credits >= 0)
                return credits;
                else
                {
                    text();
                    cout << "Enter total credits: ";
                    cin >> credits;
                    return get_credits(credits);    
                }          
             
      }
      float get_cr_limit(float cr_limit)
      {
           if (cr_limit >= 0)
                return cr_limit;
                else
                {
                    text();
                    cout << "Enter credit limit: ";
                    cin >> cr_limit;
                    return get_cr_limit(cr_limit);    
                }          
             
      }
      void text(void)
      {
           cout << "The number you entered is less than 0, "
                << "and, therefore invalid. "
                << "nPlease, try againn"
                << "n";
      }

Viewing 0 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.