I'm still getting one error not sure why?

    Error 1 error C4700: uninitialized local variable 'arranged' used   74

Thanks.

#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
void sortTestScores(int *TestScores, int size_Test);
double avgTestScore(int *TestScores, int size_Test);
void printTestScores(int *TestScores, int size_Test);
int main()
{
//Define variables
int *TestScores;
int size_Test, score;
double average;
//Get the number of test scores you want to average
cout << "How many test scores do you want to enter?";
cin >> size_Test;
//Dynamically allocate an array large enough to hold that many scores
TestScores = new int[size_Test];
//Get test scores
cout << "Enter " << size_Test << " positive test scores below:" << endl;
for (int i=0; i<size_Test; i++)
{
//Display score
cout << "Score " << i + 1 << ": "; 
cin >> score;
// Input validation. Only numbers between 0-100
while (score<0 || score>100)
{
cout << "You must enter a score that is positive" << endl;
cout << "Please enter again: ";
cin >> score;
}
TestScores[i]=score;
}
//Dsiplay the results
cout << "Test Scores: ";
printTestScores(TestScores, size_Test);
sortTestScores(TestScores, size_Test);
cout << "The test scores, sorted in ascending order, are: \n";
printTestScores(TestScores, size_Test);
average = avgTestScore(TestScores, size_Test);
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;
system ("pause");
return 0;
}
//Accepts a dynamic array of test scores and size of array, then sorts in ascending order
//Sort function implementation
void sortTestScores(int *TestScores, int size_Test)
{
int *last=TestScores+size_Test; //get last location of array
for (int *first = TestScores; first < last-1; first++)
{
    for(int *next=first+1; next<last; next++) 
    {
        if (*next<*first)
        {
        int temp=*first;
        *first=*next;
        *next=temp;
        }
    }
}
}
//calculates and returns average of test scores
double avgTestScores(double *TestScores, int size_Test)
{
int total = 0; 
double average;
int *arranged;
for (int i=0; i < size_Test; i++)
{
total+= *arranged;
arranged++;
}
average= double(total) / size_Test;
return average;
}
//Prints test scores stored in array
void printTestScores(int *TestScores, int size_Test)
{
int *arranged=TestScores;
for (int i=0; i < size_Test; i++)
{
cout << *arranged << " " << arranged << endl;
       arranged++; 
}
}

Dani AI

Generated

Brief diagnostic and focused fixes for the three symptoms in this thread (C4700, LNK2019, and the hex garbage in output).

  • Root causes: the average routine used a pointer named arranged before it was initialized (undefined behavior → C4700). The linker error came from a name/signature mismatch between the prototype/call and the function definition (main called avgTestScore while the defined function had a different name/signature). The “random” hex values are pointer addresses being printed (or garbage from reading uninitialized memory) — the print routine streamed the pointer itself instead of only the stored values.

Concrete corrections (apply in order)

  1. Make prototype, call and definition identical (same function name and parameter types). A name or parameter-type mismatch causes unresolved externals at link time.
  2. Initialize any pointer before dereferencing. Inside the average routine either set arranged to point at the array start or avoid the pointer entirely and sum with indexed access (e.g., total += TestScores[i]). Add a guard for size_Test == 0 to avoid division by zero.
  3. Stop printing pointer values in the display routine — print only the dereferenced value or use TestScores[i] so addresses like 004BB260 do not appear.
  4. Release dynamically allocated memory with delete[] when finished, or better, use std::vector<int> to eliminate manual new/delete and most pointer mistakes.

Extra notes

  • Compiler warnings (MSVC /W4 or g++ -Wall -Wextra) and tools like AddressSanitizer quickly reveal uninitialized reads and signature mismatches.
  • correctly identified the uninitialized use; that, plus the name/signature mismatch, fully explains the linker message and the corrupted output seen by . After applying the steps above the output should show only the scores and the correct average.

Recommended Answers

All 5 Replies

The error was clear, and in fact you did use it uninitialized. See line 71 up to 74? You shouldn't use this until you put something in there.

ok, I initialed it but I'm getting a different errors:

Error   2   error LNK1120: 1 unresolved externals       1
Error   1   error LNK2019: unresolved external symbol "double __cdecl avgTestScore(int *,int)" (?avgTestScore@@YANPAHH@Z) referenced in function _main  



#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
void sortTestScores(int *TestScores, int size_Test);
double avgTestScore(int *TestScores, int size_Test);
void printTestScores(int *TestScores, int size_Test);

int main()
{
//Define variables
int *TestScores;
int size_Test, score;
double average;
//Get the number of test scores you want to average
cout << "How many test scores do you want to enter?";
cin >> size_Test;
//Dynamically allocate an array large enough to hold that many scores
TestScores = new int[size_Test];
//Get test scores
cout << "Enter " << size_Test << " positive test scores below:" << endl;
for (int i=0; i<size_Test; i++)
{
//Display score
cout << "Score " << i + 1 << ": "; 
cin >> score;
// Input validation. Only numbers between 0-100
while (score<0 || score>100)
{
cout << "You must enter a score that is positive" << endl;
cout << "Please enter again: ";
cin >> score;
}
TestScores[i]=score;
}
//Dsiplay the results
cout << "Test Scores: ";
printTestScores(TestScores, size_Test);
sortTestScores(TestScores, size_Test);
cout << "The test scores, sorted in ascending order, are: \n";
printTestScores(TestScores, size_Test);
average = avgTestScore(TestScores, size_Test);
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;
system ("pause");
return 0;
}
//Accepts a dynamic array of test scores and size of array, then sorts in ascending order
//Sort function implementation
void sortTestScores(int *TestScores, int size_Test)
{
int *last=TestScores+size_Test; //get last location of array
for (int *first = TestScores; first < last-1; first++)
{
    for(int *next=first+1; next<last; next++) 
    {
        if (*next<*first)
        {
        int temp=*first;
        *first=*next;
        *next=temp;
        }
    }
}
}
//calculates and returns average of test scores
double avgTestScores(int *TestScores, int size_Test)
{
int total = 0; 
double average;
int *arranged=TestScores;
for (int i=0; i < size_Test; i++)
{
total+= *arranged;
arranged++;
}
average= double(total) / size_Test;
return average;
}
//Prints test scores stored in array
void printTestScores(int *TestScores, int size_Test)
{
int *arranged=TestScores;
for (int i=0; i < size_Test; i++)
{
cout << *arranged << " " << arranged << endl;
       arranged++; 
}
}

I got it to output but I'm getting some random stuff after it prints out numbers:

Enter 6 positive test scores below:
Score 1: 89
Score 2: 94
Score 3: 100
Score 4: -78
You must enter a score that is positive
Please enter again: 78
Score 5: 84
Score 6: 65
Test Scores:
89 004BB260
94 004BB264
100 004BB268
78 004BB26C
84 004BB270
65 004BB274
The test scores, sorted in ascending order, are:
65 004BB260
78 004BB264
84 004BB268
89 004BB26C
94 004BB270
100 004BB274
The average of all the test scores is 85.00
Press any key to continue . . .

I didn't check for more of the same uninitialized variable use. You will check for that. Also, learn to print out variables so you can debug your code.

I got it actually, thanks though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.