Extracting numbers from .txt file into array.

Oct 31, 2014 at 3:34am
I figured out how to place the file, but whenever I try to extract the txt file and put it into an array it messes up. Can anyone help? Thanks. I left out some of the extra functions because its really long with them, and I didn't have any problems when the user inputted the scores. The problems occurs somewhere between 17-38.

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
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int Max_array = 12;
int quizArray[12];
int quizarraycopy[12];

std::string reportTitle = "\n Quiz Scores\n ---------------\n";


int buildArray()
{int numberofQuizzes =0;
    
    ifstream inFile;
    inFile.open( "/Users/BB/Desktop/quizscores.txt" );
    
    if ( inFile.fail() )
    {
        cout << "The quizscores.txt input file did not open";
        exit(-1);
        }
    
    ifstream file("file.txt");
    if(file.is_open())
    {
        for (int i = 0; i <= Max_array; i++)
        {inFile >> quizArray[i];
        numberofQuizzes++;
        }
    }
    return numberofQuizzes;
}
int main()
{
    int quiz_number = 0;
// hey idoit change the program below for version 1 or verison 2
    quiz_number = buildArray( );
    int i = 0;
    for(int i=0; i < quiz_number; i++)
    {
        quizarraycopy[i]=quizArray[i];
    }
   
    sortArray(quizArray, quiz_number);
    double quizaverage = calcQuizAverage(quizArray, quiz_number);
    
    std::cout<< "\nYour quiz average is " << std::setprecision(2) << fixed <<  quizaverage<< "\n";
    
    printArray(quizarraycopy, quiz_number);
    return 0;
}
Oct 31, 2014 at 3:41am
here is the txt file:
1
2
3
4
5
6
7
8
9
10
11
12
8
6
0
0
10
0
8
10
3
8
8
8
Oct 31, 2014 at 3:57am
@BB2921

Your problem probably lies here.
1
2
3
4
5
6
7
if(file.is_open())
    {
        for (int i = 0; i <= Max_array; i++)
        {inFile >> quizArray[i];
        numberofQuizzes++;
        }
    }

This is is trying to gather 13 bits of info, and place it an array that can hold only 12 bits of info. 0 thru 11. Try changing <= to just <

Also, does your program actually find your data?

This..
inFile.open( "/Users/BB/Desktop/quizscores.txt" );
should pronably look more like this..

inFile.open( "\\Users\\BB\\Desktop\\quizscores.txt" );
Oct 31, 2014 at 4:07am
It collects data, but it outputs this:

Your quiz average is nan

 Quiz Scores
 ---------------


And I tried doing the backslashes and the program fails to read. I am using xcode/mac if that makes a difference.
Oct 31, 2014 at 4:19am
@BB2921

Okay. But you should verify it is, by outputting the array after the reads, just to make sure. Also, I don't think arrays that are created to be 12, can hold 13 bits of info., even on a Mac. QuizArray[0] to QuizArray[12], is 13. So, it should only be QuizArray[0] to QuizArray[11], to make 12.
Oct 31, 2014 at 4:46am
Even when I changed it the max_array to 11, and used the <= it it still said "your quiz average is nan" And i manually outputed all the quizarray[0-11] and they all came back as zeros.

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
int Max_array = 11;
int quizArray[12];
int quizarraycopy[12];

std::string reportTitle = "\n Quiz Scores\n ---------------\n";



int buildArray()
{int numberofQuizzes =0;
    
    ifstream inFile;
    inFile.open( "/Users/BB/Desktop/quizscores.txt" );
    
    if ( inFile.fail() )
    {
        cout << "The quizscores.txt input file did not open";
        exit(-1);
        }
    
    ifstream file("file.txt");
    if(file.is_open())
    {
        for (int i = 0; i <= Max_array; i++)
        {inFile >> quizArray[i];
        numberofQuizzes++;
        }
    }
    return numberofQuizzes;
}
Last edited on Oct 31, 2014 at 4:46am
Oct 31, 2014 at 5:19am
@BB2921

I compiled your code, and had file.txt in the source directory. It ran, and printed out.

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
// Test.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int Max_array = 12;
int quizArray[12];
int quizarraycopy[12];

std::string reportTitle = "\n Quiz Scores\n ---------------\n";


int buildArray()
{int numberofQuizzes =0;

ifstream inFile;
inFile.open( "quizscores.txt" );

if ( inFile.fail() )
{
 cout << "The quizscores.txt input file did not open";
 exit(-1);
}

ifstream file("file.txt"); // Different file than first looked for
if(file.is_open())
{
 for (int i = 0; i < Max_array; i++)
 {
  inFile >> quizArray[i];
 numberofQuizzes++;
 }
 for (int i = 0; i < Max_array; i++)// Check if array was filled
 {
	cout << quizArray[i] << endl; // Found, it was, and printed out

 }
cout << "Number of Quizzes was " << numberofQuizzes << endl; // Prints 12
}
return numberofQuizzes;
}

int main()
{
 int quiz_number = 0;
 // hey idoit change the program below for version 1 or verison 2
 quiz_number = buildArray( );
 int i = 0;
 for(int i=0; i < quiz_number; i++)
 {
	quizarraycopy[i]=quizArray[i];
 }

 /*
// Removed from program. Not needed as of yet.
sortArray(quizArray, quiz_number);
 double quizaverage = calcQuizAverage(quizArray, quiz_number);

 std::cout<< "\nYour quiz average is " << std::setprecision(2) << fixed <<  quizaverage<< "\n";

 printArray(quizarraycopy, quiz_number);*/
 return 0;
}
Oct 31, 2014 at 5:26am
Awesome thanks, I'm just an idiot and didn't have matching files.txt where you pointed it out, but it's working now. Thank you so much!
Topic archived. No new replies allowed.