char space issue

i am having a problem with my code, because it compiles just fine. the only problem i am having is that when i type in say a full name with space in between the first and last name, it doesn't request for the next variable that it is meant to request for and that just defeats the purpose of the program and how do i store the input into say a file?

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
// Assignment1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"

using namespace std;

class employeeInfo {
	char mobphoneNo[11];
	char name[25];
	char dateOfBirth[10];
	char billingAdd[50];
	char city[25];
	char residencephoneNo[11];
	float amountOut;
	

public:

	void acceptInfo () {

		cout << "What is your phone number? " << endl;
		cin >> mobphoneNo;
		
		cout << "What are your name? " << endl;
		cin >> name;
				
		cout << "What is your date of Birth? MM/DD/YYYY " << endl;
		cin >> dateOfBirth;
		
		cout << "What is your billing address? " << endl;
		cin >> billingAdd;
		
		cout << "What city do you reside in? " << endl;
		cin >> city;
		
		cout << "What is your residencial phone number? " << endl;
		cin >> residencephoneNo;
		
		cout << "How much is outstanding? " << endl;
		cin >> amountOut;
		
	}

	void display() {

		cout << "Name: \n" << name << endl;

		cout << "Date of Birth: " << dateOfBirth << endl;

		cout << "Mobile Phone number: " << mobphoneNo << endl;
		
		cout << "Landline Number: " << residencephoneNo << endl;
		
		cout << "Address: " << billingAdd << endl;
		
		cout << "City: " << city << endl;
		
		cout << "Amount to be paid: $" << amountOut << endl;

	}
};

int main()
{
	employeeInfo aise;

	aise.acceptInfo();
	system("pause");

	aise.display();
	system("pause");
}
Last edited on
Hi,

Instead of using cin>> use gets(), also it will be great if you would use strings instead of char foo[25]

http://www.cplusplus.com/reference/string/string/

PS: Welcome to cplusplus.com
Last edited on
how do i use the get()
its gets()

http://www.cplusplus.com/reference/cstdio/gets/

PS: you have to include stdio header file

PPS: you could also use cin.getline() function

http://www.cplusplus.com/reference/istream/istream/getline/
thank you for all the help thus far but it only works till i input the date of birth then it muddles it up all over again
could you show us your new code???
closed account (48T7M4Gy)
A simple example using ignore() and getline().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main(){
    int count = 0;
    std::string name;
    
    std::cout << "How many names? ";
    std::cin >> count;
    
    std::cin.ignore(); //prepare for following getline()
    
    for( int i = 0; i < count; i++)
    {
        std::cout << "Please enter name: ";
        std::getline(std::cin, name);
        std::cout << name << std::endl;
        
    }
    
    return 0;
}
How many names? 3
Please enter name: Bob Smith
Bob Smith
Please enter name: Harry Roberts
Harry Roberts
Please enter name: Ann Mary Sykes
Ann Mary Sykes
Program ended with exit code: 0
Last edited on
it works fine now. thank you so much. why does it give output like that using cin?
closed account (48T7M4Gy)
http://stackoverflow.com/questions/25475384/when-and-why-do-i-need-to-use-cin-ignore-in-c
closed account (48T7M4Gy)
// http://stackoverflow.com/questions/11835226/program-skips-cin-getline
Topic archived. No new replies allowed.