-4
#include <iostream>
using namespace std;

int main()
{
// Declare variable  
    ifstream inFile;
// Declare constant 
    const int MAX = 600;
// Declare an array of strings named name that holds up to MAX 
    string array [name] = MAX;
// Declare an array of whole numbers named grade that holds up to MAX
    double array [grade] = MAX;
// Declare variables quantity, x, avg, and sum (initialized to zero) that hold whole numbers 
    int sum = 0, avg, x, quantity;
// Open input file
    inFile.open("indata3.txt");
// Check if the file was opened
    if (!inFile)
    {
    cout << "File was not found!" << endl;
    return 1;
    }
// Set x to 0 
    x = 0;
// Read name and grade from the file and assign them to name[x] and grade[x] respectively
    cin >> name[x];
    cin >> grade[x];
// While (not-end-of-file) 
    while(inFile)
    {
//Increment x 
    x++;
//  Read a name and a grade from the file and assign them to name[x] and grade[x] respectively 
    cin >> name[x];
    cin >> grade[x];
    }
// Print message 
    cout << "Enter quantity of grades to be processed (0-" << x << "): " << endl;
// Read a value from the keyboard and assign it to quantity 
    cin >> quantity;
// For (x = 0 to quantity-1)
    for (x = 0; x <= quantity-1)
    {
//16. Accumulate grade[x] in sum 

    }
// Assign to avg the value returned by average (sum, quantity)
    avg = sum/quantity;
// Print "Average grade: ", avg 
    cout << "Average grade: " << avg << endl;
// Print "Name", "Grade", "   Comment" 
    cout << "Name" << "," << "Grade" << "," << "   Comment"  << endl;
// For (x = 0 to quantity-1) 
    for (x = 0; x <= quantity-1)
{
// Print name[x], grade[x]
    cout << name[x] << ", " << grade[x] << endl;
// If (grade[x] < avg) 
    if (grade[x] < avg)
    {
//  Print "   below average"
    cout << "   below average" << endl;
    }
// Else if (grade[x] > avg) 
    else if (grade[x] > avg)
    {
//  Print "   above average"
    cout << "   above average" << endl;
    }
// Else
    else()
    {
//  Print "   average"
    cout << "   average" << endl;
    }
}
// Close the file.
    inFile.close();

return 0;
}

Here are some of the errors our of the 20. Most repeat the undeclared identifier. Also not sure where I would need to add more curly brackets to make it syntactically correct. One more thing..how do I accumulate grade[x] into sum? Any help would greatly be appreciated thank you.

error C2065: 'name' : undeclared identifier
error C2075: 'array' : array initialization needs curly braces
error C2065: 'grade' : undeclared identifier
error C2371: 'array' : redefinition; different basic types
error C2440: 'initializing' : cannot convert from 'const int' to 'double [1]'
error C2228: left of '.open' must have class/struct/union1>    type is 'int'
error C2143: syntax error : missing ';' before ')'
error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'
error C2228: left of '.close' must have class/struct/union1>    type is 'int'
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Nora
  • 11
  • 1
  • 9
  • 3
    I think you need to find a beginners book in [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and start from there. – Some programmer dude Dec 08 '15 at 04:44
  • 1
    Use a reference such as [cpp-reference](http://en.cppreference.com/w/) to find which header you need to include for `ifstream`. It might look impossible to navigate at first, but just try it and you'll find it's mainly well structured, plus, it's searchable. And so on. – Cheers and hth. - Alf Dec 08 '15 at 05:04
  • 1
    @Cheersandhth.-Alf, given the ` array [name] = MAX;` lines, I think that the problem is at another place. OP has almost solved the `ifstream` one if they exchange `cin` for `inFile` at the right spots. – Zeta Dec 08 '15 at 05:10

2 Answers2

2

Hopefully this helps a little... (And recommend getting a C++ book and references)

Many of your variables are not declared before use.

Such as 'name' needs to be: string name[];

Lets take a look at this statement:

string array [name] = MAX;

It looks like you're not entirely familiar with C++.

"String" is the datatype.

"array" is the actual name of your array.

"name" is an undeclared variable, and must be an integer to work.

"MAX" is a constant.(You're good there.)

From the way you've written it, I'm guessing you're trying to have parallel arrays of strings and double called "name" and "grade" with 600 elements.

In C++, that would be written:

string name[MAX]; // this creates an empty string array of 600 elements

and grade would be: double grade[MAX]; // this creates an empty double array of 600 elements

This section below from your code won't work for several reasons:

// Open input file inFile.open("indata3.txt"); // Check if the file was opened if (!inFile) { cout << "File was not found!" << endl; return 1; } // Set x to 0 x = 0; // Read name and grade from the file and assign them to name[x] and grade[x] respectively cin >> name[x]; cin >> grade[x]; // While (not-end-of-file) while(inFile) { //Increment x x++; // Read a name and a grade from the file and assign them to name[x] and grade[x] respectively cin >> name[x]; cin >> grade[x]; }

A couple of the reasons are:

  1. while(not-end-of-file) is not real code, you want: while(!inFile.eof())

  2. cin >> name[x]; never accesses the file, and would never assign a value to an element

Good luck! Google is your friend! Let people know you're a newbie so they don't tear you apart in the stackoverflow wilds! Get a good IDE like dev++ or CLion.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
0

array is not a keyword in c++.

If you want to declare array of string that can hold maximum value = MAX then you should try this

string name[MAX];

where "name" is the name of array of a string and MAX is a total number of strings that array can store.

Same for the whole number you should use

double grade[MAX];

where grade is a name of whole number array.

Nutan
  • 778
  • 1
  • 8
  • 18