Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c++ text file into 2d array
New Posts  All Forums:Forum Nav:

c++ text file into 2d array

post #1 of 9
Thread Starter 
I need to read this .txt file into an array;

John 100 100 90 95 85 100 90 100 100 98 75
Tom 90 92 82 95 89 93 95 97 96 98 92
Shannon 100 90 95 85 100 65 75 95 100 90 60
Matt 98 90 95 85 100 90 90 95 100 90 82
Steve 100 100 90 95 85 75 85 90 95 100 78
Nicole 90 90 95 100 75 100 100 90 92 82 68

The disaster I've managed to create so far;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

ifstream d_in;
ofstream d_out;
d_in.open("Score.txt");

if (d_in.fail())
{
cout << "Input file failed to open." << endl;
exit(1);
}

int scores [6][12];
d_in >> scores;

return 0;
}

Gives me this error;
error: no match for 'operator>>' in 'd_in >> scores'

I've search the internet and my book for hours and I am completely lost.
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
post #2 of 9
Thread Starter 
Changed

d_in >> scores;

to

for (x=0;x<6;x++)
for (y=0;y<12;y++)
{
d_in >> scores[x][y];
}

and the error went away, but the array is still just filled with random garbage and not the info from the file.
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
post #3 of 9
Quote:
Originally Posted by Force_AMD;15491527 
Changed

d_in >> scores;

to

for (x=0;x<6;x++)
for (y=0;y<12;y++)
{
d_in >> scores[x][y];
}

and the error went away, but the array is still just filled with random garbage and not the info from the file.

That's because you have to parse the names first.

Have you tried importing everything into a string and then getting the data from there?
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
post #4 of 9
Thread Starter 
Quote:
That's because you have to parse the names first.

Have you tried importing everything into a string and then getting the data from there?

Im a civil engineering major so youll have to excuse my lack of knowledge about this stuff. Just required to take a programming class.

What does parse the names mean?

I dont know how to do anything with strings.
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
post #5 of 9
Quote:
Originally Posted by Force_AMD;15491600 
Im a civil engineering major so youll have to excuse my lack of knowledge about this stuff. Just required to take a programming class.

What does parse the names mean?

I dont know how to do anything with strings.

I am no programmer either, just do it as a hobby.

What I do when dealing with files is the following.
Code:
ifstream inputfile;
string line = "";
string file  = "";

inputfile.open("myfile.txt", ios::beg);

while (!inputfile.eof())
{
getline(inputfile, line);
file += line;
file += "\n";
}

That code will put the hole file into a character string, it is much easier for me to deal with files that way, but I am sure that there are better ways to do this.

Once you have that string, you can start an algorithm to exclude everything that is not a number, and put the numbers in the array. I'll see if I can do some little example to show you.
Edited by Joakitex - 10/28/11 at 4:50pm
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
post #6 of 9
Thread Starter 
Yeah please if you could give me an example that would be awesome. I am pretty well lost at this point. And burnt out after hours of searching for a solution.
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
post #7 of 9
Code:

#include <fstream>  
#include <iostream>  
#include <string>
using namespace std;  
  
int main(){  
   const int MAX = 80;
   char buffer[MAX];  
   string big[20]; //# of lines to be read
   ifstream infile("Score.TXT"); 
   int i = 0;
   while( !infile.eof() ){  
      infile.getline(buffer, MAX); 
      big[i] = buffer;     
      ++i; 
   }  
   
   int t = 0;
   //print lines from the string array
   while(!big[t].empty()){
        cout << big[t] << endl;
        ++t;
     }
   return 0;  
}

try this out... it should store each line from the file into a string array....then print each line to the console...exactly the same look as the text file. Now that you have easy access to the data...you can parse it into whatever you want.
Edited by surfbumb - 10/28/11 at 6:27pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #8 of 9
It's quite a mess but it works lol. If you don't understand something just ask me.

http://www.mediafire.com/?ynfiepvqn173fuf
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
post #9 of 9
Thread Starter 
someone helped me reach this conclusion which works!

string name[6]; //array to store the names
int scores[6][11]; //array to store the scores
int x, y;

for(int x = 0; x < 6; x++)
{
d_in >> name[x];
for(int y = 0; y < 11; y++)
d_in >> scores[x][y];
}

thanks for all your help
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
Bob 1/2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 (4 cores @ 3.2 GHz) Gigabyte GA-MA770T-UD3P XFX Radeon HD 5850 (920 MHz core, 1250 MHz mem.) G.Skill 2x2GB DDR3 1333 
Hard DriveOSMonitorPower
WD WD6401AALS & WD2500KS Windows 7 Pro x64 Acer AL1916W Corsair 620HX 
Case
NZXT Alpha 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c++ text file into 2d array