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.

// 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.

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

or something.

commented: Nice, simple, code. +1

Do I still have a counter in the function you gave me?

Yep, "cnt" is the counter.

for some reason I am getting a divide by zero error. it doesnt seem that the counter is counting up.

Is this how you are running the code? (I have added a line that prints out the value of the counter.)

#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();
}

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.

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

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.

The counter and the array were both declared inside the sample code (calcList), they wouldn't be visible outside of it.

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.

Add some debug...

Does list[cnt] = (int)inputVal.c_str(); get you the numeric value you entered, or the address of the string?

Sorry this is the first time I've ever programmed in C++ or used Debug. I ran the program through one cycle, counter set at 0. But it gives my list[cnt] a value of 16146904. Where does this number come from, I can't find any reason it'd be there.

EDIT:: I've ran 5 times and the number never changes its always 16146904. It seems that it is not setting my integer to list[cnt] ?

Add some debug...

Does list[cnt] = (int)inputVal.c_str(); get you the numeric value you entered, or the address of the string?

Well i dont think the int cast would do the trick. You can do two things for this

1

list[cnt]=atoi(inputVal.c_str();

or

2

istringstream s(inputVal);

s>>list[cnt];

I guess these 2 are standard conversions.

(I'm just slow I guess...but here it is anyway)

You can't just cast a character pointer (that is what inputVal.c_str() returns) to an int and expect to get the integer value represented by the string characters.

Sample code and output:

string foo = "124";
	cout << "foo=" << foo << endl;
	cout << "(int)foo.c_str()=" << (int)foo.c_str() << endl;
	cout << "atoi(foo.c_str())=" << atoi(foo.c_str()) << endl;
foo=124
(int)foo.c_str()=1963856
atoi(foo.c_str())=124

I chose to use

list[cnt]=atoi(inputVal.c_str();list[cnt]=atoi(inputVal.c_str();

and its holding the right values!

but one more thing needs to be fixed still, for some reason it automatically sets list[0] to 0. I can see it in the output, it shows:

"Enter an integer: 0 Enter an integer:"

Heres the code I have so far

int  calcList() {
		 
		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);
}

:$ yup he's right.... std::string is a class, and .c_str() would convert it to a c style string... then the cast would make it the ascii value of the number, not the actual number. Sorry about that.

That first '0' you're seeing almost looks like unresolved input.

Your menu selection only reads a character from cin, if you have to hit a return before the menu option is selected, the return may still be in the input stream when you get to calcList. Then the first read gets an empty string. If this might be the problem, you could either make sure to clear the input stream before or at the start of calcList() or you could read a line like you have in calcList() when getting the menu selection.

try adding

cin.ignore();

In the top of your calclist function.
That would work i guess.If Mutan is right :)

That worked great! Thank you everyone very much.

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.