Switch/Break taking me for a loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 2
Reputation: bocabomb85 is an unknown quantity at this point 
Solved Threads: 0
bocabomb85 bocabomb85 is offline Offline
Newbie Poster

Switch/Break taking me for a loop

 
0
  #1
Mar 29th, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,267
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Switch/Break taking me for a loop

 
-7
  #2
Mar 29th, 2009
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.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: bocabomb85 is an unknown quantity at this point 
Solved Threads: 0
bocabomb85 bocabomb85 is offline Offline
Newbie Poster

Re: Switch/Break taking me for a loop

 
0
  #3
Mar 29th, 2009
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
  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)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Switch/Break taking me for a loop

 
0
  #4
Mar 30th, 2009
Looking at your previous post, there's an orphan if there ...
            break;
            case '#':
                if 
            break;
            default:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC