New Posts  All Forums:Forum Nav:

Help with strings

post #1 of 5
Thread Starter 
For one of my assignment in my C++ class, I need to fill a structure with strings regarding info of a contacts list. I'm reading from a text file that has first and last names, the street, the city, state, etc... and idk how to read the street address as a whole string, considering it will only read until a space. I'm also not sure how to read separate strings like first and last name when there is only a comma in between the 2 and no space. An example of the line of text I'm reading from is : Brad,Broccoli,1 Vegetable Lane,Green Valley,CA,11111,555.555.5551,bbroccoli@somewhere.c om,www.domain.com/bbroccoli. If anyone could help it would be greatly appreciated
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 635 Propus @ 2.9 Ghz ASRock 890FX Deluxe 5 Sapphire HD 5770 x2 4 Gb OCZ Reaper DDR3 1600 
Hard DriveOptical DriveOSMonitor
500 Gb Samsung Spinpoint F3 + 1 TB Spinpoint F3 Blu Ray reader/DVD burner Windows 7 Ultimate x64 23" Acer LCD 2048x1152 
PowerCase
BFG Tech GS-550 550W Antec 300 Illusion 
  hide details  
Reply
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 635 Propus @ 2.9 Ghz ASRock 890FX Deluxe 5 Sapphire HD 5770 x2 4 Gb OCZ Reaper DDR3 1600 
Hard DriveOptical DriveOSMonitor
500 Gb Samsung Spinpoint F3 + 1 TB Spinpoint F3 Blu Ray reader/DVD burner Windows 7 Ultimate x64 23" Acer LCD 2048x1152 
PowerCase
BFG Tech GS-550 550W Antec 300 Illusion 
  hide details  
Reply
post #2 of 5
Hey Jspoon,

Can you show us what you have so far?

Thanks
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #3 of 5
Thread Starter 
Well, I don't have much because i'm getting very confused.

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

struct Contact {
string first_name;
string last_name;
string street;
string city;
string state;
string zipcode;
string phone;
string email;
string url;
};

void OpenFile (ifstream fin);

void FillArray (Contact contacts[], ifstream fin);

using namespace std;
int main()
{
Contact contacts[500];

}

void OpenFile (ifstream fin)
{
fin.open("contacts.txt");
if (fin.fail())
{
cout << "Error reading file";
exit(1);
}
}


void FillArray (Contact contacts[], ifstream fin)
{

int count = 0;

fin >> contacts[count].first_name;
fin >> contacts[count].last_name;
fin >> contacts[count].street;
fin >> contacts[count].city;
fin >> contacts[count].state;
fin >> contacts[count].zipcode;
fin >> contacts[count].phone;
fin >> contacts[count].email;
fin >> contacts[count].url;
count = count + 1;

while (!fin.eof)
{
fin >> contacts[count].first_name;
fin >> contacts[count].last_name;
fin >> contacts[count].street;
fin >> contacts[count].city;
fin >> contacts[count].state;
fin >> contacts[count].zipcode;
fin >> contacts[count].phone;
fin >> contacts[count].email;
fin >> contacts[count].url;
count = count + 1;
}



I made the array of the structures, and opened the file I'm reading from, but i'm getting confused on filling the structure because of the array. And i know how I'm filling the structure isn't right, considering it would read both the first and last name as one string, the street as like 3, and the rest of the info as 1, since its only separated by commas.
Edited by Jspoon - 2/3/11 at 8:52am
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 635 Propus @ 2.9 Ghz ASRock 890FX Deluxe 5 Sapphire HD 5770 x2 4 Gb OCZ Reaper DDR3 1600 
Hard DriveOptical DriveOSMonitor
500 Gb Samsung Spinpoint F3 + 1 TB Spinpoint F3 Blu Ray reader/DVD burner Windows 7 Ultimate x64 23" Acer LCD 2048x1152 
PowerCase
BFG Tech GS-550 550W Antec 300 Illusion 
  hide details  
Reply
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 635 Propus @ 2.9 Ghz ASRock 890FX Deluxe 5 Sapphire HD 5770 x2 4 Gb OCZ Reaper DDR3 1600 
Hard DriveOptical DriveOSMonitor
500 Gb Samsung Spinpoint F3 + 1 TB Spinpoint F3 Blu Ray reader/DVD burner Windows 7 Ultimate x64 23" Acer LCD 2048x1152 
PowerCase
BFG Tech GS-550 550W Antec 300 Illusion 
  hide details  
Reply
post #4 of 5
It sounds like you just need to control how your input stream is getting the data. I would suggest using get() or getline().

getline(buff,buffsize,delim) would probably be the easiest way, since you can specify a delimiter character value. If you don't specify a delimiter it will read until it hits the '\
' newline character.
Hurts So Good
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 960T Asus Crosshair III Formula EVGA GTX570 16GB G.SKILL DDR3 
Hard DriveOSMonitorKeyboard
2 x WD 1TB 64MB Cache Windows 7 Pro 64-bit Samsung 24.6" 2ms Logitech G15 
PowerCaseMouse
Corsair 850W Antec Nine Hundred Two Logitech G700 
  hide details  
Reply
Hurts So Good
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 960T Asus Crosshair III Formula EVGA GTX570 16GB G.SKILL DDR3 
Hard DriveOSMonitorKeyboard
2 x WD 1TB 64MB Cache Windows 7 Pro 64-bit Samsung 24.6" 2ms Logitech G15 
PowerCaseMouse
Corsair 850W Antec Nine Hundred Two Logitech G700 
  hide details  
Reply
post #5 of 5
You can read the whole line and parse it, or you can read comma to comma with getline() as Kl1X outlined.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming