Code is outputting the wrong values

Sep 26, 2014 at 7:53pm
Whenever I run my code it's outputting strange values for the number of values in CS 250.

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
  while(in_file){
            //Output the line
            cout << left << setw(24) << setfill( ' ' ) << "Grade Value" << ": " << grade_value << "\n"
                 << left << setw(24) << setfill( ' ' ) << "Course Name"  << ": ";

            //Classify the course as CS150 or CS250
            //increment the appropriate counter
            //update the appropriate total

            if( course_name == "CS150" ){

                //Adding all of the grades for CS150
                grade_count_cs150++;
                addition_total_cs150 += grade_value;
                cout << "CS150\n";

            }

            ///Complete the else part for CS250

            else {
                    //Adding all of the grades for CS250
                    grade_count_cs250++;
                    addition_total_cs250 += grade_value;
                    cout << "CS250\n";
                }

            //Insert a blank line

            cout << "\n";

            ///Read next line from the input file

            in_file >> grade_value >> course_name;

        }//End of while loop 


The file is outputting these values: http://imgur.com/u7cZGcT

Thanks for the help
Sep 26, 2014 at 8:05pm
How and where were grade_count_cs150 and addition_total_cs150 initialized? And was grade_value "read" once before the loop?
Sep 26, 2014 at 8:09pm
Both Grade_Counts are initialized as Double and grade_value/course_name were read before the while loop began.
Edit: here's even more of the code to show
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
78
79
80
81
82
83
84
85
86
87
88
ifstream in_file;
        in_file.open("A4-Input.txt");

        ///Write the Code for File Stream Validation, Print an ERROR message if the file could not be found!

        if( !in_file )
        {
            cerr << "\n\n Error: Input file could not be opened."
                 << "\n\tExiting the program.\n\n\n";
            return 1;
        }

        //Set cout precision
        cout.precision( 2 );
        cout.flags( ios::showpoint | ios::fixed );

        //Read the first line
        in_file >> grade_value >> course_name;

        while(in_file){
            //Output the line
            cout << left << setw(24) << setfill( ' ' ) << "Grade Value" << ": " << grade_value << "\n"
                 << left << setw(24) << setfill( ' ' ) << "Course Name"  << ": ";

            //Classify the course as CS150 or CS250
            //increment the appropriate counter
            //update the appropriate total

            if( course_name == "CS150" ){

                //Adding all of the grades for CS150
                grade_count_cs150++;
                addition_total_cs150 += grade_value;
                cout << "CS150\n";

            }

            ///Complete the else part for CS250

            else {
                    //Adding all of the grades for CS250

                    addition_total_cs250 += grade_value;
                    grade_count_cs250++;
                    cout << "CS250\n";
                }

            //Insert a blank line

            cout << "\n";

            ///Read next line from the input file

            in_file >> grade_value >> course_name;

        }//End of while loop

        //Print Program Heading

        cout << left  << setw( 60 ) << setfill( '*' ) << "*" << "\n"
             << right << setfill( ' ' ) << setw( 38 ) << "Average Calculator\n"
             << left  << setw( 60 ) << setfill( '*' ) << "*" << "\n";
        //Output the final summary
        ///Complete the cout statement
        cout << setfill(' ')
             << left << setw(40) << "Number of grades in CS150"         << ": " << right << setw(8) << grade_count_cs150 << "\n"
             << left << setw(40) << "Total Addition of grades in CS150" << ": " << right << setw(8) << addition_total_cs150 << "\n"
             << left << setw(40) << "Average Numeric grade in CS150"    << ": " << right << setw(8) << addition_total_cs150/grade_count_cs150 << "\n"
             << "\n"
             << left << setw(40) << "Number of grades in CS250"         << ": " << right << setw(8) << grade_count_cs250 << "\n"
             << left << setw(40) << "Total Addition of grades in CS250" << ": " << right << setw(8) << addition_total_cs250 << "\n"
             << left << setw(40) << "Average Numeric grade in CS250"    << ": " << right << setw(8) << addition_total_cs250/grade_count_cs250 << "\n";

        ///Close the input file

        in_file.close();

    }
    //End of Program 2

    else{

        cout<< "INVALID CHOICE! Please choose a number from Main Menu!"<<endl;

    }

    return 0;
}
Last edited on Sep 26, 2014 at 8:10pm
Sep 26, 2014 at 8:17pm
That code still doesn't show how the two variables in question were initialized. And there is a difference between initialized and declared.
1
2
3
double value; // declared
double test = 1.0; // declared and initialized.
value = 10.0; // Initial value is 10.0 (initialized). 
Sep 26, 2014 at 8:22pm
Oh. The variable was only declared. I just initialized it as 0 but nothing has changed. Basically random values are being outputted by the program.
Sep 26, 2014 at 9:22pm
Post the smallest complete program that illustrates your problem, one that can be compiled and exhibits the problem.
Topic archived. No new replies allowed.