A program that collects users information Question

Here is my code it complies but when it asking the question it work all the way up to when it ask you for your Address, then it skips city, and then it goes on then skips email address.. Then ask if you have an occupation but whatever answer you give it, it just skip to return( 0 );

thanks

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <windows.h>

using namespace std;

int main( )
{
    SetConsoleTitle( ( "Information" ) );

    // Declare variable*************************************************
    string fullName;
    string streetAddress, city, state;
    string emailAddress;
    string occupation; // job

    int zipCode;
    int birthYear; // the year he/she was born
    int age; // stores there age

    double heightInches; // there hight in inches
    double currentWeight;

    char personSex;
    char fileName[20]; // will make a text file to put all the information in
    char haveOccupation; // test to see if the person has an occupation
    // end variable *****************************************************

    ofstream outFile;

    cout << "Enter a text file name to store information\n";
    cout << "example (example.txt): ";
    cin >> fileName;

    outFile.open( fileName );

    cin.ignore( 20, '\n' );

    /****************************
    information about the person
    ****************************/

    cout << "Full name: ";
    getline( cin, fullName );

    cout << "Year of birth example (1991): ";
    cin >> birthYear;

    age = ( 2010 - birthYear );

    cout << "Sex(m/f): ";
    cin >> personSex;

    cout << "Height(inches): ";
    cin >> heightInches;

    cout << "Weight(lb): ";
    cin >> currentWeight;

    /********************************
    end information about the person
    *********************************/

    cout << endl << endl;

    /************************************************************
    Starts information about were they adress / occupation / email
    ************************************************************/

    cout << "Street address: ";
    getline( cin, streetAddress );

    cout << "City: ";
    getline( cin, city );

    cout << "State: ";
    getline( cin, state );

    cout << "zip code: ";
    cin >> zipCode;

    cout << "Email: ";
    getline( cin, emailAddress );

    cout << "Do you have an occupation(y/n): ";
    cin >> haveOccupation;

    outFile << "Name  : " << fullName << endl;
    outFile << "Age   : " << age << endl;
    outFile << "Sex   : " << personSex << endl;
    outFile << "Height: " << heightInches << endl;
    outFile << "Weight: " << currentWeight << endl;
    outFile << endl << endl;
    outFile << "Address: " << streetAddress << endl;
    outFile << "City  : " << city << endl;
    outFile << "State : " << state << endl;
    outFile << "zip   : " << zipCode << endl;
    outFile << "Email : " << emailAddress << endl;

    if ( haveOccupation == 'y' || haveOccupation == 'Y' )
    {
        cout << "What is your occupation: ";
        getline( cin, occupation );

        outFile << "Occupation: " << occupation << endl;
    }

    /*************************************************
    End of information about adress/occupation / email
    **************************************************/

    outFile.close( );
    return( 0 );
}
FAQ: http://www.cplusplus.com/forum/articles/6046/

You are using both the stream input operator and getline. Choose one or the other.
If I have to choose one or the other then how can you make the kind of program I am trying to make?
Change all your datatypes to string and use getline().

For one thing, ZIP codes are not ints. They are strings. There is no three of four-digit ZIP code, but there are ZIP codes that start with "00" and "0".

Single character input works well as a string.

And for numerics, you can convert the strings to numeric values like so:

1
2
3
4
5
6
7
int to_int(const std::string& s)
{
    std::istringstream tmp(s);
    int result;
    tmp >> result;
    return result;
}


Or use boost::lexical_cast.
Ok I got my program to work using something else but I was wounding is it a bad thing that I'm using
1
2
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );


so much ?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <windows.h>

using namespace std;

int main( )
{
    SetConsoleTitle( ( "Information" ) );

    // Declare variable*************************************************
    string fullName;
    string streetAddress, city, state;
    string emailAddress;
    string occupation; // job

    int zipCode;
    int birthYear; // the year he/she was born
    int age; // stores there age

    double heightInches; // there hight in inches
    double currentWeight;

    char personSex;
    char fileName[20]; // will make a text file to put all the information in
    char haveOccupation; // test to see if the person has an occupation
    // end variable *****************************************************

    ofstream outFile;

    cout << "Enter a text file name to store information\n";
    cout << "example (example.txt): ";
    cin >> fileName;
    
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    outFile.open( fileName );

    /****************************
    information about the person
    ****************************/

    cout << "Full name: ";
    getline( cin, fullName );
  
    cout << "Year of birth example (1991): ";
    cin >> birthYear;
    
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    age = ( 2010 - birthYear );

    cout << "Sex(m/f): ";
    cin >> personSex;
    
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
    

    cout << "Height(inches): ";
    cin >> heightInches;
    
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    cout << "Weight(lb): ";
    cin >> currentWeight;
    
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    /********************************
    end information about the person
    *********************************/

    cout << endl << endl;

    /************************************************************
    Starts information about were they adress / occupation / email
    ************************************************************/

    cout << "Street address: ";
    getline( cin, streetAddress );

    cout << "City: ";
    getline( cin, city );

    cout << "State: ";
    getline( cin, state );

    cout << "zip code: ";
    cin >> zipCode;
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    cout << "Email: ";
    getline( cin, emailAddress );

    cout << "Do you have an occupation(y/n): ";
    cin >> haveOccupation;
    
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    outFile << "Name  : " << fullName << endl;
    outFile << "Age   : " << age << endl;
    outFile << "Sex   : " << personSex << endl;
    outFile << "Height: " << heightInches << endl;
    outFile << "Weight: " << currentWeight << endl;
    outFile << endl << endl;
    outFile << "Address: " << streetAddress << endl;
    outFile << "City  : " << city << endl;
    outFile << "State : " << state << endl;
    outFile << "zip   : " << zipCode << endl;
    outFile << "Email : " << emailAddress << endl;

    if ( haveOccupation == 'y' || haveOccupation == 'Y' )
    {
        cout << "What is your occupation: ";
        getline( cin, occupation );

        outFile << "Occupation: " << occupation << endl;
    }

    /*************************************************
    End of information about adress/occupation / email
    **************************************************/

    outFile.close( );
    return( 0 );
}

Topic archived. No new replies allowed.