How to use the peek function

I am writing a program that reads in information from a text file into an array. I made a struct containing all the components of the text file and within that struct i have sub structs. I want to read a date from that text file. I want to use the peek function to read the numbers then stop at each '.'. How would I incorporate that function into my program? Sorry if my problem still doesn't make sense. If it doesn't I will try my best to explain again. I am still new to programming. Here are the functions and structs Im using:
struct Date
{
double month;
double day;
double year;
};

struct Size
{
double length;
double width;
double height;
};

struct money
{
int dollars;
int cents;
};

struct Shipment
{
string name;
Date expDate;
Size boxSize;
double weight;
char storage;
Date shipDate;
money price;
};

void printHeading() {
int width = 12;
cout<< left
<< setw(width) << "Name"
<< setw(width) << "Expiration Date"
<< setw(width - 4) << "Box Size"
<< setw(width + 2) << "Weight"
<< setw(width + 2) << "Storage"
<< setw(width) << "Ship Date"
<< setw(width) << "Price"
<< endl;
}

void printShip(Shipment ship) {
int width = 12;

cout << left
<< setw(width) << ship.name
<< setw(width) << ship.expDate
<< setw(width - 4) << ship.boxSize
<< setw(width + 2) << ship.weight
<< setw(width) << ship.storage
<< setprecision(2) << fixed << right
<< setw(width) << ship.shipDate
<< setw(width) << ship.price
<< endl;
cout.unsetf(ios::fixed);
}

void initializeShipment(Shipment ship[], int arraySize){

for(int a = 0; a < arraySize; a++)
{
ship[a].name = "";
ship[a].expDate;
ship[a].boxSize;
ship[a].weight = 0.0;
ship[a].storage = ' ';
ship[a].shipDate;
ship[a].price;
}
}

void readShipment(ifstream& inFile,Shipment ship[], int arraySize){

for(int i = 0; i < arraySize; i++)
{
inFile >> ship[i].name;
inFile >> ship[i].expDate;
inFile >> ship[i].boxSize;
inFile >> ship[i].weight;
inFile >> ship[i].storage;
inFile >> ship[i].shipDate;
inFile >> ship[i].price;
}
}
Do You really need peek function?

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
#include <iostream>
#include <fstream>

using std::istream;
using std::ifstream;
using std::cout;

struct Date {
unsigned short month;
unsigned short day;
unsigned short year;
};

istream& operator>>(istream& from, Date& to)
{
	char ch1, ch2;
	from >> to.month >> ch1 >> to.day >> ch2 >> to.year;
	if (ch1 != '/' || ch2 != '/') throw "Bad date format";
}

int main()
{
	ifstream f("test");
	Date d;
	f >> d;
}
No I don't, it was the only way I knew how. I tried your way and it works perfectly! Thank you!
Last edited on
Topic archived. No new replies allowed.