Manish Gill
Manish Gill

Reputation: 98

double values not displaying with cout

In the program, I have to create a function to fill elements in an array of type double, and then display them using another function show(). Now, when I input a simple numbers, like: 1,2,3,4,5; the show function doesn't display those numbers, but instead, garbage values like: 8.586689e+273.

I can't figure out what is wrong, especially when I have another program open right here, which is printing the numbers out just fine.

Here is the code:

#include<iostream>

int fill(double arr[], int);
void show(const double arr[], int);
void reverse(double arr[], int);
const int size = 5;
using namespace std;

int main()
{
double arr[size];

int limit = fill(arr,size);

show(arr,limit);
reverse(arr,limit);
show(arr,limit);

return 0;
}

int fill(double arr[], int size)
{
cout<<"Enter the values to fill the array: "<<endl;
int i,temp;

    for(i=0;i<size;i++){
        cout<<"Enter entry #"<<i+1<<" : ";
        cin>>temp;
        if(!cin){
                    cin.clear();
                    while(cin.get()!='\n')
                        continue;
                    cout<<"Bad Input.Input Process Terminated.";
                    break;
                }
        else if(temp<0){
            break;
            arr[i]=temp;
        }
    }
return i;
}


void show(const double ar[], int n)
{
using namespace std;
    for(int i=0;i<n;i++){
        cout<<"Entry #"<<(i+1)<<": ";
        cout<<ar[i]<<endl;
    }
}

void reverse(double arr[], int limit)
{
    cout<<"Reversing values..."<<endl;
    double temp;
    for(int i=0;i<limit/2;i++){
        temp = arr[i];
        arr[i]=arr[limit-i-1];
        arr[limit-i-1]=temp;
    }
}

Upvotes: 2

Views: 387

Answers (3)

A. K.
A. K.

Reputation: 38166

This is just an example of careless mistake.

else if(temp<0){
            break;
           //^^^^^ After break
            arr[i]=temp;
           //How can you expect the next line to get executed.
        }

Upvotes: 0

St&#233;phane
St&#233;phane

Reputation: 20340

You not storing the data into your array. So the array contains garbage, and displays garbage.

I'm guessing this code:

    else if(temp<0){
        break;
        arr[i]=temp;
    }

...should instead have been:

    else if(temp<0){
        break;
    }
    arr[i]=temp;

Upvotes: 1

Beno&#238;t
Beno&#238;t

Reputation: 16994

You are not filling the area. Note where you were trying to do so. You put it inside the temp<0 condition, after the break statement. Therefore it is never executed. Since your array is uninitialized (you should use a vector instead), it contains weird values which aren't displayed properly.

int fill(double arr[], int size)
{
cout<<"Enter the values to fill the array: "<<endl;
int i,temp;

    for(i=0;i<size;i++){
        cout<<"Enter entry #"<<i+1<<" : ";
        cin>>temp;
        if(!cin){
                    cin.clear();
                    while(cin.get()!='\n')
                        continue;
                    cout<<"Bad Input.Input Process Terminated.";
                    break;
                }
        else if(temp<0){
            break;
        }
        arr[i]=temp;
    }
return i;
}

Upvotes: 2

Related Questions