WHILE won't run
Dec 1, 2010 at 3:12am UTC
Professor decided he wanted program to not accept negative numbers and I can't seem to get the WHILE statement to even run. Brain is going gaga.Have used Nums and score but it still gets ignored.
Thanks for looking
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include<iostream>
using namespace std;
void sortAsc(float *, int &);
void getAvg(float *, int &);
void seeArr(float *, int &);
void main()
{
int Nums;
float *score;
cout << "How many test scores will you be entering? " ;
cin >> Nums;
score = new float [Nums];
for (int i = 0; i < Nums; i++)
{
cout << "Test #" << (i + 1) << ": " ;
cin >> *(score + i);
while (Nums < 0)
{
cout << "Invalid Entry! Please use positive numbers " << endl;
}
}
cout << endl;
sortAsc (score, Nums);
cout << "In Ascending Order: " << endl;
seeArr(score, Nums);
cout << endl;
getAvg(score, Nums);
delete [] score;
system("pause" );
}
void sortAsc(float *score, int &Numscore) //Function to sort by ascending
{
bool swap;
float temp;
do
{
swap = false ;
for (int i = 0; i < (Numscore - 1); i++)
{
if (*(score + i) > *(score + (i + 1)))
{
temp = *(score + i);
*(score + i) = *(score + (i + 1));
*(score + (i + 1)) = temp;
swap = true ;
}
}
}
while (swap);
}
void getAvg(float *score, int &Numscore) //Function to find average
{
float total = 0, average;
for (int i = 0; i < Numscore; i++)
total += *(score + i);
average = total / Numscore;
cout << "Average Score: \n" ;
cout << average;
cout << endl;
}
void seeArr(float *score, int &Numscore) //Function to show array
{
for (int i = 0; i < Numscore; i++)
cout << *(score + i) << " " ;
}
Dec 1, 2010 at 4:16am UTC
Try this to replaces lines 17 through 27 in your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
score = new float [Nums];// new code to follow this line
float entry = 0.0;
for (int i = 0; i < Nums; i++)
{
cout << "Test #" << (i + 1) << ": " ;
cin >> entry;
while (entry < 0)// repeat until entry >= 0
{
cout << "Invalid Entry! Please use positive numbers " << endl;
cout << "Test #" << (i + 1) << ": " ;
cin >> entry;
}
*(score + i) = entry;// positive entry recorded
}
Dec 1, 2010 at 4:56am UTC
I see. I was just assuming I had enough code to make it work just by adding a WHILE and because none of the code was calling anything (let alone a function) it just blew right by like whitespace. Maybe a little less caffiene and more reading?
Thanks! It works slick as... well... really good now.
Topic archived. No new replies allowed.