473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

Not getting the program to read out lowest number?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. // Function prototype
  7. void getScore(int& score, ifstream& inFile);
  8. void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest);
  9. int findLowest(int, int, int, int, int);
  10. int grades(int, int, int, int, int);
  11. int main()
  12. {
  13.     ifstream inFile;
  14.     inFile.open("grades.txt");
  15.     int lowest;
  16.     int testScr1, testScr2, testScr3, testScr4, testScr5;
  17.  
  18.     // I deleted all the semicolons after if()
  19.     // Semicolons after if() are a bad idea; they make the comparison meaningless because a semicolon is a complete statement that means do nothing.
  20.     // So the if() will do nothing and then run whatever was underneath.
  21.     getScore(testScr1, inFile);
  22.     lowest = testScr1;
  23.     getScore(testScr2, inFile);
  24.     if (lowest > testScr2)
  25.         lowest = testScr2;
  26.     getScore(testScr3, inFile);
  27.     if (lowest > testScr3)
  28.         lowest = testScr3;
  29.     getScore(testScr4, inFile);
  30.     if (lowest > testScr4)
  31.         lowest = testScr4;
  32.     getScore(testScr5, inFile);
  33.     if (lowest > testScr5)
  34.         lowest = testScr5;
  35.  
  36.     calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5, lowest);
  37.  
  38.     inFile.close();
  39.  
  40.     return 0;
  41. }
  42.  
  43. void getScore(int& score, ifstream& inFile)
  44. {
  45.  
  46.     inFile >> score;
  47. }
  48.  
  49.  
  50. void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest)
  51. {
  52.     int sum;
  53.  
  54.     double average;
  55.  
  56.  
  57.     sum = s1 + s2 + s3 + s4 + s5 - lowest;
  58.     average = sum / 4.0;
  59.  
  60.     cout << setw(4) << fixed << showpoint << setprecision(2);
  61.     cout << "The avergae of the four highest scores are: " << average << endl;
  62. }
  63.  
  64. int findLowest(int s1, int s2, int s3, int s4, int s5)
  65. {
  66.     int lowest = s1;
  67.  
  68.     cout << "The lowest test score is: " << lowest << endl;
  69.  
  70.     return lowest;
  71. }
  72.  
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.
Jul 2 '14 #1
12 1276
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 2 '14 #2
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. // Function prototype
  7. void getScore(int& score, ifstream& inFile);
  8. void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testcr5);
  9. int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
  10. int grades(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
  11. int main()
  12. {
  13.     ifstream inFile;
  14.     inFile.open("grades.txt");
  15.     int lowest = 0;
  16.     int testScr1, testScr2, testScr3, testScr4, testScr5;
  17.  
  18.  
  19.     getScore(testScr1, inFile);
  20.     if (testScr1 < lowest)
  21.     {
  22.         testScr1 = lowest;
  23.     }
  24.     getScore(testScr2, inFile);
  25.     if (testScr2 < lowest)
  26.     {
  27.         testScr2 = lowest;
  28.     }
  29.     getScore(testScr3, inFile);
  30.     if (testScr3 < lowest)
  31.     {
  32.         testScr3 = lowest;
  33.     }
  34.     getScore(testScr4, inFile);
  35.     if (testScr4 < lowest)
  36.     {
  37.         testScr4 = lowest;
  38.     }
  39.     getScore(testScr5, inFile);
  40.     if (testScr5 < lowest)
  41.     {
  42.         testScr5 = lowest;
  43.     }
  44.     calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
  45.  
  46.     inFile.close();
  47.  
  48.     return lowest;
  49. }
  50.  
  51. void getScore(int& score, ifstream& inFile)
  52. {  
  53.     inFile >> score;
  54. }
  55.  
  56.  
  57.     void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
  58.     {
  59.         int sum = 0;
  60.         int lowest; 
  61.         double average;
  62.  
  63.     lowest = findLowest(testScr1, testScr2, testScr3, testScr4, testScr5);
  64.  
  65.     sum = testScr1 + testScr2 + testScr3 + testScr4 + testScr5 - lowest;
  66.     average = sum / 4.0;
  67.  
  68.     cout << setw(4) << fixed << showpoint << setprecision(2);
  69.     cout << "The avergae of the four highest scores are: " << average << endl;
  70. }
  71.  
  72. int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
  73. {
  74.     int lowest = testScr1;
  75.  
  76.     cout << "The lowest test score is: " << lowest << endl;
  77.  
  78.     return lowest;
  79. }
  80.  

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.
Jul 2 '14 #3
Luk3r
300 256MB
I think your logic is a little flawed. First, lowest SHOULD = 100 to begin with (since it's the highest score possible)
Expand|Select|Wrap|Line Numbers
  1. 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,:
Expand|Select|Wrap|Line Numbers
  1. lowest = testScr1;
  2. lowest = testScr2;
  3. lowest = testScr3;
  4. lowest = testScr4;
  5. 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!
Jul 2 '14 #4
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. // Function prototype
  7. void getScore(int& score, ifstream& inFile);
  8. void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testcr5);
  9. int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
  10. int grades(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
  11. int main()
  12. {
  13.     ifstream inFile;
  14.     inFile.open("grades.txt");
  15.     int lowest = 100;
  16.     int testScr1, testScr2, testScr3, testScr4, testScr5;
  17.  
  18.  
  19.     getScore(testScr1, inFile);
  20.     if (testScr1 < lowest )
  21.     {
  22.         testScr1 = lowest;
  23.     }
  24.     getScore(testScr2, inFile);
  25.     if (testScr2 < lowest)
  26.     {
  27.         lowest = testScr2;
  28.     }
  29.     getScore(testScr3, inFile);
  30.     if (testScr3 < lowest)
  31.     {
  32.         lowest = testScr3;
  33.     }
  34.     getScore(testScr4, inFile);
  35.     if (testScr4 < lowest)
  36.     {
  37.         lowest = testScr4;
  38.     }
  39.     getScore(testScr5, inFile);
  40.     if (testScr5 < lowest)
  41.     {
  42.         lowest = testScr5;
  43.     }
  44.     calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
  45.  
  46.     inFile.close();
  47.  
  48.     return lowest;
  49. }
  50.  
  51. void getScore(int& score, ifstream& inFile)
  52. {  
  53.     inFile >> score;
  54. }
  55.  
  56.  
  57.     void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
  58.     {
  59.         int sum = 0;
  60.         int lowest; 
  61.         double average;
  62.  
  63.     lowest = findLowest(testScr1, testScr2, testScr3, testScr4, testScr5);
  64.  
  65.     sum = testScr1 + testScr2 + testScr3 + testScr4 + testScr5 - lowest;
  66.     average = sum / 4.0;
  67.  
  68.     cout << setw(4) << fixed << showpoint << setprecision(2);
  69.     cout << "The avergae of the four highest scores are: " << average << endl;
  70. }
  71.  
  72. int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
  73. {
  74.     int lowest = testScr1;
  75.  
  76.     cout << "The lowest test score is: " << lowest << endl;
  77.  
  78.     return lowest;
  79. }
  80.  
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.
Jul 2 '14 #5
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
Jul 2 '14 #6
Luk3r
300 256MB
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.
Jul 2 '14 #7
I did Luk3r...right after i posted the code again..if you see above you will see my results.
Jul 2 '14 #8
Rabbit
12,516 Expert Mod 8TB
On line 74, you set lowest = testScr1
Jul 2 '14 #9
Luk3r
300 256MB
Thanks, Rabbit. I'm at work so my responses are a little slow... but you covered what I meant :D
Jul 2 '14 #10
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
Jul 2 '14 #11
donbock
2,426 Expert 2GB
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.
Jul 2 '14 #12
Luk3r
300 256MB
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.
Jul 2 '14 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Randi | last post by:
I am having problems with this question: Write a C++ while statement that will input numbers from a file until the end of file is reached. Output the lowest number in the file. Ok, I do...
2
by: cs8404 | last post by:
I cannot quite figure out how to accomplish the following results. My table is "Products" with the following fields: ID Item Metal Size Price 1 Ring 18ctGold 4-7 $23.00...
2
by: laura | last post by:
Hi, I'm looking for the algorithm which does the lowest number of comparison for the string matching problem (in the worst case). I know that the complexity is liniar in the length of the...
1
by: coolindienc | last post by:
I got this program running and gives me result that I want (actually not yet). Now I want to find out the lowest and highest numbers I entered. Andy advise??? number = int (input ("Enter a...
13
by: td0g03 | last post by:
Hello again guys. I have a question. I'm working on a program for my class and I'm stuck on how to find the lowest and highest number of a user input. We aren't at arrays yet so that's out of the...
1
by: scubber | last post by:
hi, im new (as you're going to painfully aware of in a minute) to programming and was wondering if anyone could help with this little problem: i am trying to read a number of values which are...
5
by: Zamdrist | last post by:
Let's say I have a result set (records) containing numbers such as: 0 1 2 3 9 16 21 45
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.