New Posts  All Forums:Forum Nav:

C++ Array help

post #1 of 10
Thread Starter 
I am writing a program to read in all the names in a file to a array, then allowing someone to search it and determine the rank of the name.

If someone wants to just PM me we could go back and forth quickly I doubt this will take long to explain.

If not here is what I have so far.

Code:
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void fill(ifstream& input, ofstream& output);

int search(char bname[1000], char gname[1000], char inpu);

int main()
{
ifstream in;
ofstream out;

in.open("babynames2004.txt");
out.open("bnames.txt");

fill(in, out);

in.close();
out.close();

return 0;
}


void fill(ifstream& input, ofstream& output)
{
char bname[1000], gname[1000];
int rank, ranking[1000];
char inpu, result, ans;

while (rank=0, rank>1000, rank++)
{
input >> bname >> gname;
rank = ranking;
}

do
{
cout << "What name would you like to search for?" << endl;
cin >> inpu;

result = search(bname, gname, inpu);

cout << inpu << " is " << result << endl;

cout << "would you like to run this program again? (Y or y to continue)" << endl;
cin >> ans;

} while ((ans='Y') || (ans='y'));

}
Also the file is in the format of
Quote:
1. john emma

rank boyname girlname

Edited by sdla4ever - 5/9/11 at 7:52pm
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #2 of 10
Well I'll take a shot at helping ya, sir.

Most recently I've been doing alot of C#, so pardon in advance if I misspeak or say something that sounds foreign.

It seems that while you have a solid idea, detname is only running once. Perhaps you can put it in a for loop, to run 1000 times (assuming that there are the Top 1000 Baby Names).

How's that sound?
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
post #3 of 10
sounds like u might need a struct
Code:

struct names
{
char bname[1000];
char gname[1000];
int rank;
}
int main()
{
ifstream in;
ofstream out;

in.open("babynames2004.txt");
out.open("bnames.txt");

//read from file
//input string query
//search from the array, use a loop
//if string.find() != string::npos //means there is a match
//output bname,gname and rank

in.close();
out.close();

return 0;
}
Why not use strings instead of char arrays? string::find() is a function which you can use from your program
http://www.cplusplus.com/reference/string/string/find/
Budget Build!
(15 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 955 Black Edition MSI 870A-G54 ATI radeon 6850 G Skill 8GB DDR3 PC12800 
Hard DriveHard DriveCoolingOS
Seagate Barracuda 7200.12A 500GB Crucial M4 CoolerMaster 212+ Windows 7 Home Premium 64bit 
MonitorKeyboardPowerCase
LG 18.5" 1943SE-PF Logitech Aerocool 500W PSU Fractal Design R3 
MouseMouse Pad
Razer Deathadder Razer Mantis 
  hide details  
Reply
Budget Build!
(15 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 955 Black Edition MSI 870A-G54 ATI radeon 6850 G Skill 8GB DDR3 PC12800 
Hard DriveHard DriveCoolingOS
Seagate Barracuda 7200.12A 500GB Crucial M4 CoolerMaster 212+ Windows 7 Home Premium 64bit 
MonitorKeyboardPowerCase
LG 18.5" 1943SE-PF Logitech Aerocool 500W PSU Fractal Design R3 
MouseMouse Pad
Razer Deathadder Razer Mantis 
  hide details  
Reply
post #4 of 10
what seems to be the problem?

You need to create an array of objects...each object holds all 3 pieces of data.

This will make searching and printing much simpler.


Since your using c++ just create a new class called BabyName...then in main you would create the array of Objects like this:

Code:
BabyName* data[amount];

for(int i = 0; i < amount; ++i){
        data[i] = new BabyName(put all 3 pieces of data here);
}
you would put this for loop inside another for loop that grabs the information from the file. You would also have to create 1 constructor that takes 3 parameters obviously when each object is created.
Edited by surfbumb - 5/8/11 at 11:04pm
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 #5 of 10
Thread Starter 
Ok thanks so far, I'm going to read through this chapter then continue to try. I'll post back in a hour or 2.
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #6 of 10
If you want to lookup the rank by entering a name you probably want to use a hashtable rather than an array. C++ is not the best choice for this task - a python script to do it would be about a dozen lines. Are you constrained to using c++?
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #7 of 10
Quote:
Originally Posted by rabidgnome229 View Post
If you want to lookup the rank by entering a name you probably want to use a hashtable rather than an array. C++ is not the best choice for this task - a python script to do it would be about a dozen lines. Are you constrained to using c++?
definitely a hashtable especially if the amount of elements is large...good point...but this is beyond the scope of the assignment I would believe.

OP - Look up how to construct an array of Objects and you should see how easy the assignment becomes.
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 10
Thread Starter 
Quote:
Originally Posted by surfbumb View Post
definitely a hashtable especially if the amount of elements is large...good point...but this is beyond the scope of the assignment I would believe.

OP - Look up how to construct an array of Objects and you should see how easy the assignment becomes.
I hate having to teach myself lol, I'll look. I updated the code in the OP

This class is strictly online so I don't ever have the luxury of speaking to the teacher for help.
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #9 of 10
Quote:
Originally Posted by sdla4ever View Post
I hate having to teach myself lol, I'll look. I updated the code in the OP

This class is strictly online so I don't ever have the luxury of speaking to the teacher for help.
I just finished my online c++ class...have the final this week. I've basically taught myself with the primer c++ book...and some of the professors notes...but mostly you learn by doing. We finished the class with an inheritance assignment and a polymorphism assignment...it was challenging.

I wish I had the time to help you but I dont atm.

If you just have a simple text file...and have bname, gname, and rank all on a single line...then you would use a delimiter to separate the information into the appropriate variables when you read in from the file.

Ex. bname gname rank

Your delimiter would simply be " "... since each is separated by a space...again you would have to look this up.
Edited by surfbumb - 5/9/11 at 10:15pm
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 #10 of 10
Quote:
Originally Posted by surfbumb View Post
Ex. bname gname rank

Your delimiter would simply be " "... since each is separated by a space...again you would have to look this up.
Code:
#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include <iterator>
#include <sstream>
#include <algorithm>

int main()
{
  std::string str = "The quick brown fox";

  // construct a stream from the string
  std::stringstream strstr(str);

  // use stream iterators to copy the stream to the vector as whitespace separated strings
  std::istream_iterator<std::string> it(strstr);
  std::istream_iterator<std::string> end;
  std::vector<std::string> results(it, end);

  // send the vector to stdout.
  std::ostream_iterator<std::string> oit(std::cout);
  std::copy(results.begin(), results.end(), oit);
}
From this source: http://stackoverflow.com/questions/5...-a-string-in-c
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming