943,600 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 748
  • C++ RSS
Mar 29th, 2009
0

Switch/Break taking me for a loop

Expand Post »
Hello, I am reaally green to C++
1. I am trying to create a code that calculates and then produces a small invoice.

Input: Company Name
Input: Account Number
Input: Opening Balance
Input: Account Type:
I=invoiceReceived P=invoicePayment #endTransaction

Input: Amount Invoiced
Input: Amount Paid

If someone with experience were to see the code it would probably be easy, but I cannot say I am quite there yet.

2. My code is always resulting in a syntax error:'break' 2059. Line 114

3. I would like my code to produce an invoice displaying only the companyName, accountNum, and the endingBalance,

however it gets stuck in athe loop i constructed not allowing me to get further than I,P, or # selection.

4. There are no contents of any input files, all data can be made up on the spot.

C++ Syntax (Toggle Plain Text)
  1. // program_id APAccounts.cpp
  2. // written_by matt schwartz
  3. // date_written 3/22/2009
  4. // description This program will get account transactions and return
  5. // account balances and the sum of account balances.
  6. //
  7. // #include <fstream>
  8. // #include<conio.h>
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15. #define clearScreen() {for(int i=0; i<=25; ++i) {cout << endl; cout << flush;}}
  16.  
  17. // functions
  18. char moreTrans(); // Asks if there are more transactions
  19. void getAccountInfo(); // function gets company name and account number, stores results global variable
  20. double displayAccountSummary(double openBalance); // does the math and returns the closing balance
  21. double transaction(); // function to get the transaction info into an array
  22. double sumArray(const double list[], int listSize); // function that will sum an array
  23.  
  24. // global variables
  25. int invoiceArraySize = 0;
  26. int paymentArraySize = 0;
  27. double beginBalance = 0;
  28. double invoice[20] = {0}; // arrays for storing the info we need
  29. double payment[20] = {0}; // to process
  30. double accountTotals[40] = {0};
  31. string companyName; // suppliers company name
  32. string accountNum; // Little shops account number with this company
  33.  
  34. int main()
  35. {
  36. char flag = 'Y'; // loop control variable, exit when the user says
  37. // there are no more accounts to enter.
  38.  
  39. int counter = 0; // array element counter, # of accounts processed
  40.  
  41. double totalPayable = 0;
  42.  
  43. while (flag != 'N') // enter loop
  44. {
  45. getAccountInfo(); // calling a function, Asks for company name and account number and stores
  46.  
  47. accountTotals[counter] = transaction(); // calling a function, gets the details of account
  48. // transaction returns the ending balance of thr current account ending balance of each account is an array element
  49. counter = counter++;
  50. displayAccountSummary(beginBalance);
  51. cout << "Would you like to enter more accounts?(Y/N)" << endl;
  52. cin >> flag;
  53. }
  54.  
  55. totalPayable = sumArray(accountTotals, counter);
  56. cout << fixed << showpoint;
  57. cout << setprecision(2);
  58. // cout<< "The sum of the open Accounts payable is $" << sumArray(accountTotals, counter) << endl;
  59. // cout<< "The sum of the open Accounts payable is $" << totalPayable << endl;
  60. return 0;
  61. }
  62.  
  63. void getAccountInfo() // function gets company name and
  64. { // account info and stores them in
  65. cout << " Please enter the Company Name "; // global variables.
  66. cout << endl;
  67. cin >> companyName;
  68. cout << " Please enter the account number ";
  69. cout << endl;
  70. cin >> accountNum;
  71. }
  72.  
  73.  
  74. double transaction()
  75. {
  76. double transAmount = 0;
  77. char transactionType = 'I';
  78.  
  79. clearScreen();
  80. cout << endl << endl << endl << endl << endl << " Suppliers company name " << companyName << endl;
  81. cout << "Please enter the account opening balance. ";
  82. cout << endl;
  83. cin >> beginBalance;
  84.  
  85. while ( transactionType != '#') // enter loop
  86. {
  87. cout << "Please enter a transaction type ";
  88. cout << endl;
  89. cout << "I = invoice ";
  90. cout << endl;
  91. cout << "P = Payment ";
  92. cout << endl;
  93. cout << "# = no more transactions ";
  94. cout << endl;
  95. cout << "(I/P/#) ? ";
  96. cout << endl;
  97. cin >> transactionType;
  98. switch (transactionType)
  99. { // enter switch
  100. case 'I':
  101. case 'i':
  102. invoice[invoiceArraySize] = transAmount; // transaction is a debit, store the amount in the invoice array
  103. invoiceArraySize++;
  104. // transactionType = '#';
  105. break;
  106. case 'P':
  107. case 'p':
  108. payment[paymentArraySize] = transAmount; // transaction is a credit, store the amount in the payment array
  109. paymentArraySize++;
  110. // transactionType = '#';
  111. break;
  112. case '#':
  113. if
  114. break;
  115. default:
  116. cout << "Invalid response, please try again. I, P, or # ";
  117. cout << endl;
  118. cin >> transactionType;
  119. }
  120. // exit switch
  121.  
  122. }
  123. // exit loop
  124. return beginBalance;
  125. }
  126.  
  127. double sumArray(const double list[], int listSize) // function that will sum an array
  128. { // returns a sum
  129. int index;
  130. double sum = 0;
  131.  
  132. for (index = 0; index < listSize; index++)
  133. sum =sum + list[index];
  134. return sum;
  135. }
  136.  
  137. char moreTrans() // function will ask if there
  138. { // are more transactions to
  139. char resp; // procees and returns the
  140. // response
  141. cout << "Would you like to enter more transactions?(Y/N)" << endl;
  142. cin >> resp;
  143. return resp;
  144. }
  145.  
  146. double displayAccountSummary(double beginBalance)
  147. {
  148. double closingBalance;
  149.  
  150. cout << "The sum of the payments is $" << sumArray(payment, paymentArraySize);
  151. cout << endl;
  152. cout << "The sum of the invoices is $" << sumArray(invoice, invoiceArraySize);
  153. cout << endl;
  154. cout << "The opening Balance is $" << beginBalance;
  155. cout << "The closing Balance is $" << (beginBalance + sumArray(invoice, invoiceArraySize) - sumArray(payment, paymentArraySize));
  156. closingBalance = (beginBalance + sumArray(invoice, invoiceArraySize) - sumArray(payment, paymentArraySize));
  157. return closingBalance;
  158.  
  159. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bocabomb85 is offline Offline
2 posts
since Mar 2009
Mar 29th, 2009
-7

Re: Switch/Break taking me for a loop

For starters, you are missing a break at the end of your default block

...

default:
cout << "Invalid response, please try again. I, P, or # ";
cout << endl;
cin >> transactionType;
break;
}
Last edited by jbennet; Mar 29th, 2009 at 11:11 pm.
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,485 posts
since Apr 2005
Mar 29th, 2009
0

Re: Switch/Break taking me for a loop

I appreciate it Master Danny, no cigar though. When I refer to my C++ book the syntax doesn't work out that way. But, I will take advice from a helpful person over a book anyday? Any idea why i get stuck in the
C++ Syntax (Toggle Plain Text)
  1. while ( transactionType != '#') // enter loop
  2. {
  3. cout << "Please enter a transaction type ";
  4. cout << endl;
  5. cout << "I = invoice ";
  6. cout << endl;
  7. cout << "P = Payment ";
  8. cout << endl;
  9. cout << "# = no more transactions ";
  10. cout << endl;
  11. cout << "(I/P/#) ? ";
  12. cout << endl;
  13. cin >> transactionType;
  14. switch (transactionType)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bocabomb85 is offline Offline
2 posts
since Mar 2009
Mar 30th, 2009
0

Re: Switch/Break taking me for a loop

Looking at your previous post, there's an orphan if there ...
            break;
            case '#':
                if 
            break;
            default:
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: TooTiredToSeeProblem
Next Thread in C++ Forum Timeline: Inheritence and constructors question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC