How to exit a loop

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 34
Reputation: neoseeker191 is an unknown quantity at this point 
Solved Threads: 0
neoseeker191 neoseeker191 is offline Offline
Light Poster

How to exit a loop

 
0
  #1
Jan 18th, 2009
I am doing a homework assignment in which we have to get an undetermained amount of numbers from the user. I was wondering how to tell when the user is done entering numbers and wants to exit the loop, such as perhaps a blank space or a word like 'quit'.

Here is what I have so far, I used 72 as the exit number just as a test.

  1. // Function Used to Get Numbers into Array
  2. int calcList() {
  3.  
  4. for (int i=0; i<100; i++) {
  5. std::cout << "Enter an integer: ";
  6. std::cin >> list[i];
  7. counter = counter + 1;
  8.  
  9. // Exit Loop When 72 is Inputed
  10. if (list[i]== 72) {
  11. return(0);
  12. }
  13.  
  14. }
  15. }

Thank You.
Last edited by Ancient Dragon; Jan 19th, 2009 at 9:24 am. Reason: corrected code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How to exit a loop

 
1
  #2
Jan 18th, 2009
  1. int calcList()
  2. {
  3. std::string inputVal;
  4. int cnt = 0;
  5. int list[100];
  6.  
  7. while (inputVal != "complete") {
  8. std::cout << "Enter an integer: ";
  9. std::getline(std::cin, inputVal);
  10.  
  11. if (inputVal != "complete") {
  12. list[cnt] = (int)inputVal.c_str();
  13. cnt++;
  14. }
  15. }
  16.  
  17. return 0;
  18. }
or something.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 34
Reputation: neoseeker191 is an unknown quantity at this point 
Solved Threads: 0
neoseeker191 neoseeker191 is offline Offline
Light Poster

Re: How to exit a loop

 
0
  #3
Jan 19th, 2009
Do I still have a counter in the function you gave me?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: 1bennyd is an unknown quantity at this point 
Solved Threads: 1
1bennyd 1bennyd is offline Offline
Newbie Poster

Re: How to exit a loop

 
0
  #4
Jan 19th, 2009
Yep, "cnt" is the counter.
A proud member of R-forum.org - A community that provides help to users of statistics and the R programming language.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 34
Reputation: neoseeker191 is an unknown quantity at this point 
Solved Threads: 0
neoseeker191 neoseeker191 is offline Offline
Light Poster

Re: How to exit a loop

 
0
  #5
Jan 19th, 2009
for some reason I am getting a divide by zero error. it doesnt seem that the counter is counting up.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: 1bennyd is an unknown quantity at this point 
Solved Threads: 1
1bennyd 1bennyd is offline Offline
Newbie Poster

Re: How to exit a loop

 
0
  #6
Jan 19th, 2009
Is this how you are running the code? (I have added a line that prints out the value of the counter.)

  1. #include <iostream>
  2.  
  3. int calcList(void)
  4. {
  5. std::string inputVal;
  6. int cnt = 0;
  7. int list[100];
  8.  
  9. while (inputVal != "complete") {
  10. std::cout << "Enter an integer: ";
  11. std::getline(std::cin, inputVal);
  12.  
  13. if (inputVal != "complete") {
  14. list[cnt] = (int)inputVal.c_str();
  15. cnt++;
  16. std::cout << "counter: " << cnt << std::endl;
  17. }
  18. }
  19.  
  20. return 0;
  21. }
  22.  
  23. int main() {
  24. calcList();
  25. }
A proud member of R-forum.org - A community that provides help to users of statistics and the R programming language.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 34
Reputation: neoseeker191 is an unknown quantity at this point 
Solved Threads: 0
neoseeker191 neoseeker191 is offline Offline
Light Poster

Re: How to exit a loop

 
0
  #7
Jan 19th, 2009
Still getting a divide by zero error.
Here is my whole code.
I have only changed the counter on calcMean() to cnt to reflect your code, and that has been the only one i have been testing.

  1. // prog1.cpp : Defines the entry point for the console application.
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. // Global Variables
  11. int calcList();
  12. int list[100] = {0};
  13. int counter = 0;
  14. int cnt = 0;
  15.  
  16. int _tmain(int argc, _TCHAR* argv[])
  17. {
  18. // Define Variables
  19. char choiceLetter;
  20. string escape = "x";
  21. int answer = 0;
  22. double answer_std = 0;
  23. string function;
  24.  
  25. // Math Choice Variables
  26. int calcMinimum();
  27. int calcMaximum();
  28. int calcMean();
  29. int calcStdDev();
  30.  
  31. // Prompts User with Letter Choices
  32. cout << "Welcome to a math program!\n"
  33. << "Please Choose a letter:\n\n"
  34. << "L -- Minimum\n"
  35. << "G -- Maximum\n"
  36. << "M -- Mean\n"
  37. << "D -- Standard Deviation";
  38.  
  39. // Ask User To Choose a Letter Choice
  40. cout << "\n-->";
  41. cin.get(choiceLetter);
  42.  
  43. switch (choiceLetter)
  44. {
  45. case 'L':
  46. case 'l': calcList(); answer = calcMinimum(); cout << "\n The Answer Is: " << answer;
  47. break;
  48.  
  49. case 'G':
  50. case 'g': calcList(); answer = calcMaximum(); cout << "\n The Answer Is: " << answer;
  51. break;
  52.  
  53. case 'M':
  54. case 'm': calcList(); answer = calcMean(); cout << "\n The Answer Is: " << answer;
  55. break;
  56.  
  57. case 'D':
  58. case 'd': calcList(); answer_std = calcStdDev(); cout << "\n The Answer Is: " << answer_std;
  59. break;
  60.  
  61. default:
  62. cout << "Not a valid character or escape character entered"
  63. << "\nPress any key to close program";
  64. cin.ignore();
  65. cin.get();
  66. return 0;
  67. break;
  68. }
  69.  
  70.  
  71.  
  72. // Display Closing Statement to User
  73. cout << "\n\nPress Enter to Close" << endl;
  74. cin.ignore();
  75. cin.get();
  76. return 0;
  77.  
  78.  
  79.  
  80. }
  81.  
  82. // Function Used to Get Numbers into Array
  83. int calcList(void) {
  84.  
  85. std::string inputVal;
  86. int cnt = 0;
  87. int list[100];
  88.  
  89. while (inputVal != "complete") {
  90. std::cout << "Enter an integer: ";
  91. std::getline(std::cin, inputVal);
  92.  
  93. if (inputVal != "complete") {
  94. list[cnt] = (int)inputVal.c_str();
  95. cnt++;
  96. }
  97. }
  98. return(0);
  99. }
  100.  
  101. // Function Used to Calculate Minimum
  102. int calcMinimum() {
  103. int answer = list[0];
  104.  
  105. for (int i=0; i < counter; i++) {
  106. if (list[i] < answer) {
  107. answer = list[i];
  108. }
  109. }
  110. return answer;
  111. }
  112.  
  113. // Function Used to Calculate Maximum
  114. int calcMaximum() {
  115. int answer = list[0];
  116.  
  117. for (int i=0; i < counter; i++) {
  118. if (list[i] > answer) {
  119. answer = list[i];
  120. }
  121. }
  122. return answer;
  123. }
  124.  
  125. // Function Used to Calculate Mean
  126. int calcMean() {
  127. int sum = 0;
  128.  
  129. for (int i=0; i < cnt; i++) {
  130. sum += list[i];
  131. }
  132. int answer = sum / cnt;
  133. return answer;
  134. }
  135.  
  136. // Function Used to Calculate Standard Deviation
  137. int calcStdDev() {
  138. int sum=0;
  139. double deviation=0;
  140.  
  141. for (int i=0; i < counter; i++) {
  142. sum += list[i];
  143. }
  144.  
  145. double answer = sum /counter;
  146.  
  147. for (int i=0; i < counter; i++) {
  148. deviation += ( (list[i]-answer)*(list[i]-answer) );
  149. }
  150.  
  151.  
  152. double deviation_avg = deviation / counter;
  153.  
  154. double std_deviation = sqrt (deviation_avg);
  155.  
  156. return std_deviation;
  157. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 34
Reputation: neoseeker191 is an unknown quantity at this point 
Solved Threads: 0
neoseeker191 neoseeker191 is offline Offline
Light Poster

Re: How to exit a loop

 
0
  #8
Jan 19th, 2009
Alright I managed to fix the divide by 0 error, but now it doesnt seem like that code is storing the integers in an array.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: How to exit a loop

 
0
  #9
Jan 19th, 2009
The counter and the array were both declared inside the sample code (calcList), they wouldn't be visible outside of it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 34
Reputation: neoseeker191 is an unknown quantity at this point 
Solved Threads: 0
neoseeker191 neoseeker191 is offline Offline
Light Poster

Re: How to exit a loop

 
0
  #10
Jan 19th, 2009
Ah I see, that makes sense. So I defined both variables as Global, and now I am getting an answer, but the answer is wrong and an extremely high number. Also, everytime I run it using the same integers I get a different number as the answer.
Last edited by neoseeker191; Jan 19th, 2009 at 10:37 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC