ok i'm trying program have let user enter students names followed test score. i'm trying set i'll have array of names , array of grades. i'm having trouble adding names array. tried call cin.getline() inside loop , assign each subscript in array. however, fails miserably. can point me in right direction?
#include<iostream> #include<iomanip> #include<string> using namespace std; //function prototypes void sort(double*, int); double average(double*, int); void drop(double*, int); int main() { char ch; char name[30]; int numtestscores; string *studentnames; double *testscoreptr; double testaverage; //get number of test scores. cout << "\nhow many test scores enter? "; cin >> numtestscores; //validate input. /*while (numtestscores < 0) { cout << "the number cannot negative.\n"; cout << "enter number: "; cin >> numtestscores; }*/ // allocate array hold test scores studentnames = new string[numtestscores]; testscoreptr = new double[numtestscores]; //fill array test scores. for(int = 0; < numtestscores; i++) { cout << "enter name , test score test # " << (i+1) << " : "<< endl; cin.getline(name,30); studentnames[i] = name; cout << studentnames[i] <<endl; //i tried use see if names going array cin.get(); } //get test score. //cout << "enter test score " //<< (i + 1) << ": "; //cin >> testscoreptr[i]; //validate input. /*while (numtestscores < 0) { cout << "negative scores not allowed.\n"; cout << "enter score test: "; cin >> testscoreptr[i];*/ // sort test scores. sort(testscoreptr, numtestscores); //display resulting data. cout << "\nthe test scores in ascending " << "order, , average, are:\n\n"; cout << "score" <<endl; cout << "----" <<endl; (int j = 0; j < numtestscores; j++) { cout << "\n" << fixed << setprecision(2) << setw(6) << testscoreptr[j]; } //drop lowest grade drop(testscoreptr, numtestscores); // average of test scores. testaverage = average(testscoreptr, numtestscores); cout << "\n\naverage score: " << setprecision(2) << fixed << testaverage <<endl <<endl; // free dynamically-allocated memory. delete [] testscoreptr; testscoreptr = 0; return 0; } //**************************************** //function sort //this function performs selection sort //on array pointed score //parameter ascending order. size //parameter holds number of elements. //***************************************** void sort(double* score, int size) { int startscan, minindex; double minvalue; (startscan = 0; startscan < (size - 1); startscan++) { minindex = startscan; minvalue = score[startscan]; for(int index = startscan + 1; index < size; index++) { if (score[index] < minvalue) { minvalue = score[index]; minindex = index; } } score[minindex] = score[startscan]; score[startscan] = minvalue; } } void drop(double* score, int size) { score[0] = 0; } //************************************************* //function average //this function calculates , returns //average of values stored in array //passed scores parameter. //parameter numscors holds number of elements in array double average(double* score, int numscores) { double total = 0; //accumulator numscores--; //calculate total of scores. (int k = 0; k <= numscores ; k++) { total += score[k]; } //return average score. return (total/numscores); }
you not taking account newline characters.
this line:
cin >> numtestscores;
reads number input, not newline character. if typed 8<enter> read 8 newline not read input.
first time go loop , this:
cin.getline(name,30);
this reads new line character typed after 8 (and nothing else).
couple of other gotchas...
1) forgot read , throw away newline.
cin >> numtestscores;
solutions:
// 1: read line string read value string. std::string testscoresstring; std::getline(std::cin, testscoresstring); std::stringstream testscorestream(testscorestring); testscorestream >> numtestscores // 2: use boost::lexical cast on string stream: std::string testscoresstring; std::getline(std::cin, testscoresstring); testscorestream = boost::lexical_cast<int>(testscorestring); // 3: read number ignore input new line character cin >> numtestscores; cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
2: don't use new/delete in c++ code
can new dynamic objects remembering delete them hard. should using smart pointers or containers control lifespan of dynamic objects.
studentnames = new string[numtestscores]; testscoreptr = new double[numtestscores];
in situation better use std::vector.
std::vector<std::string> studentnames(numtestscores); std::vector<double> testscoreptr(numtestscores);
3: don't use fixed size size array user input:
cin.getline(name,30);
you asking user crash program. typing in realy long name.
use version reads line std::string. string expand required.
std::getline(std::cin, studentnames[i]);
4: don't need endl here. use endl when want buffer flush. use time fine. wanted make sure knew flushed buffer.
cout << studentnames[i] <<endl;
4: not sure for. reading , throwing away first character of next line!!!!!!
cin.get();
5: note works fine. >> operator ignores newline character searches next score.
//get test score. //cout << "enter test score " //<< (i + 1) << ": "; //cin >> testscoreptr[i];
6: predicted above forgot delete 1 of arrays. use vector. place use new/delete writing code c. c++ code should have practically no deletes in (unless writing smart pointer/container).
delete [] testscoreptr;
7: know there std::sort method.
void sort(double* score, int size)
8: can close std::accumulate
double average(double* score, int numscores)
Comments
Post a Comment