Using a function to read a single input line into the string s.... much confused.

The function getline(cin, s) read a single input line into the string s. Write a program that uses getline to read input of the following exact form:
FNAME first name
MNAME middle name
LNAME last name
ADDRESS a street name and house number
CITY a city name
STATE a state abbreviation
ZIP a zip code
ORDER_DATE the date of the order
TOTAL_ITEMS number of items (an integer)
ORDER_TOTAL total amount of order (type double)
SHIP_DATE date of shipment
Then print out the top part of a shipment manifest in the following form:
John Q. Public
11801 Jones Valley Ct.
Pleasant Hills, MN 66504
=====================================================================x
Order Date: 10-Oct-2001 Total Items: 10 Order Total: $100.00
Ship Date: 20-Oct-2001





HERE IS MY CODE:


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
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main()
{
  	
    string f_name, m_name, l_name, add1, add2, add3, city, state, zip, order_date, total_items, order_total, ship_date;
    
    cout << "Enter first name ";
    getline(cin, f_name);
    cout << "Enter middle initial ";
    getline(cin, m_name);
    cout << "Enter last name ";
    getline(cin, l_name);
    cout << "Enter address 1 ";
    getline(cin, add1);
    cout << "Enter add2 ";
    getline(cin, add2);
    cout << "Enter add3 ";
    getline(cin, add3);
    cout << "Enter city ";
    getline(cin, city);
    cout << "Enter state ";
	getline(cin, state);
    cout << "Enter zip ";
    getline(cin, zip);
    cout << "Enter order date ";
    getline(cin, order_date);
    cout << "Enter total items ";
    getline(cin, total_items);
    cout << "Enter order total ";
    getline(cin, order_total);
    cout << "Enter ship date ";
    getline(cin, ship_date); 
    
    cout << f_name + " " + m_name + " " + l_name << endl << add1 + " " + add2 + " " + add3<< endl << city << state << zip << endl << "Order Date:" << order_date + " " << "Total Items:" 
        << total_items + " " << "Order Total:" << order_total << endl << "Ship Date:" << ship_date << endl;
    
    return 0;
    
}



It works with the output:


John Q. Public
11801 Jones Valley
PleasantHillsMN66504
Order Date:10-Oct-2001 Total Items:10 Order Total:$100
Ship Date:20-Oct-2001


But seems a bit long, Need help simplifying!!
Last edited on
It's A High level language...
Please edit your post and use code tags - the <> button on the right.

The problem is to read from a file, not the keyboard.

With you cout statement, you can build the output over multiple cout statements - no need for 1 huge long statement.

1
2
3
4
5
6
cout << f_name + " " + m_name + " " + l_name << endl;
cout  << add1 + " " + add2 + " " + add3<< endl;
cout  << city << state << zip << endl;
cout  << "Order Date:" << order_date + " " << "Total Items:"
cout  << total_items + " " << "Order Total:" << order_total << endl ;
cout << "Ship Date:" << ship_date << endl;

Last edited on
New code!


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
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main()
{
  	
    string f_name, m_name, l_name, address, city, state, zip, order_date, total_items, order_total, ship_date;
    
    cout << "Enter first name ";
    getline(cin, f_name);
    cout << "Enter middle initial ";
    getline(cin, m_name);
    cout << "Enter last name ";
    getline(cin, l_name);
    cout << "Enter address ";
    getline(cin, address);
    cout << "Enter city ";
    getline(cin, city);
    cout << "Enter state ";
	getline(cin, state);
    cout << "Enter zip ";
    getline(cin, zip);
    cout << "Enter order date ";
    getline(cin, order_date);
    cout << "Enter total items ";
    getline(cin, total_items);
    cout << "Enter order total ";
    getline(cin, order_total);
    cout << "Enter ship date ";
    getline(cin, ship_date); 
    
    cout << f_name + " " + m_name + " " + l_name << endl;
	cout  << address << endl;
	cout  << city << state << zip << endl;
	cout  << "Order Date:" << order_date + " " << "Total Items:";
	cout  << total_items + " " << "Order Total:" << order_total << endl;
	cout << "Ship Date:" << ship_date << endl;
    
    return 0;
    
}

TheIdeasMan wrote:
The problem is to read from a file, not the keyboard.


The mention of the getline function implies reading the info from a text file, otherwise you just use cin.

I would create a class to store the info into, and a vector to put the class objects into. That way you can read multiple item from the file not just one only.

If you create a class, then you will have to think about some sensible functions to have. You will need some constructors to set the data, and a PrintData function to output the info. Don't have a get & set function for each member variable.

With variables names, I prefer to have names like TotalItems, OrderTotal, ShipDate rather than total_items, order_total, ship_date. If you put them into a class then pre-pend them with m_ so that you can see they are member variables - this is normal practice.

Technically, the relationship between a Customer & Orders is a one to many, which implies having a class for each of these, but for now you can get away with putting it all into one class.

HTH Good Luck
Topic archived. No new replies allowed.