I have a program that needs to be looped continuously until the user inputs an X. I am not really sure how to go about doing that. Can I make the whole program one giant while() loop?
Here is the code for the program:
// 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 cnt = 0;
std::string inputVal = "";
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() {
cin.ignore();
while (inputVal != "complete") {
std::cout << "Enter an integer: ";
std::getline(std::cin, inputVal);
if (inputVal != "complete") {
list[cnt]=atoi(inputVal.c_str());
cnt++;
cout << list[cnt];
}
}
return(0);
}
// Function Used to Calculate Minimum
int calcMinimum() {
int answer = list[0];
for (int i=0; i < cnt; 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 < cnt; 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 < cnt; i++) {
sum += list[i];
}
double answer = sum /cnt;
for (int i=0; i < cnt; i++) {
deviation += ( (list[i]-answer)*(list[i]-answer) );
}
double deviation_avg = deviation / cnt;
double std_deviation = sqrt (deviation_avg);
return std_deviation;
}