12 Replies - 2614 Views - Last Post: 08 July 2011 - 01:26 PM Rate Topic: -----

#1 elthain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-July 11

Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 07:08 PM

Hey guys.

I am trying to code a program that sorts weather data. It is as written:

//Purpose: Stores and Displays the Monthly, Total, Average, Highest, and Lowest Amounts of Rainfall Stored in an Array for Twelve Months
//Date: 07/07/2010        
//Filename: Intermediate23.cpp
//Input: Amount of Rainfall per Month
//Output:  Monthly, Total, Average, Highest, and Lowest Amounts of Rainfall

#include <iostream>
using namespace std;

//function prototypes
void displayMonthly(double rain[], int numElements);
void displayTotal(double rain[], int numElements);
void displayAverage(double rain[], int numElements);
void displayHighest(int rain[], int numElements);
void displayLowest(int rain[], int numElements);

int main()
{
	//declare array and variables
	double rainfall[] = {0.0};
	int rainAmount[] = {0};
	int choice = 0;


	//get rainfall amounts
	for (int x = 0; x < 12; x += 1)
	{
		cout  << "Enter rainfall for month "
		<< x + 1 << ": ";
		cin >> rainfall[x];
	}   //end for

	do
	{
		//display menu and get choice
		cout << endl;
		cout << "1 Display monthly amounts" << endl;
		cout << "2 Display total amount" << endl;
		cout << "3 Display average amount" << endl;
		cout << "4 Display highest amount" << endl;
		cout << "5 Display lowest amount" << endl;
		cout << "6 End program" << endl;
		cin  >> choice;

		//call for appropriate function or end program
		if (choice == 1)
			displayMonthly(rainfall, 12);
		else
			if (choice == 2)
				displayTotal(rainfall, 12);
			else
				if (choice == 3)
					displayAverage(rainfall, 12);
				else
					if (choice == 4)
						displayHighest(rainAmount, 12);
					else
						if (choice == 5)
							displayLowest(rainAmount, 12);
		  //end if
		     //end if
		       //end if
		         //end if
		           //end if
	   
	} while (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5);

	system("pause");
		return 0;
} //end of main function

//***function definitions***
void displayMonthly(double rain[], int numElements)
{
	cout << "Monthly rainfall amounts: " << endl;
	for (int x = 0; x < numElements; x += 1)
		cout << rain[x] << endl;
	//end for
} //end of displayMonthly function

void displayTotal(double rain[], int numElements)
{ 
	double total = 0.0;
	for (int x = 0; x < numElements; x += 1)
		total = total + rain[x];
	//end for
	cout << "Total rainfall: " << total << endl;

} //end of displayTotal function

void displayAverage(double rain[], int numElements)
{
	double total = 0.0;
	double average = 0.0;
	for (int x = 0; x < numElements; x += 1)
		total = total + rain[x];
	//end for
	//calculate average using the total amount
	average = total / 12;
	cout << "Average rainfall: " << average << endl;
} // end of displayAverage function

void displayHighest(int rain[], int numElements)
{
	//assign first element's value to the high variable
	int high = rain[0];

	//begin the search with the second element
	int x = 1;
	//search for the highest number
	while (x < numElements)
	{
		if (rain[x] > high)
			high = rain[x];
		//end if
		x += 1;
	} //end while

	return high;
	cout << "Highest amount: " << high << endl;
	
}//end of displayHighest function

void displayLowest(int rain[], int numElements)
{
	//assign the first element's value to the low variable
	int low = rain[0];
	//begin the search with the second element
	int x = 1;
	
	//search for the lowest number
	while (x < numElements)
	{
		if (rain[x] < low)
			low = rain[x];
		    //end if
		    x += 1;
	} // end while

	return low;
	cout << "Lowest amount: " << low << endl;
	
}//end of displayLowest function



Now what works is the first three void conditions, but the display highest and lowest only keeps on giving me the prompt back when I build the solution. Furthermore, the prompt keeps on returning regardless of what I do.

Is This A Good Question/Topic? 0
  • +

Replies To: Not getting output for highest and lowest numbers in c++ code. Help?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,493
  • Joined: 08-August 08

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 07:43 PM

You return before outputting, so the last line is never executed.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 5,411
  • Joined: 27-December 05

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 07:50 PM

When I tried to build your program my compiler gave the following error messages
test.cc: In function ‘int main()’:
test.cc: In function ‘void displayHighest(int*, int)’:
test.cc:119: error: return-statement with a value, in function returning 'void'
test.cc: In function ‘void displayLowest(int*, int)’:
test.cc:140: error: return-statement with a value, in function returning 'void'


i.e., it is not compilable, so I wonder if you are actually running the code that you have posted here.

How to fix those errors should be obvious -- post again if you can't figure that out. Notice that even if you give those functions return types, the print statements on lines 120 and 141 will never be executed because they appear after the functions return.
Was This Post Helpful? 0
  • +
  • -

#4 elthain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-July 11

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 08:13 PM

Well, should the cout that I put for the highest and lowest come before "return?"

Hold on, lemme try what you suggested.

I ran the code, but it did not work, so that is why I came here.
Was This Post Helpful? 0
  • +
  • -

#5 elthain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-July 11

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 08:41 PM

All right,

so my new code goes like this:

//Purpose: Stores and Displays the Monthly, Total, Average, Highest, and Lowest Amounts of Rainfall Stored in an Array for Twelve Months
//Author: Oscar Benitez	
//Date: 07/07/2010        
//Filename: Intermediate23.cpp
//Input: Amount of Rainfall per Month
//Output:  Monthly, Total, Average, Highest, and Lowest Amounts of Rainfall

#include <iostream>
using namespace std;

//function prototypes
void displayMonthly(double rain[], int numElements);
void displayTotal(double rain[], int numElements);
void displayAverage(double rain[], int numElements);
void displayHighest(int rain[], int numElements);
void displayLowest(int rain[], int numElements);

int main()
{
	//declare array and variables
	double rainfall[] = {0.0};
	int rainAmount[] = {0};
	int choice = 0;


	//get rainfall amounts
	for (int x = 0; x < 12; x += 1)
	{
		cout  << "Enter rainfall for month "
		<< x + 1 << ": ";
		cin >> rainfall[x];
	}   //end for

	do
	{
		//display menu and get choice
		cout << endl;
		cout << "1 Display monthly amounts" << endl;
		cout << "2 Display total amount" << endl;
		cout << "3 Display average amount" << endl;
		cout << "4 Display highest amount" << endl;
		cout << "5 Display lowest amount" << endl;
		cout << "6 End program" << endl;
		cin  >> choice;

		//call for appropriate function or end program
		if (choice == 1)
			displayMonthly(rainfall, 12);
		else
			if (choice == 2)
				displayTotal(rainfall, 12);
			else
				if (choice == 3)
					displayAverage(rainfall, 12);
				else
					if (choice == 4)
						displayHighest(rainAmount, 12);
					else
						if (choice == 5)
							displayLowest(rainAmount, 12);
		  //end if
		     //end if
		       //end if
		         //end if
		           //end if
	   
	} while (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5);

	system("pause");
		return 0;
} //end of main function

//***function definitions***
void displayMonthly(double rain[], int numElements)
{
	cout << "Monthly rainfall amounts: " << endl;
	for (int x = 0; x < numElements; x += 1)
		cout << rain[x] << endl;
	//end for
} //end of displayMonthly function

void displayTotal(double rain[], int numElements)
{ 
	double total = 0.0;
	for (int x = 0; x < numElements; x += 1)
		total = total + rain[x];
	//end for
	cout << "Total rainfall: " << total << endl;

} //end of displayTotal function

void displayAverage(double rain[], int numElements)
{
	double total = 0.0;
	double average = 0.0;
	for (int x = 0; x < numElements; x += 1)
		total = total + rain[x];
	//end for
	//calculate average using the total amount
	average = total / 12;
	cout << "Average rainfall: " << average << endl;
} // end of displayAverage function

void displayHighest(int rain[], int numElements)
{
	//assign first element's value to the high variable
	int high = rain[0];

	//begin the search with the second element
	int x = 1;
	//search for the highest number
	while (x < numElements)
	{
		if (rain[x] > high)
			high = rain[x];
		//end if
		x += 1;
	} //end while

	cout << "Highest amount: " << high << endl;
	return high;
	
	
}//end of displayHighest function

void displayLowest(int rain[], int numElements)
{
	//assign the first element's value to the low variable
	int low = rain[0];
	//begin the search with the second element
	int x = 1;
	
	//search for the lowest number
	while (x < numElements)
	{
		if (rain[x] < low)
			low = rain[x];
		    //end if
		    x += 1;
	} // end while
	
	cout << "Lowest amount: " << low << endl;
	return low;
	
	
}//end of displayLowest function




Now, the problem is that I am running visual studio 2010 and it has no option to rebuild the solution, it only says "build" and only says:
========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

The first code I posted ran normally, so what do I do here. I am new to all this, so any help would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 elthain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-July 11

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 08:52 PM

Now when I run it, this comes up:

1>c:\users\acer\documents\visual studio 2010\Projects\tan solution\Debug\tan project.exe : fatal error LNK1120: 1 unresolved externals
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 08:57 PM

Please post the entire error message. The message should tell you the name of the unresolved external.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 elthain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-July 11

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 09:10 PM

The external problem is gone. Now it is not building. It says:

========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========



and I bring up the black window but it runs the same as before.

I think I have to rebuild it as it is using an old build, but it does not show the option to rebuild at all.

Anyways: hows about this, someone run the following corrected code on their compiler and tell me what it says:

//Purpose: Stores and Displays the Monthly, Total, Average, Highest, and Lowest Amounts of Rainfall Stored in an Array for Twelve Months
//Author: Oscar Benitez	
//Date: 07/07/2010        
//Filename: Intermediate23.cpp
//Input: Amount of Rainfall per Month
//Output:  Monthly, Total, Average, Highest, and Lowest Amounts of Rainfall

#include <iostream>
using namespace std;

//function prototypes
void displayMonthly(double rain[], int numElements);
void displayTotal(double rain[], int numElements);
void displayAverage(double rain[], int numElements);
int displayHighest(int rain[], int numElements);
int displayLowest(int rain[], int numElements);

int main()
{
	//declare array and variables
	double rainfall[] = {0.0};
	int rainAmount[] = {0};
	int choice = 0;


	//get rainfall amounts
	for (int x = 0; x < 12; x += 1)
	{
		cout  << "Enter rainfall for month "
		<< x + 1 << ": ";
		cin >> rainfall[x];
	}   //end for

	do
	{
		//display menu and get choice
		cout << endl;
		cout << "1 Display monthly amounts" << endl;
		cout << "2 Display total amount" << endl;
		cout << "3 Display average amount" << endl;
		cout << "4 Display highest amount" << endl;
		cout << "5 Display lowest amount" << endl;
		cout << "6 End program" << endl;
		cin  >> choice;

		//call for appropriate function or end program
		if (choice == 1)
			displayMonthly(rainfall, 12);
		else
			if (choice == 2)
				displayTotal(rainfall, 12);
			else
				if (choice == 3)
					displayAverage(rainfall, 12);
				else
					if (choice == 4)
						displayHighest(rainAmount, 12);
					else
						if (choice == 5)
							displayLowest(rainAmount, 12);
		  //end if
		     //end if
		       //end if
		         //end if
		           //end if
	   
	} while (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5);

	system("pause");
		return 0;
} //end of main function

//***function definitions***
void displayMonthly(double rain[], int numElements)
{
	cout << "Monthly rainfall amounts: " << endl;
	for (int x = 0; x < numElements; x += 1)
		cout << rain[x] << endl;
	//end for
} //end of displayMonthly function

void displayTotal(double rain[], int numElements)
{ 
	double total = 0.0;
	for (int x = 0; x < numElements; x += 1)
		total = total + rain[x];
	//end for
	cout << "Total rainfall: " << total << endl;

} //end of displayTotal function

void displayAverage(double rain[], int numElements)
{
	double total = 0.0;
	double average = 0.0;
	for (int x = 0; x < numElements; x += 1)
		total = total + rain[x];
	//end for
	//calculate average using the total amount
	average = total / 12;
	cout << "Average rainfall: " << average << endl;
} // end of displayAverage function

int displayHighest(int rain[], int numElements)
{
	//assign first element's value to the high variable
	int high = rain[0];

	//begin the search with the second element
	int x = 1;
	//search for the highest number
	while (x < numElements)
	{
		if (rain[x] > high)
			high = rain[x];
		//end if
		x += 1;
	} //end while
	cout << "Highest amount: " << high << endl;
	return high;
	
	
}//end of displayHighest function

int displayLowest(int rain[], int numElements)
{
	//assign the first element's value to the low variable
	int low = rain[0];
	//begin the search with the second element
	int x = 1;
	
	//search for the lowest number
	while (x < numElements)
	{
		if (rain[x] < low)
			low = rain[x];
		    //end if
		    x += 1;
	} // end while
	cout << "Lowest amount: " << low << endl;
	return low;
	
	
}//end of displayLowest function



This post has been edited by elthain: 07 July 2011 - 09:16 PM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 09:29 PM

You are trying to access your arrays out of bounds. In the following:
	double rainfall[] = {0.0};
	int rainAmount[] = {0};
	int choice = 0;


	//get rainfall amounts
	for (int x = 0; x < 12; x += 1)
	{
		cout  << "Enter rainfall for month "
		<< x + 1 << ": ";
		cin >> rainfall[x];
	}   //end for

You have declared an array rainfall that has 1 element, yet in your loop you are trying to access 12 elements.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 29-May 11

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 07 July 2011 - 11:47 PM

int displayHighest(int rain[], int numElements);
int displayLowest(int rain[], int numElements);



int displayHighest(int rain[], int numElements)
{
        //assign first element's value to the high variable
        int high = rain[0];

        //begin the search with the second element
        int x = 1;
        //search for the highest number
        while (x < numElements)
        {
                if (rain[x] > high)
                        high = rain[x];
                //end if
                x += 1;
        } //end while

        return high;
        cout << "Highest amount: " << high << endl;

}//end of displayHighest function

int displayLowest(int rain[], int numElements)
{
        //assign the first element's value to the low variable
        int low = rain[0];
        //begin the search with the second element
        int x = 1;

        //search for the lowest number
        while (x < numElements)
        {
                if (rain[x] < low)
                        low = rain[x];
                    //end if
                    x += 1;
        } // end while

        return low;
        cout << "Lowest amount: " << low << endl;

}//end of displayLowest function




These are the changes in your code, it works fine
Was This Post Helpful? 0
  • +
  • -

#11 elthain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-July 11

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 08 July 2011 - 12:11 PM

Yes, it does compile now, and does display for a high and low numbers, but now it only gives me the same number (1072693248) for the highest, and zero for the lowest, no matter what I put in. Is there something wrong with the function definitions? The prototypes are defined in my earlier postings.

int displayHighest(int rain[], int numElements)
{
	
    //assign first element's value to the high variable
	int high = rain[0];
	
	//begin the search with the second element
	int x = 1;
	
	//search for the highest number
	while (x < numElements)
	{
		if (rain[x] > high)
			high = rain[x];
		//end if
		x += 1;
	} //end while
    
	
	cout << "Highest amount: " << high << endl;
	return high;
	
	
	
	
}//end of displayHighest function

int displayLowest(int rain[], int numElements)
{
	//assign the first element's value to the low variable
	int low = rain[0];
	//begin the search with the second element
	int x = 1;
	
	//search for the lowest number
	while (x > numElements)
	{
		if (rain[x] < low)
			low = rain[x];
		    //end if
		    x += 1;
	} // end while
	
	cout << "Lowest amount: " << low << endl;
    return low;
	
	
	
	
}//end of displayLowest function


Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 08 July 2011 - 12:21 PM

Please post the complete program.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Not getting output for highest and lowest numbers in c++ code. Help?

Posted 08 July 2011 - 01:26 PM

View Postelthain, on 08 July 2011 - 08:11 PM, said:

	int x = 1;
	
	//search for the lowest number
	while (x > numElements)

This doesn't look right - your loop will only start when numElements is less than 1.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1