2 Replies - 115 Views - Last Post: 27 July 2012 - 08:36 PM Rate Topic: -----

#1 robertc  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 29-June 12

help with program to calculate averages

Posted 27 July 2012 - 04:21 PM

hey, i'm having trouble with an assignment. the assignment is to call on a file that has information in it then echo the information and use it to separate brackets and make averages and all that good stuff. my problem is i am getting an unexpected identifier error on my if statement. i am new to c++ and this is my first class so any help would be awesome. here is my code and i have attached the file that i am supposed to call upon



#include <iostream> 
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main () {
    
    
    //initialize variables
    string filepath;
    string name;
    char seen;
    int age;
    int score;
    int total1;
    int count1;
    int total2;
    int count2;
    int total3;
    int count3;
    int total4;
    int count4;
    ifstream infile;
    
    //prompt user for file path
    cout << "please enter the full file path:" << "\n";
    cin >> filepath;
    
    
    
    //opens file
    infile.open (filepath.c_str());
    
        
    //program interprets the file with variables
    while (!infile.eof()){
        infile >> name >> seen >> age >> score;
        
        //echo file
        cout << name  << "    "    << setw(15)  << left <<
                seen  << setw(10)  << left <<
                age   << setw(10)  << left <<
                score << setw(10)  << left <<
                endl;
        
        //if statements to separate the brackets of people
        //next line gives expected identifier error 
        if ( age >= 18 ) && ( seen == Y ){
            total1 += score;
            count1++;
        }else if ( age >= 18 ) && ( seen == N ){
            total2 += score;
            count2++;
        }else if ( age <= 18 ) && ( seen == Y ){
            total3 += score;
            count3++;
        }else if ( age <= 18 ) && ( seen == N ){
            total4 += score;
            count4++;
        }
                        
    }
    
    cout << score << total1 << total2 << total3 << total4 <<
                     count1 << count2 << count3 << count4;
    
    
    
    infile.close();
    
    getchar();
    getchar();
    return 0;
}



Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: help with program to calculate averages

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1915
  • View blog
  • Posts: 5,719
  • Joined: 05-May 12

Re: help with program to calculate averages

Posted 27 July 2012 - 04:30 PM

An if statement looks like:

if ( boolean expression ) statement

Check your parenthesis in your if statements.

This post has been edited by Skydiver: 27 July 2012 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1915
  • View blog
  • Posts: 5,719
  • Joined: 05-May 12

Re: help with program to calculate averages

Posted 27 July 2012 - 08:36 PM

By the way, your comment on line 11 is completely misleading. You aren't initializing any variables, and it will cause havoc with your computations downstream.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1