Live Chat & Podcast Sunday at 12:00PM Eastern!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Tag Cloud
acer audio bios boot bsod compaq computer connection crash dell display driver drivers error firefox freeze game hard disk hard drive hardware install internet laptop linksys macro malware network networking outlook outlook 2003 outlook 2007 problem recovery redirect router server slow toshiba trojan usb video virus vista vpn windows windows 7 windows vista windows xp wireless youtube
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
c++ error!

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
eric_sc's Avatar
Computer Specs
Member with 67 posts.
 
Join Date: Sep 2007
Experience: Intermediate
22-Nov-2009, 07:18 PM #1
Unhappy c++ error!
hello, can anyone help me figure out why my code won't execute to completion. i don't know why it crashes every time i get to the end of my function executions!!!

here is my code, and the compiled .exe file is attached!
------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<string.h>
//#include "again.cpp"

using namespace std;

char name[30];

int main()
{
//variables
int num;
int* array;
int average;
int low_score = 0;
int index_low = 0;

//function prototypes
void describe();
void num_scores(int& num);
int allocate_memory(int num, int* array);
void fill_array(int num, int* array);
void selection_sort(int num, int* array);
void find_low(int num, int* array, int& low_score, int& index_low);
void calculation(int num, int* array, int& average);
void print2(int num, int* array, int average);

//function calls
describe();

cout<<"Enter the student's name: ";
cin.getline(name,30);

num_scores(num);
allocate_memory(num, array);
fill_array(num, array);
find_low(num, array, low_score, index_low);
selection_sort(num, array);
calculation(num, array, average);
print2(num, array, average);

delete[] array;
return 0;
}


void describe()
{
cout<<endl;
cout<<" This program gets grade input data from the user, then\n";
cout<<" dynamically allocates an array large enough to hold any\n";
cout<<" number of test scores the user wishes to enter and does\n";
cout<<" an analysis, providing information to the screen and \n";
cout<<" external file scores.txt. This information will be the\n";
cout<<" original test scores in the order in which they were entered,\n";
cout<<" the sorted test scores with an announcement of the lowest\n";
cout<<" score (which is to be dropped in calculation), and the\n";
cout<<" average test score, which will not include the lowest score.\n";
cout<<" An announcement will inform the user of the dropped score.\n\n ";
system("PAUSE");
system("CLS");
}

void num_scores(int& num)
{
cout<<"Enter the number of scores you will be processing: ";
cin>>num;

return;

//a function that gets the number of test scores to be entered from
//the user and then pass this number to another function that will dynamically
//allocate memory for the array of scores
}

int allocate_memory(int num, int* array)
{
array = new int[num];
cout<<"Memory has been allocated for "<<num<<" grades to be entered.\n\n";
cout<<endl<<endl;

//dynamically allocate memory for the array of scores passed from get_scores()
}

void fill_array(int num, int* array)
{
void print1(int num, int* array);
int grade;

cout<<endl;
for(int i=0; i<num; i++)
{
cout<<"Enter grade "<<i+1<<": ";
cin>>grade;

array[i] = grade;
}

print1(num, array);
return;


//fills this array with scores that are entered by the user
}

void find_low(int num, int* array, int& low_score, int& index_low)
{
int low = array[0];
for(int i=0; i<num; i++)
{
if(array[i] <= low)
{
low = array[i];
low_score = array[i];
index_low = i;
}
}
cout<<endl<<endl;
cout<<"The lowest score is test #"<<index_low+1<<", ";
cout<<"with a score of "<<low_score<<"%.\n\n";
system("PAUSE");
return;

//search through array and determine the subscript of the lowest score
}

void selection_sort(int num, int* array)
{
system("CLS");
cout<<endl;
cout<<"Grades are being sorted...\n\n";
system("PAUSE");

int p;
int minPosition;
int minValue;

for(p = 0; p<(num-1); p++)
{
minPosition = p;
minValue = array[p];

for(int i = p+1;i<num;i++)
{
if(array[i]<minValue)
{
minValue = array[i];
minPosition = i;
}

}
array[minPosition] = array[p];
array[p] = minValue;
}
return;
//sort the array from lowest to highest
}

void calculation(int num, int* array, int& average)
{
int counter = 1;
cout<<endl<<endl;
cout<<"Scores sorted. Lowest Dropped. Remaining results:";
cout<<endl;
for(int i=1; i<num; i++)
{
cout<<counter<<". ";
cout<<setw(3)<<array[i]<<"%"<<endl;
counter++;
}

double sum = 0;
double quotient = 0;

for(int j=1; j<num; j++)
{
sum += array[j];
}

quotient = (sum/(num-1)+.5);
average = static_cast<int>(quotient);
cout<<endl<<endl;
cout<<"After dropping the lowest score, the student average is ";
cout<<average<<"%.\n\n";
system("PAUSE");

return;

//returns the average score to the nearest integer value through a formal
//argument where the calculation is done dropping the lowest score.
}

void print1(int num, int* array)
{
ofstream outs;
outs.open("scores.txt", ios::app);

outs<<endl<<endl<<"---------------------------------------------\n";
outs<<"Student Name: "<<name<<endl<<endl;
for(int i=0; i<num; i++)
{
outs<<left<<setw(15)<<"Score on Exam #"<<setw(3)<<i+1<<": ";
outs<<setw(3)<<array[i]<<"%"<<endl;
}

outs.close();

system("CLS");
cout<<endl<<"---------------------------------------------\n";
cout<<"Student Name: "<<name<<endl<<endl;
for(int i=0; i<num; i++)
{
cout<<left<<setw(15)<<"Score on Exam #"<<setw(3)<<i+1<<": ";
cout<<setw(3)<<array[i]<<"%"<<endl;
}
return;
//The original test scores in the order in which they were entered
//in tabular form.
}

void print2(int num, int* array, int average)
{
ofstream outs;
outs.open("scores.txt", ios::app);

outs<<endl<<endl;
outs<<"After sorting scores and dropping the lowest, the results are:";
outs<<endl;
for(int i=1; i<num; i++)
{
outs<<left<<setw(15)<<"Score on Exam #"<<setw(3)<<i+1<<": ";
outs<<setw(3)<<array[i]<<"%"<<endl;
}
outs<<endl;
outs<<"The average score for "<<name<<", without counting the low ";
outs<<"score, is "<<average<<"%.\n\n";

outs.close();
return;
//The sorted test scores, with the lowest score dropped (listed in tabular
//form). The average test score, with the lowest score dropped, rounded off
//to the nearest integer value, along with an announcement of the lowest
//score that was dropped.
}
Attached Files
File Type: zip ews_project4.zip (2.1 KB, 2 views)
Closed Thread Bookmark and Share   techguy.org/879595

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 11:16 PM.
Copyright © 1996 - 2010 TechGuy, Inc. All rights reserved.
Powered by Cermak Technologies, Inc.