| | |
How to exit a loop
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 34
Reputation:
Solved Threads: 0
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.
Thank You.
Here is what I have so far, I used 72 as the exit number just as a test.
C++ Syntax (Toggle Plain Text)
// Function Used to Get Numbers into Array int calcList() { for (int i=0; i<100; i++) { std::cout << "Enter an integer: "; std::cin >> list[i]; counter = counter + 1; // Exit Loop When 72 is Inputed if (list[i]== 72) { return(0); } } }
Thank You.
Last edited by Ancient Dragon; Jan 19th, 2009 at 9:24 am. Reason: corrected code tags
C++ Syntax (Toggle Plain Text)
int calcList() { std::string inputVal; int cnt = 0; int list[100]; while (inputVal != "complete") { std::cout << "Enter an integer: "; std::getline(std::cin, inputVal); if (inputVal != "complete") { list[cnt] = (int)inputVal.c_str(); cnt++; } } return 0; }
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 1
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.
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 1
Is this how you are running the code? (I have added a line that prints out the value of the counter.)
cpp Syntax (Toggle Plain Text)
#include <iostream> int calcList(void) { std::string inputVal; int cnt = 0; int list[100]; while (inputVal != "complete") { std::cout << "Enter an integer: "; std::getline(std::cin, inputVal); if (inputVal != "complete") { list[cnt] = (int)inputVal.c_str(); cnt++; std::cout << "counter: " << cnt << std::endl; } } return 0; } int main() { calcList(); }
A proud member of R-forum.org - A community that provides help to users of statistics and the R programming language.
•
•
Join Date: Jan 2009
Posts: 34
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
// prog1.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <iostream> #include <string> #include <cmath> using namespace std; // Global Variables int calcList(); int list[100] = {0}; int counter = 0; int cnt = 0; int _tmain(int argc, _TCHAR* argv[]) { // Define Variables char choiceLetter; string escape = "x"; int answer = 0; double answer_std = 0; string function; // Math Choice Variables int calcMinimum(); int calcMaximum(); int calcMean(); int calcStdDev(); // Prompts User with Letter Choices cout << "Welcome to a math program!\n" << "Please Choose a letter:\n\n" << "L -- Minimum\n" << "G -- Maximum\n" << "M -- Mean\n" << "D -- Standard Deviation"; // Ask User To Choose a Letter Choice cout << "\n-->"; cin.get(choiceLetter); switch (choiceLetter) { case 'L': case 'l': calcList(); answer = calcMinimum(); cout << "\n The Answer Is: " << answer; break; case 'G': case 'g': calcList(); answer = calcMaximum(); cout << "\n The Answer Is: " << answer; break; case 'M': case 'm': calcList(); answer = calcMean(); cout << "\n The Answer Is: " << answer; break; case 'D': case 'd': calcList(); answer_std = calcStdDev(); cout << "\n The Answer Is: " << answer_std; break; default: cout << "Not a valid character or escape character entered" << "\nPress any key to close program"; cin.ignore(); cin.get(); return 0; break; } // Display Closing Statement to User cout << "\n\nPress Enter to Close" << endl; cin.ignore(); cin.get(); return 0; } // Function Used to Get Numbers into Array int calcList(void) { std::string inputVal; int cnt = 0; int list[100]; while (inputVal != "complete") { std::cout << "Enter an integer: "; std::getline(std::cin, inputVal); if (inputVal != "complete") { list[cnt] = (int)inputVal.c_str(); cnt++; } } return(0); } // Function Used to Calculate Minimum int calcMinimum() { int answer = list[0]; for (int i=0; i < counter; i++) { if (list[i] < answer) { answer = list[i]; } } return answer; } // Function Used to Calculate Maximum int calcMaximum() { int answer = list[0]; for (int i=0; i < counter; i++) { if (list[i] > answer) { answer = list[i]; } } return answer; } // Function Used to Calculate Mean int calcMean() { int sum = 0; for (int i=0; i < cnt; i++) { sum += list[i]; } int answer = sum / cnt; return answer; } // Function Used to Calculate Standard Deviation int calcStdDev() { int sum=0; double deviation=0; for (int i=0; i < counter; i++) { sum += list[i]; } double answer = sum /counter; for (int i=0; i < counter; i++) { deviation += ( (list[i]-answer)*(list[i]-answer) ); } double deviation_avg = deviation / counter; double std_deviation = sqrt (deviation_avg); return std_deviation; }
![]() |
Similar Threads
- trying to loop my program (C++)
- while loop (Java)
- exiting loop with switch (C++)
- Loop troubles (C++)
- Alright this loop problem is bugging me (Java)
- help me understand end of do while loop (C++)
Other Threads in the C++ Forum
- Previous Thread: Please Help me
- Next Thread: File Handling : Problem Reading end of Line
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






