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;
}

The structure of the program should look something like:

#include <iostream>
#include <string>

int main() {
  std::string escape = "x";
  std::string choice;

  do {
    // Get input
    std::getline( std::cin, choice );

    // Code here

  } while (choice != escape);
}

Also, part of your code is incorrect.

// Math Choice Variables
int calcMinimum();
int calcMaximum();
int calcMean();
int calcStdDev();

You can't define functions inside a function, so you need to move these prototypes outside main.

Hope this helps.

This works but it doesn't reprompt me to enter a new set of integers. Also thanks for the incorrect code tip.

I would use a bit more structure than that. Put all that crap in main into a different function, like this:

//#includes and whatnot...

int main(/*arg stuff or w/e*/)
{
    while (Menu() != true) {}
    return 0;
}

bool Menu()
{
     //some crap from the old main()
     //this is just here to clarify the choice part
     char cChoice = 0;
     cin >> cChoice;
    //all the other crap in main()
    return (cChoice == 'x') ? true : false;
}

You where using a string datatype to compare to a char. Keep em both chars since string brings in a lot of uneccesary overhead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.