Array and While Loop

If I remove the While loop, everything works fine. But if I include the While loop, the program will ask for 2 times the number of temperatures asked.

Any ideas on why this is happening?

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
#include <iostream>

using namespace std;

int input(int);

int main()
{
    int num, temp;
    cout << "Input the number of temperatures (not more than 10): ";
    cin >> num;
    while ((num < 0) || (num > 10))
    {
        cout << "Invalid input."<<endl;
        cout << "Input the number of temperatures (not more than 10): ";
        cin >> num;
    }
    input(num);
    return 0;
}

int input(int number)
{

    int temperature[number];
    for (int i = 0; i < number; i++)
    {
        cout << "Input Temperature: ";
        cin >> temperature[i];
    }
    return temperature[number];
}
Seems to work ok for me. If I input 5 it will ask me to input the temperature five times.
If you input 15 the program will say Invalid Output
Then asks you to input another number.
If you input 5 after that, it will ask you to input the temperature 10 times.
1
2
3
4
int input(int number)
{

    int temperature[number];


This is illegal in C++.
What do you mean by illegal?
I input 15 then 5 and it asks me to input the temperature 5 times. I'm not saying you are wrong though, the behaviour you describe could be a result of undefined behaviour (see below).

What do you mean by illegal?
The size of an array has to be a compile time constant (except if you dynamically allocate the array) but some compilers allow it anyway. Your compiler clearly allows it so this is not the cause of your problem.

return temperature[number];
This is a bit of a problem though. This tries to access the element at index number.
Valid indices for the temperature array are 0 to number-1.
This means you are accessing out of bounds, which means the behaviour is undefined meaning anything could happen.
Last edited on
I'm sorry I don't really understand :/
So what are you suggesting me to do?
closed account (E0p9LyTq)
Here is one possible rewrite of your program, using vectors:

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
#include <iostream>
#include <vector>

std::vector<int> input(int);

int main()
{
   int num;

   std::cout << "Input the number of temperatures (not more than 10): ";
   std::cin >> num;

   while ((num < 0) || (num > 10))
   {
      std::cout << "Invalid input.\n";
      std::cout << "Input the number of temperatures (not more than 10): ";
      std::cin >> num;
   }

   std::vector<int> temp_list = input(num);

   std::cout << "\nThe list has the following " << temp_list.size() << " values:\n";

   for (auto it = temp_list.begin() ; it != temp_list.end(); ++it)
   {
      std::cout << *it << " ";
   }
   std::cout << "\n";

   return 0;
}

std::vector<int> input(int number)
{
   std::vector<int> temp_list;
   int temp;

   for (int i = 0; i < number; i++)
   {
      std::cout << "Input Temperature: ";
      std::cin >> temp;

      temp_list.push_back(temp);
   }
   return temp_list;
}
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

void input(int, int*);

int main()
{
    int num = -1;
    
    while ( (num < 0) || (num > 10) )
    {
        cout << "Invalid input."<<endl;
        cout << "Input the number of temperatures (not more than 10): ";
        cin >> num;
    }
    
    int* temp = new int[num];
    
    input( num, temp );

    delete [] temp;
    return 0;
}

void input(int number, int* temperature)
{
    for (int i = 0; i < number; i++)
    {
        cout << "Input Temperature: ";
        cin >> temperature[i];
    }
    return;
}
Last edited on
Firstly, if you run that program, it will immediately say that it has an invalid input, even though the user hasn't done anything. Secondly, while I know the OS usually cleans things like this up, you're not using delete[] on temp. Furthermore, this is a beginner's forums and you've just given an example program that not only doesn't delete heap allocated memory, but uses raw owning pointers, something that there is no need to teach people who are learning basic C++ now we have C++ 11 with vectors and smart pointers (and also you used using namespace std). Why do you keep posting manual memory management examples?
closed account (48T7M4Gy)
Sounds like the Inspector shadowlouse of the c++ police is out today.
Oh come on, I just wanted to know why you do that (there might be a valid reason which I don't know), and to point out to supernoob about having to always use delete with new and delete[] with new[] so they don't end up with memory leak problems.
Supernoob #cire is right it is an error to define a code like you defined in your snippet.
1
2
3
4
int input(int number)
{

    int temperature[number];

instead you wil have to
--> 1)Define a variable in the function to store the number.
I don't understand why you put int num = -1
closed account (48T7M4Gy)
Setting num = -1 serves two purposes - first, to initialise num, and second and more importantly to ensure the while loop is entered. It's a common trick of the trade given the while( x<0 ... test.
But wouldn't it cout "Invalid input." on the first line?
This may be of interest:
http://www.LB-Stuff.com/user-input
closed account (48T7M4Gy)
Yep, it will output "invalid etc".
This leaves you, as the programmer, with all sorts of design problems to solve.
- leave the offending line out?
- adopt a more sophisticated error trapping routine?
- leave it as it is - sort of makes sense?
Topic archived. No new replies allowed.