• 0

Array Function Help


Question

Hello,

I am writing this code, using arrays to read a list of students name, gpa and student number and sorting them out and then allowing the user to search through it.

Example input file -

Carlo 4861 2.56

Heather 8567 3.43

Kyle 5545 3.99

Would appreciate if someone can go over my code and help me.. thanks in advance -

/*****************************************************************************************************************

Student System

***********************************************************************************************************************/

#include <iostream>

#include <string>

#include <fstream>

#include <iomanip>

using namespace std;

#define SMAX 50

int read_array (string s,float x[],float y[],int n);

void sort_array(float x[],float y[],int n);

string search_array(

/**********************************************

Main Function

***********************************************/

void main()

{int i,n;

float x[sMAX],y[sMAX];

string s;

// Get User Input

cout << "Enter Student Name :" ;

cin >> s;

// Entering Value into Array

n = read_array(s,x,y,SMAX);

// Sort Function

sort_array(x,y,n);

// Output

for (i=0li<n;i++)

{cout << x,y;

};

}

/**************************************************************

read_array()

Reading data from input file into array

*******************************************************************************************************/

int read_array(string s,float x[], float y[],int n)

{int i;

fstream f;

// open file

f.open('c:\\civ3\student.txt',ios::in);

if (!f.is_open()) return 0;

i = 0;

while (!f.eof())

{ f >> x >> y;

if(f.good()) i++;

};

cout << i;

// close file

f.close();

return i;

}

/***********************************************************************

sorting function

***********************************************************************/

void sort_array(float x[], float y[], int n)

{int i,j;

float temp;

temp = 0;

for (i = 0 ; i < n ; i++)

for (j = 0; j < (n-1) ; j++)

if (x[j] > x [j+1])

{ temp = x[j];

x[j] = x[j+1];

x[j+1] = temp;

temp = y[j];

y[j] = y[j+1];

y[j+1] = temp;

};

}

/*******************************************************************

Searching Array

********************************************************************/

Still have to write for Searching...

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Are you having any specific issues with this code? Also, please use

 tags around your code so that the indentation is kept, it is very hard to read non-indented code.
Link to comment
Share on other sites

  • 0
Are you having any specific issues with this code? Also, please use
 tags around your code so that the indentation is kept, it is very hard to read non-indented code.

Well I am not able to find the problem in this code ? its just giving me errors, i don't know where am i wrong..

Link to comment
Share on other sites

  • 0

Just went over it briefly and tbh it's a mess, you need to pay more attention.

Line 17: looks like you're beginning a function declaration, but it's incomplete:

string search_array(

The missing closing parenthese will cause compilation to fail right there.

Line 47: looks like you mistook a ";" for an "l", won't compile:

for (i=0li&lt;n;i++)

Line 64: a string literal must be enclosed in double quotes, no single quotes:

f.open('c:\\civ3\student.txt',ios::in);

Line 45: you can only output a single value at a time with cout, chain calls to "<<" to output multiple values. This won't compile:

cout &lt;&lt; x[i],y[i];

There are probably more errors, but they are all syntactic in nature so you need to make sure what you write is valid C++. The compiler errors can help you out with this if you read them carefully.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.