One More Homework Problem

This is a discussion on One More Homework Problem within the C++ Programming forums, part of the General Programming Boards category; Okay, so I have one more set of code I need some help on. How I got this far without ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    One More Homework Problem

    Okay, so I have one more set of code I need some help on. How I got this far without smashing my skull into a monitor I still have no idea

    I get this error when compiling:

    D:\Jen\School\Jen_Tonon_assignment3.cpp In function `int main()':
    D:\Jen\School\Jen_Tonon_assignment3.cpp no match for 'operator<<' in 'abovebelow1 << std::endl'
    D:\Jen\School\Jen_Tonon_assignment3.cpp no match for 'operator<<' in 'abovebelow2 << std::endl'
    D:\Jen\School\Jen_Tonon_assignment3.cpp no match for 'operator<<' in 'abovebelow3 << std::endl'
    D:\Jen\School\Jen_Tonon_assignment3.cpp no match for 'operator<<' in 'abovebelow4 << std::endl'
    D:\Jen\School\Jen_Tonon_assignment3.cpp no match for 'operator<<' in 'abovebelow5 << std::endl'

    Code:
    // Personal Survey
    // Jennifer Tonon
    // 10-12-05
    // This progam stores information on students, entered by a teacher, and
    // completes a class average.
    
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    using std::string;
    using std::cin;
    using std::getline;
    
       int main()
       
       {
           string student1;
           string student2;
           string student3;
           string student4;
           string student5;
           int attendance1, attendance2, attendance3, attendance4, attendance5;
           int exam1, exam2, exam3, exam4, exam5;
           int final1, final2, final3, final4, final5;
           string abovebelow1;
           string abovebelow2;
           string abovebelow3;
           string abovebelow4;
           string abovebelow5;
           double average;
       
           
           cout << "Enter first student's name: ";
           getline(cin,student1);
           cout << "Enter second student's name: ";
           getline(cin,student2);
           cout << "Enter third student's name: ";
           getline(cin,student3);
           cout << "Enter fourth student's name: ";
           getline(cin,student4);
           cout << "Enter fifth student's name: ";
           getline(cin,student5);
           
           cout << "Enter first student's attendance: ";
           cin >> attendance1;
           cout << "Enter second student's attendance: ";
           cin >> attendance2;
           cout << "Enter third student's attendance: ";
           cin >> attendance3;
           cout << "Enter fourth student's attendance: ";
           cin >> attendance4;
           cout << "Enter fifth student's attendance: ";
           cin >> attendance5;
              
           cout << "Enter first student's exam score: ";
           cin >> exam1;
           cout << "Enter second student's exam score: ";
           cin >> exam2;
           cout << "Enter third student's exam score: ";
           cin >> exam3;
           cout << "Enter fourth student's exam score: ";
           cin >> exam4;
           cout << "Enter fifth student's exam score: ";
           cin >> exam5;
           
           cout << "Enter first student's final score: ";
           cin >> final1;
           if (attendance1 >= 5)
           final1 = final1 + 5;
           cout << "Enter second student's final score: ";
           cin >> final2;
           if (attendance2 >= 5)
           final2 = final2 + 5;
           cout << "Enter third student's final score: ";
           cin >> final3;
           if (attendance3 >= 5)
           final3 = final3 + 5;
           cout << "Enter fourth student's final score: ";
           cin >> final4;
           if (attendance4 >= 5)
           final4 = final4 + 5;
           cout << "Enter fifth student's final score: ";
           cin >> final5;
           if (attendance5 >= 5)
           final5 = final5 + 5;
           
           average = (final1 + final2 + final3 + final4 + final5) / 5;
           
           if (final1 >= average)
           abovebelow1 = "Above Average";
           else
           abovebelow1 = "Below Average";
           if (final2 >= average)
           abovebelow2 = "Above Average";
           else
           abovebelow2 = "Below Average";
           if (final3 >= average)
           abovebelow3 = "Above Average";
           else
           abovebelow3 = "Below Average";
           if (final4 >= average)
           abovebelow4 = "Above Average";
           else
           abovebelow4 = "Below Average";
           if (final5 >= average)
           abovebelow5 = "Above Average";
           else
           abovebelow5 = "Below Average";
           
           cout << "Student Name  Attendance  Exam Score  Final Score  Above/Below Average ";
           cout << student1, attendance1, exam1, final1, abovebelow1 << endl;
           cout << student2, attendance2, exam2, final2, abovebelow2 << endl;
           cout << student3, attendance3, exam3, final3, abovebelow3 << endl;
           cout << student4, attendance4, exam4, final4, abovebelow4 << endl;
           cout << student5, attendance5, exam5, final5, abovebelow5 << endl;
           
             
    	  system("PAUSE");
          return 0;    
              
              
       }
    I know it's something stupidly simple that I'm missing, but any help would rock!

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Change this

    Code:
           cout << "Student Name  Attendance  Exam Score  Final Score  Above/Below Average ";
           cout << student1, attendance1, exam1, final1, abovebelow1 << endl;
           cout << student2, attendance2, exam2, final2, abovebelow2 << endl;
           cout << student3, attendance3, exam3, final3, abovebelow3 << endl;
           cout << student4, attendance4, exam4, final4, abovebelow4 << endl;
           cout << student5, attendance5, exam5, final5, abovebelow5 << endl;
    to this

    Code:
           cout << "Student Name  Attendance  Exam Score  Final Score  Above/Below Average ";
           cout << student1 << attendance1 << exam1 << final1 << abovebelow1 << endl;
           cout << student2 << attendance2 << exam2 << final2 << abovebelow2 << endl;
           cout << student3 << attendance3 << exam3 << final3 << abovebelow3 << endl;
           cout << student4 << attendance4 << exam4 << final4 << abovebelow4 << endl;
           cout << student5 << attendance5 << exam5 << final5 << abovebelow5 << endl;

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    final1 = final1 + 5;
    ->
    Code:
    final1 += 5;
    Code:
           string student1;
           string student2;
           string student3;
           string student4;
           string student5;
    Look into arrays.

    You should indent your if statements.

    And one other thing:
    Code:
    using namespace std;
    uses everything in std. So you don't need
    Code:
    using std::string;
    as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Arrays can definitely made your code more simple. So could a structure if your teacher will let you use either of them in this program.

    For example (This is grayed out because you might not want this much of an answer):

    ...ok so they didn't have a great gray color. Just try not to read it if you don't want any answers ...and keep in mind that I am also learning myself, so my code may not be perfect, it's just some ideas for you.

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    struct studentInfo {
           string name;
           int attendance, exam, final;
           string abovebelow;
           };
    
    int main() {
        studentInfo student[4];
        string studPos[5] = {"first", "second", "third", "fourth", "fifth"};
        char dumpChar; // Had to add this to empty the input stream for the next
                       // name.
        double average;
    
        for ( int x = 0; x < 5; x++ ) {
        cout << "Enter " << studPos[x] << " student's name: ";
        getline(cin, student[x].name);
        cout << "Enter " << studPos[x] << " student's attendance: ";
        cin >> student[x].attendance;
        cout << "Enter " << studPos[x] << " student's exam score: ";
        cin >> student[x].exam;
        cout << "Enter " << studPos[x] << " student's final exam score: ";
        cin >> student[x].final;
        cin.get(dumpChar);
        }
        
    // From here you would calculate the rest of your code. The only 
    // problem I have with this code is for some reason something is still 
    // left in the cin stream, so it skips over name entering after you 
    // enter the first. I'm sure there is a simple solution for this, though.
    }
    Last edited by SlyMaelstrom; 10-12-2005 at 05:33 PM.
    Sent from my iPadŽ

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I have an idea that grey text on a grey background is hard to read. Just make it orange or something.

    main should have a return 0, but it doesn't need it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    It's supposed to be hard to read. That's why I did it, so she wouldn't have to look at it if she didn't want to. Also, I didn't put return zero because in my comment I added she should finish her code after that.

    Return 0 is part of that.

    Also, you should really be using iomanip to format the text in your code. Otherwise you'll get weird looking output.

    ...for the hell of it, here is the whole program working with my code. Also some sample output.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    struct studentInfo {
           string name;
           int attendance, exam, final;
           string abovebelow;
           };
    
    int main() {
        int studentNum = 5;
        studentInfo student[studentNum];
        string studPos[20] = {"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", 
                             "9th", "10th", "11th", "12th", "13th", "14th", "15th",
                             "16th", "17th", "18th", "19th", "20th"};
                             // Handles 20 students.
        double average;
        char dumpChar; // Had to add this to empty the input stream for the next
                       // name.
        
        for ( int x = 0; x < studentNum; x++ ) {
        cout << "Enter " << studPos[x] << " student's name: ";
        getline(cin, student[x].name);
        cout << "Enter " << studPos[x] << " student's attendance: ";
        cin >> student[x].attendance;
        cout << "Enter " << studPos[x] << " student's exam score: ";
        cin >> student[x].exam;
        cout << "Enter " << studPos[x] << " student's final exam score: ";
        cin >> student[x].final;
            if (student[x].attendance >= 5)
            student[x].final = student[x].final + 5;
        cin.get(dumpChar);
           
        average = average + student[x].final; 
        }
        
        average = average / studentNum;
        
        for ( int x = 0; x < studentNum; x++ ) {   
            if (student[x].final >= average)
            student[x].abovebelow = "Above Average";
            else
            student[x].abovebelow = "Below Average";
            }
    
        cout << endl;    
        cout << setw(12) << "Student Name" << setw(12) << "Attendance" << setw(12) << "Exam Score"
             << setw(13) << "Final Score" << setw(21) << "Above/Below Average" << endl;
        for ( int x = 0; x < studentNum; x++ ) { 
        cout << setw(12) << student[x].name << setw(12) << student[x].attendance << setw(12)
             << setw(12) << student[x].exam << setw(12) << student[x].final << setw(21)
             << student[x].abovebelow << endl;
             }
                    
        system("PAUSE");
        return 0;    
                      
       }
    
    Sample Output:
    Code:
    Enter 1st student's name: Bill
    Enter 1st student's attendance: 18
    Enter 1st student's exam score: 92
    Enter 1st student's final exam score: 93
    Enter 2nd student's name: Joe
    Enter 2nd student's attendance: 15
    Enter 2nd student's exam score: 75
    Enter 2nd student's final exam score: 83
    Enter 3rd student's name: Sarah
    Enter 3rd student's attendance: 20
    Enter 3rd student's exam score: 100
    Enter 3rd student's final exam score: 100
    Enter 4th student's name: Peter
    Enter 4th student's attendance: 0
    Enter 4th student's exam score: 15
    Enter 4th student's final exam score: 46
    Enter 5th student's name: Jessica
    Enter 5th student's attendance: 18
    Enter 5th student's exam score: 96
    Enter 5th student's final exam score: 67
    
    Student Name  Attendance  Exam Score  Final Score  Above/Below Average
            Bill          18          92           98        Above Average
             Joe          15          75           88        Above Average
           Sarah          20         100          105        Above Average
           Peter           0          15           46        Below Average
         Jessica          18          96           72        Below Average
    Press any key to continue . . .
    The reason it's better to use the array is it's easier to change if say, you wanted to have 30 students listed. You don't have to define 100 new variables, you just change the value of studentNum and ofcourse you must change the values of studPos if you want more than 20 students.
    Last edited by SlyMaelstrom; 10-13-2005 at 06:46 AM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21