c++ not working

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
Here is the instructions Write a c++ program that takes six test scores as input and stores them in an array. It 
drops the lowest score, and computes the average of the remaining five scores. It outputs 
the average score
and here is what I got. I have the lowtest score wrong somehow but not sure what the problem is.......


#include <iostream>
#include <iomanip>

using namespace std;
float lowest;
float i;
double avg;
float grades;
float Score1;
float Score2;
float Score3;
float Score4;
float Score5;
float testScore;
float lowScore;
int sz=5;
//Function Prototype
void getScore();
void findLowest( int testScore[], int sz );
void calcAverage ( );
void getScore()
{
float Score1, Score2, Score3, Score4, Score5;
cout << "Enter in 5 test scores and I will store ";
cout << "them in variables. ";
cin >> Score1 >> Score2 >> Score3 >> Score4 >> Score5;
cout << fixed << showpoint;
}
void findLowest( float testScore[], int sz )
{
float lowest = testScore[0]; // make the first element of the array the lowest.
/* loop through the array and compare the rest with
 first element.
*/
for ( int i = 0; i < sz; i++ )
{
if ( testScore[i] < lowest )
{
lowest = testScore[i];
}
}
cout << "The lowest is " << lowest << endl;
}
void calcAverage()
{
for ( float i = 0.0; sz < i; i++ )
{
if ( i > lowest )
{
i*= (float (Score1), float (Score2), float (Score3), float (Score4));
avg = (i)/4.0;
}
}
cout << "The Average is " << avg << endl;
}
int main( )
{
cout << "Using Functions ";
cout << "to calculate the average of a series of functions.";
getScore();
// here is my error//;
findLowest()float testScore(i*), int sz );
calcAverage();
cout << "calculate the average of a ";
cout << "series of functions.";
#pragma region wait
cout<<" Press Enter to Quit";
cin.ignore();
cin.get();
#pragma end region
return 0;





it is saying that c:\lab 8 is not recognized as and internal or external command
Last edited on
Edit your post so it uses code tags - the <> button on the right

We can help you from there.

Also if you have compiler errors, post them in full also.
ok I think I did the edit right..... and the error is c:\lab 8 is not recognized as and internal or external command
@nenakay

Thanks for doing that - it makes it easier. Although I meant you to post the compiler output in full

And it would be better if it was indented properly - are you using an IDE? If so it can do this automatically.

OK, there is no need to have 5 float variables (and 5 more unrelated ones in the function) if you have an array of them, but you haven't declared the array - even though you use it in your functions.

You need to get the concept of using a for loop to get input - what if you had lots of numbers? Are you going to have lots of variables?

Also function calls.

There are lots of conceptual problems - best to do some reading :

http://www.cplusplus.com/doc/tutorial/


Hope all goes well.
I don't even see where c:\lab 8 is included in the code.


here is the line in question:

findLowest()float testScore(i*), int sz );

you copied the function declaration but did not send any proper values.

Also, test score is declared as a global float, and not an array, but you intend to pass it as an array.

there are a lot of global variables which is usually not a good idea. There are a lot of problems here dude.
ok thank you very much :) I rewrote the program....
still haveing problems


#include <iostream>
#include <string>

using namespace std;

void LowestScore (
int *gradeArray,
int &lowestIndex )
{
int scoreIndex;

lowestIndex =1;
scoreIndex =1;
while (1)
{
if (gradeArray[lowestIndex]>gradeArray[scoreIndex])
{
lowestIndex=scoreIndex;
}
else
{
}
scoreIndex =scoreIndex+1;
if (scoreIndex>6) break;
}
}
int main()
{

int Total;
int Score;
int ScoreIndex;
int LowestIndex;
double Average;
int GradeArray[6+1];

ScoreIndex =1;
cout << "Please enter 6 test scores" << endl; while (1)
{
"Please enter ";
cout << << ScoreIndex << " score" << endl;
cin >> Score;
GradeArray[ScoreIndex] = Score;
ScoreIndex =ScoreIndex+1;
if (ScoreIndex>6) break;
}
LowestIndex =1;
LowestScore(GradeArray,LowestIndex);
ScoreIndex =1;
Total =0;
while (1)
{
if (ScoreIndex==LowestIndex)
{
;
}
else
{
Total =Total+GradeArray[ScoreIndex];
}
ScoreIndex =ScoreIndex+1;
if (ScoreIndex>6) break;
}
Average =(Total+0.0)/5;
cout << "Your test average: " << Average << endl;
return 0;
}
Last edited on
Always use the code tags - every time - without fail

Are you having problems - tell us what they are. You are expecting us to find your problems and fix them.

Does it compile? If not post the compiler output (the errors) in full.

Some basic problems i see:

Don't use infinite loops.

Don't put multiple statements on one line.



I need to change the string raptor_prompt_variable_zzyz; to another variable but I am not sure what I am suppost to change it to can anyone help me figure out what variable I need to change it to



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
#include <iostream>
#include <string>

using namespace std;

 void LowestScore (
       int  *gradeArray,
       int  &lowestIndex )
{
   int scoreIndex;

   lowestIndex =1;
   scoreIndex =1;
   while (1)
   {
      if (gradeArray[lowestIndex]>gradeArray[scoreIndex])
      {
         lowestIndex=scoreIndex;
      }
      else
      {
      }
      scoreIndex =scoreIndex+1;
      if (scoreIndex>6) break;
   }
}
int main()
{
   string raptor_prompt_variable_zzyz;
   int Total;
   int Score;
   int ScoreIndex;
   int LowestIndex;
   double Average;
   int GradeArray[6+1];

   ScoreIndex =1;
   cout << "Please enter 6 test scores" << endl;   while (1)
   {
      raptor_prompt_variable_zzyz ="Please enter ";
      cout << raptor_prompt_variable_zzyz << ScoreIndex << " score" << endl;
      cin >> Score;
      GradeArray[ScoreIndex] = Score;
      ScoreIndex =ScoreIndex+1;
      if (ScoreIndex>6) break;
   }
   LowestIndex =1;
   LowestScore(GradeArray,LowestIndex);
   ScoreIndex =1;
   Total =0;
   while (1)
   {
      if (ScoreIndex==LowestIndex)
      {
	;
      }
      else
      {
         Total =Total+GradeArray[ScoreIndex];
      }
      ScoreIndex =ScoreIndex+1;
      if (ScoreIndex>6) break;
   }
   Average =(Total+0.0)/5;
   cout << "Your test average: " << Average << endl;
   return 0;
}


Last edited on
Line 22, you have a close brace immediately following an open brace.

Line 35, why are you declaring an array as [6+1]? Why not simply declare it as [7]?

And ya, using 2 infinite while loops is kind of a hack, those loops could be redone more efficiently as for loops.

Lines 54-56 Not sure what you are trying to do here at all, this if statement is pointless with no code between the braces.
ok thank you very much... :)
How do I fix the loops to not be a hack? not to be infinite loops?


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
#include <iostream>
#include <string>

using namespace std;

 void LowestScore (int  *gradeArray,int  &lowestIndex )
{
   int scoreIndex;

   lowestIndex =1;
   scoreIndex =1;
   while (1)
   {
      if (gradeArray[lowestIndex]>gradeArray[scoreIndex])
      {
         lowestIndex=scoreIndex;
      }
      else
     
      scoreIndex =scoreIndex+1;
      if (scoreIndex>6) break;
   }
}
int main()
{
   string score;
   int Total;
   int Score;
   int ScoreIndex;
   int LowestIndex;
   double Average;
   int GradeArray[7];

   ScoreIndex =1;
   cout << "Please enter 6 test scores" << endl;   
   while (1)
   {
      score ="Please enter ";
      cout << score << ScoreIndex << " score" << endl;
      cin >> Score;
      GradeArray[ScoreIndex] = Score;
      ScoreIndex =ScoreIndex+1;
      if (ScoreIndex>6) break;
   }
   LowestIndex =1;
   LowestScore(GradeArray,LowestIndex);
   ScoreIndex =1;
   Total =0;
   while (1)
   {
      if (ScoreIndex=LowestIndex)
   
      else
      {
         Total =Total+GradeArray[ScoreIndex];
      }
      ScoreIndex =ScoreIndex+1;
      if (ScoreIndex>6) break;
   }
   Average =(Total+0.0)/5;
   cout << "Your test average: " << Average << endl;
   return 0;
}
Your code:
while (1)

if (ScoreIndex>6) break;

One way of doing it - not the best in this situation:

1
2
3
4
while (ScoreIndex<6) {
//your code here
}


If the size of the array is known, you could use a for loop instead - this is the normal way of processing arrays:

1
2
3
4
5
6
7
8
9
10
11
const unsigned short SIZE = 6;

int GradeArray[SIZE];

unsigned short Count;
for (Count=0;Count<SIZE;Count++) {

//your code here
}



Even better, you can use a vector, then you don't have to worry about specifying the size, just use push_front (keeps the input order) to insert new values. You can still refer to individual elements using the at function. Read about it:

http://www.cplusplus.com/reference/stl/vector/


Also, it is usual to declare functions before main, then provide the definitions of them after main.

HTH
Last edited on
ok this is what I got so far
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string score;
   int Total;
   int Score;
   int ScoreIndex;
   int LowestIndex;
   double Average;
   int GradeArray[6];
   for (Count=0;Count<SIZE;Count++)

 void LowestScore (int  *gradeArray,int  &lowestIndex )
{
   int scoreIndex;

   lowestIndex =1;
   scoreIndex =1;
   while (scoreIndex>6)
   {
      if (gradeArray[lowestIndex]>gradeArray[scoreIndex])
      {
         lowestIndex=scoreIndex;
      }
      else
     
      scoreIndex =7;
      if (scoreIndex>6) break;
   }
}

   ScoreIndex =7;
   cout << "Please enter 6 test scores" << endl;   
   while (1)
   {
      score ="Please enter ";
      cout << score << ScoreIndex << " score" << endl;
      cin >> Score;
      GradeArray[ScoreIndex] = Score;
      ScoreIndex =ScoreIndex+1;
      if (ScoreIndex>6) break;
   }
   LowestIndex =1;
   LowestScore(GradeArray,LowestIndex);
   ScoreIndex =7;
   Total =0;
   while (ScoreIndex>6)
   {
      if (ScoreIndex=LowestIndex)
   
      else
      {
         Total =Total+GradeArray[ScoreIndex];
      }
      ScoreIndex =ScoreIndex+1;
      if (ScoreIndex>6) break;
   }
   Average =(Total+0.0)/5;
   cout << "Your test average: " << Average << endl;
   return 0;
}
@nenakay

But that doesn't compile does it? You cannot declare functions inside functions. Did you understand what I said about declaring functions before main with definitions after main?

There a quite a few concepts you are not getting at the moment.

Did you understand what I said about declaring functions before main with definitions after main?

Did you see what I was saying about using a for loop?

With this:

1
2
scoreIndex =7;
      if (scoreIndex>6) break;


Not making sense, is it?

And this one?
Average =(Total+0.0)/5;

Have a go at fixing these things, and we can look at your code then.

HTH

Topic archived. No new replies allowed.