Reading From a File

I have to read and analyze a file called Random.rtf full of random numbers. The output of my program is:

File Random.rtf is open.
Number of Numbers: 0
Sum of Numbers: 0
Average of Numbers: nan
Largest Number: 0
Smallest Number: 1001
Second Largest Number: 0
Second Smallest Number: 1001

Am I doing something wrong in my code? I have confirmed that my file is opening I just can't get it to read any numbers from it.

Im using x-code if that changes anything.

Thanks,
Spenser


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

using namespace std;
int main()
{
const string file_name = "Random.rtf";
ifstream input_file;
double total = 0, sum = 0, big_num = 0, sm_num = 1001, big_num2 = 0, sm_num2 = 1001;
double num, ave;

input_file.open (file_name);

if (input_file)
{
cout << "File " << file_name << " is open." << endl;

while (input_file >> num)
{
total++;
sum += num;

if (num >= big_num)
big_num = num;

if (num <= sm_num)
sm_num = num;

if (num >= big_num2 && num < big_num)
big_num2 = num;

if (num < sm_num2 && num > sm_num)
sm_num2 = num;
}

ave = (sum/total);

cout << "Number of Numbers:" << setw (5) << total << endl;
cout << "Sum of Numbers:" << setw (5) << sum << endl;
cout << "Average of Numbers:" << setw (5) << ave << endl;
cout << "Largest Number:" << setw (5) << big_num << endl;
cout << "Smallest Number:" << setw (5) << sm_num << endl;
cout << "Second Largest Number:" << setw (5) << big_num2 << endl;
cout << "Second Smallest Number:" << setw (5) << sm_num2 << endl;

}

else cout << "Error opening " << file_name;

input_file.close();

return 0;
}
RTF files are not simple text files. They contain a load of formatting info in them. Try to open rtf file with notepad and you will see what it really contains.
I got a .txt file and it is working but theres a bug. It doesn't calculate the second largest number right. I think the same thing is wrong with the second smallest but it just incidentally got the right value.
it would be easier if we could see your input and what big_num2 ended up equaling
Last edited on
42
468
335
501
170
725
479
359
963
465
706
146
282
828
962
492
996
943
828
437
392
605
903
154
293
383
422
717
719
896
448
727
772
539
870
913
668
300
36
895
704
812
323
334
674
665
142
712
254
869
548
645
663
758
38
860
724
742
530
779
317
36
191
843
289
107
41
943
265
649
447
806
891
730
371
351
7
102
394
549
630
624
85
955
757
841
967
377
932
309
945
440
627
324
538
539
119
83
930
542
834
116
640
659
705
931
978
307
674
387
22
746
925
73
271
830
778
574
98
513
987
291
162
637
356
768
656
575
32
53
351
151
942
725
967
431
108
192
8
338
458
288
754
384
946
910
210
759
222
589
423
947
507
31
414
169
901
592
763
656
411
360
625
538
549
484
596
42
603
351
292
837
375
21
597
22
349
200
669
485
282
735
54
1000
419
939
901
789
128
468
729
894
649
484
808
422
311
618
814
515


The problem with the way I have it is that when it gets to the actual second max (987) that is the largest number so the program assigns that to big_num so big_num2 can't take that value on. 1000 is the largest value but that doesn't come up until much later in the file.
add one more line to your first if statement.
1
2
3
4
5
if (num >= big_num)
{
big_num2= big_num;
big_num = num;
}

Pretty sure this would work, but try it out and let me know haha... and actually maybe you can use this to simplify your if statements a bit. You might not even need the last one, and you may be able to eliminate the 2nd to last by modifying the one right after the first if like this one.
Last edited on
Topic archived. No new replies allowed.