Not getting the program to read out lowest number?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Diablo01
    New Member
    • Jun 2014
    • 13

    Not getting the program to read out lowest number?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    // Function prototype
    void getScore(int& score, ifstream& inFile);
    void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest);
    int findLowest(int, int, int, int, int);
    int grades(int, int, int, int, int);
    int main()
    {
    	ifstream inFile;
    	inFile.open("grades.txt");
    	int lowest;
    	int testScr1, testScr2, testScr3, testScr4, testScr5;
    
    	// I deleted all the semicolons after if()
    	// Semicolons after if() are a bad idea; they make the comparison meaningless because a semicolon is a complete statement that means do nothing.
    	// So the if() will do nothing and then run whatever was underneath.
    	getScore(testScr1, inFile);
    	lowest = testScr1;
    	getScore(testScr2, inFile);
    	if (lowest > testScr2)
    		lowest = testScr2;
    	getScore(testScr3, inFile);
    	if (lowest > testScr3)
    		lowest = testScr3;
    	getScore(testScr4, inFile);
    	if (lowest > testScr4)
    		lowest = testScr4;
    	getScore(testScr5, inFile);
    	if (lowest > testScr5)
    		lowest = testScr5;
    
    	calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5, lowest);
    
    	inFile.close();
    
    	return 0;
    }
    
    void getScore(int& score, ifstream& inFile)
    {
    
    	inFile >> score;
    }
    
    
    void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest)
    {
    	int sum;
    	
    	double average;
    
    	
    	sum = s1 + s2 + s3 + s4 + s5 - lowest;
    	average = sum / 4.0;
    
    	cout << setw(4) << fixed << showpoint << setprecision(2);
    	cout << "The avergae of the four highest scores are: " << average << endl;
    }
    
    int findLowest(int s1, int s2, int s3, int s4, int s5)
    {
    	int lowest = s1;
    
    	cout << "The lowest test score is: " << lowest << endl;
    
    	return lowest;
    }
    the program is suppose to report back the lowest number of 5 grades in a file..then it is suppose to report the average of remaining 4 higher scores..It appears to be reporting the correct average which tells me that it is reading the file and dropping the lowest score but it does not report back the lowest score first.I am sure its an easy fix.. I have worked on this for hours..I am new to programming so please be patient.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It doesn't appear you are displaying the lowest value before you call calcAverage.

    There is a findLowest function that you apparently never call. It seems like the insides of this function is now an inline series of if statements.

    Comment

    • Diablo01
      New Member
      • Jun 2014
      • 13

      #3
      Code:
      #include <iostream>
      #include <iomanip>
      #include <fstream>
      using namespace std;
      
      // Function prototype
      void getScore(int& score, ifstream& inFile);
      void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testcr5);
      int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
      int grades(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
      int main()
      {
      	ifstream inFile;
      	inFile.open("grades.txt");
      	int lowest = 0;
      	int testScr1, testScr2, testScr3, testScr4, testScr5;
      	
      
      	getScore(testScr1, inFile);
      	if (testScr1 < lowest)
      	{
      		testScr1 = lowest;
      	}
      	getScore(testScr2, inFile);
      	if (testScr2 < lowest)
      	{
      		testScr2 = lowest;
      	}
      	getScore(testScr3, inFile);
      	if (testScr3 < lowest)
      	{
      		testScr3 = lowest;
      	}
      	getScore(testScr4, inFile);
      	if (testScr4 < lowest)
      	{
      		testScr4 = lowest;
      	}
      	getScore(testScr5, inFile);
      	if (testScr5 < lowest)
      	{
      		testScr5 = lowest;
      	}
      	calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
      
      	inFile.close();
      
      	return lowest;
      }
      
      void getScore(int& score, ifstream& inFile)
      {  
      	inFile >> score;
      }
      
      
      	void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
      	{
      		int sum = 0;
      		int lowest; 
      		double average;
      
      	lowest = findLowest(testScr1, testScr2, testScr3, testScr4, testScr5);
      
      	sum = testScr1 + testScr2 + testScr3 + testScr4 + testScr5 - lowest;
      	average = sum / 4.0;
      
      	cout << setw(4) << fixed << showpoint << setprecision(2);
      	cout << "The avergae of the four highest scores are: " << average << endl;
      }
      
      int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
      {
      	int lowest = testScr1;
      
      	cout << "The lowest test score is: " << lowest << endl;
      
      	return lowest;
      }

      K I made some changes but now it just reads the top number as being the lowest value regardless of the number...It could be 100 and it says the 100 is the lowest score which is not the case. The program reads from a text file that has 5 test cores in it.

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        I think your logic is a little flawed. First, lowest SHOULD = 100 to begin with (since it's the highest score possible)
        Code:
        int lowest = 100;
        Then, if testScr1 < lowest then lowest = testScr1. Whereas you are saying that each score from the IN file actually equals lowest. So,:
        Code:
        lowest = testScr1;
        lowest = testScr2;
        lowest = testScr3;
        lowest = testScr4;
        lowest = testScr5;

        Now, here's the biggest problem: I'm not a C programmer so I don't know if your program is properly reading the IN file or not, but I hope what I've provided helps some!

        Comment

        • Diablo01
          New Member
          • Jun 2014
          • 13

          #5
          Code:
          #include <iostream>
          #include <iomanip>
          #include <fstream>
          using namespace std;
          
          // Function prototype
          void getScore(int& score, ifstream& inFile);
          void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testcr5);
          int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
          int grades(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
          int main()
          {
          	ifstream inFile;
          	inFile.open("grades.txt");
          	int lowest = 100;
          	int testScr1, testScr2, testScr3, testScr4, testScr5;
          	
          
          	getScore(testScr1, inFile);
          	if (testScr1 < lowest )
          	{
          		testScr1 = lowest;
          	}
          	getScore(testScr2, inFile);
          	if (testScr2 < lowest)
          	{
          		lowest = testScr2;
          	}
          	getScore(testScr3, inFile);
          	if (testScr3 < lowest)
          	{
          		lowest = testScr3;
          	}
          	getScore(testScr4, inFile);
          	if (testScr4 < lowest)
          	{
          		lowest = testScr4;
          	}
          	getScore(testScr5, inFile);
          	if (testScr5 < lowest)
          	{
          		lowest = testScr5;
          	}
          	calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
          
          	inFile.close();
          
          	return lowest;
          }
          
          void getScore(int& score, ifstream& inFile)
          {  
          	inFile >> score;
          }
          
          
          	void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
          	{
          		int sum = 0;
          		int lowest; 
          		double average;
          
          	lowest = findLowest(testScr1, testScr2, testScr3, testScr4, testScr5);
          
          	sum = testScr1 + testScr2 + testScr3 + testScr4 + testScr5 - lowest;
          	average = sum / 4.0;
          
          	cout << setw(4) << fixed << showpoint << setprecision(2);
          	cout << "The avergae of the four highest scores are: " << average << endl;
          }
          
          int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
          {
          	int lowest = testScr1;
          
          	cout << "The lowest test score is: " << lowest << endl;
          
          	return lowest;
          }
          LUK3R,
          Thank you for your advise. I did exactly what you said last night at 1 in the morning among other things and when I do this it comes back as 100 being the lowest score and that is not even one of the scores for it to read from...Its like its replacing the first score with 100...cause it reports an average... the average is not right but then again neither is the lowest number reported. i am constantly going in circles..I have re written this code 3 times.. numerous hours on what should be an easy code..At least that is what Im being told.

          Comment

          • Diablo01
            New Member
            • Jun 2014
            • 13

            #6
            edit:
            I saw that I did not change the first one lowest = testScr1..so i did it and now it reads the top number as being the lowest which is not the lowest number. theses are the grades it has to work with which are read from grades.txt:
            71
            94
            62
            75
            82

            Comment

            • Luk3r
              Contributor
              • Jan 2014
              • 300

              #7
              Seems you have some code cleanup to do. In the bottom section of your code you are also saying that lowest = testScr1, which again, is incorrect. You're then just telling your integer to = a score, which is then printed on the screen.

              Comment

              • Diablo01
                New Member
                • Jun 2014
                • 13

                #8
                I did Luk3r...right after i posted the code again..if you see above you will see my results.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  On line 74, you set lowest = testScr1

                  Comment

                  • Luk3r
                    Contributor
                    • Jan 2014
                    • 300

                    #10
                    Thanks, Rabbit. I'm at work so my responses are a little slow... but you covered what I meant :D

                    Comment

                    • Diablo01
                      New Member
                      • Jun 2014
                      • 13

                      #11
                      Ok well I dont know what I should be setting lowest as.This may be my confusion. I am not getting what I should be setting lowest as.. I cant leave it blank..Im lost again...Sorry I am new to programming and I am sure it shows. I feel like this should be easier than what it is.Sorry

                      Comment

                      • donbock
                        Recognized Expert Top Contributor
                        • Mar 2008
                        • 2427

                        #12
                        I think your original code that initialized lowest to the first score is a better idea than initializing lowest to 100. That will always work; while initializing to 100 only works if 100 is the highest possible score. I've gotten back papers scored against a higher range that that.

                        Line 22 is backwards. You want to assign to lowest. Additionally, as I suggest above, you should make that assignment unconditional.

                        Lines 19-43 acquire the inputs and find the lowest value, but you don't use that value. Lines 72-79 purport to find the lowest value, but there is nothing there. You probably want to move some of the logic from lines 19-43 to there.

                        Line 48 uses lowest as the return value for main. However, you won't have that value available if you move the find-lowest logic into the lines 72-79.

                        Comment

                        • Luk3r
                          Contributor
                          • Jan 2014
                          • 300

                          #13
                          I have to assume that donbock knows much more what he's talking about than I do. But I made your code work (including our suggested changes), simply by adding int lowest = 100; under int grades(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);, commenting out int lowest = 100; on line 15, and commenting out int lowest = testScr1; on line 74. Additionally, in regards to what donbock said, if you stick with what we were working on, you can make lowest = 999 if you want, since no paper will ever have that high of a score and every grade will always be lower. I stuck with 100 and used 5 random values.

                          Comment

                          Working...