get function to exit at when user enters negative 1?

hey guys, i cant figure out one thing ive tried if statements, do whiles, while loops and i cant get my getTemp function to exit out when user enters negative 1. any tips?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  /********************************************************************************************
*Project: lowest and highest temperature
*File hw5.cpp
*authors: Damien Carney
*Date: 10/24/2014
*Description: use seperate functions to have a user partially fill an array with temperatures
and find highest and lowest temperature in the array and out put it to console.
*********************************************************************************************/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;

/**********************************************************************************************
*function: main
*description: call functions and output lowest and highest temperature acquired from functions.
**********************************************************************************************/
//prototype
void getTemp(double[], int size);
double getLowest(const double[], int);
double getHighest(const double[],int);

int main ()

{
	double lowestTemp;
	double highestTemp;
	const int size = 100;
	double temperature[size];
	getTemp(temperature, size);
	lowestTemp = getLowest(temperature, size);
	highestTemp = getHighest(temperature, size);
	cout << "the highest & lowest temperatures entered were: "
		  << lowestTemp << " " << highestTemp;
}

/**********************************************************************************************
*function: getTemp
*description: call functions and output lowest and highest temperature acquired from functions.
**********************************************************************************************/

void getTemp(double temp[], int size)
{
	for (int index = 0; index < size; index++)
	{
		if (index != -1)
		{
		cout << "enter temperature #(-1 to exit) " << (index+1) << ": ";
		cin >> temp[index];
		}
	}
}

/**********************************************************************************************
*function: getLowest
*description: retreive lowest temperature that user entered
**********************************************************************************************/

double getLowest(const double array[], int size)
{
	double lowest = array[0];
	for (int count = 1; count < size; count++)
	{
		if (array[count] < lowest)
			lowest = array[count];
	}
	return lowest;
}

/**********************************************************************************************
*function: getHighest
*description: retreive highest temperature that user entered
**********************************************************************************************/

double getHighest(const double array[],int size)
{
	double highest = array[0];
	for (int count = 1; count < size; count++)
	{
		if (array[count] > highest)
			highest = array[count];
	}
	return highest;
}
In the for loop put an else statement that contains break;
*edit* nevermind, Cody had it right.

1
2
3
4
5
6
7
8
9
10
11
12
13
void getTemp(double temp[], int size)
{
	for (int index = 0; index < size; index++)
	{
		if (index != -1)
		{
			cout << "enter temperature #(-1 to exit) " << (index+1) << ": ";
			cin >> temp[index];
		}
		else
			break;
	}
}


You may want to take your input into a temporary variable and test it rather than going directly into temp[index]. otherwise you'll end up with the -1 value being inserted into the array.
Last edited on
closed account (SECMoG1T)
i cant get my getTemp function to exit out when user enters negative 1
What you want is exit when the user enters a -1 temperature not index index will never be negative as you are incrementing it from 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  for (int index = 0; index < size; index++) 	
    { 		
             if (index != -1) 		
               { 		
                     cout << "enter temperature #(-1 to exit) " << (index+1) << ": "; 	
                	cin >> temp[index]; 
         	} 	
   }
   
 /// you can use this
 
 int index=0, value;
 While (index <size)
  {
      cout << "enter temperature #(-1 to exit) " << (index+1) << ": "; 	
      cin >> value;
  
      if (value==-1)
           exit (1);/// include cstdlib or use return
         
         temp [i]=value; ++i; 
   }
   
Last edited on
Why change it to a while loop? Makes more sense to use a for loop.
closed account (SECMoG1T)
both does equally the same thing btw there is also readability. Well if for loop are your best choice you can switch that code into a for loop they are just equal ,

1
2
3
4
5
6
7
8
9
10
11
 int value; 
 for (int index=0; index <size; index++)
   {
       cout << "enter temperature #(-1 to exit) " << (index+1) << ": "; 	
        cin >> value; 
        
        if (value==-1) exit (1);/// include cstdlib or use return 
       ////edit: you dont want to use exit am sure use break as Essclercuffi suggested 
        
         temp [i]=value; 
    }
Last edited on
The main difference between the for's and the while's is a matter of pragmatics: we usually use for when there is a known number of iterations, and use while constructs when the number of iterations in not known in advance.

http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main(){
	
	int i = 0, size = 10, value = 0;

	while (i < size && value != -1){

		std::cin >> value;
		i++;
	}

	return 0;
}
Last edited on
closed account (SECMoG1T)
@mobotus thenk you for pointing that out.
Topic archived. No new replies allowed.