2 Replies - 882 Views - Last Post: 14 December 2009 - 02:51 AM Rate Topic: -----

#1 cpenbutt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-October 09

error with array

Posted 14 December 2009 - 12:26 AM

I just cant get this program to work right. It compiles but doesnt allow me to enter data.




Write a program that searches a 5X1 integer array and finds the maximum value. The program should be set up as follows (notice I donít require any functions be used. You may use them if you wish):
1. Fill the array by reading the 5 values from a data file.
2. Find the maximum value of the array.
3. Display the maximum value and the array (as a 5X1).
Test your program using the following data:
Run 1: 19 Run 2: 16.8
24 13.2
1 20
10 12.7
14 34.9

#include <iostream>
#include <fstream>
using namespace std;


int main()
//variables
{
fstream instream;
char value[5];
float num, large;
int i=0;
float data [10];
//Input
	cout  <<   "Values:\n";
	cout  <<   value[0]; cout  <<   "\t";
	cout  <<   value[1]; cout  <<   "\t";
	cout  <<   value[2]; cout  <<   "\t";
	cout  <<   value[3]; cout  <<   "\t";
	cout  <<   value[4]; cout  <<   "\t";
	cout  <<   value[5]; cout  <<   "\t";
	cout  <<   "\n";

while (i<5)
{
		  instream>>num;																																										
		   data[i]=num;
		  i++;
		  }
		  //End while
		  large = data[0];
		  for(i=1;i<5;i++);
		  if (large<data[i]);
		  large = data [i];
		  cout<<"largest value in file "<<large<<endl;
		  system ("pause");
		  return 0;
		  }
		  //end program



Anybody who can help is a lifesaver!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: error with array

#2 sabriath  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 26
  • Joined: 13-December 09

Re: error with array

Posted 14 December 2009 - 01:10 AM

"instream>>num;"


Try 'cin', although i'm not sure if that's numerical or not.

cin >> num;
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: error with array

Posted 14 December 2009 - 02:51 AM

View Postcpenbutt, on 14 Dec, 2009 - 07:26 AM, said:

int main()
//variables
{
fstream instream;


Are you entering data from a file or from the user?

If its from a file, then you need to provide a filename; e.g.
    std::ifstream myfile("hello.txt");
    if( !myfile.is_open() )
    {
        std::cerr << "Unable to open file" << std::endl;
        exit( EXIT_FAILURE );
    } 



Also be careful; you're overrunning the boundaries of your value array here. its length is 5, so its valid range is [0] to [4]
    cout  <<   value[5]; cout  <<   "\t"; 

This post has been edited by Bench: 14 December 2009 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1