Hi, I'm trying to create a form for entering data

I am very new to C++ and I've spent three days scouring the internet and testing different ways of getting this done, but I cannot find the answer.

I'm trying to get the program to display the fields that I want with half of them on the left side and half on the right side. All the fields need to be displayed at once, not after entering information. I also want to be able to enter the information directly below each field. I want it to look like this:

                                  ***************
                                  *     Header         *
                                  *  Personal Data  *
                                  ***************
First Name:                                                         Last Name:
<Information to be entered>                                        <Information>
Address:                                                            Phone:
<Information>                                                      <Information>
Email:                                                              Sex:    
<Information>                                                      <Information>
Age:                                                                Department:
<Information>                                                      <Information>
                                          

I realize that the header is displayed wrongly, I can't seem to fix it, but the rest of the output is displayed how I want the program to look.
Any help would be much appreciated!
Last edited on
don't know if i understood that right. you first want to print:
First Name: Last Name:
<Information to be entered> <Information>
Address: Phone:
<Information> <Information>
Email: Sex:
<Information> <Information>
Age: Department:
<Information> <Information>

and then add each information, by selecting the according field?

if that's the case, you can't do that with c++. however you could write First Name = ... in the command prompt.
if you really want to select the fields, I'd create a GUI
Last edited on
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>



using namespace std;
int main () {
	string first_name;
	string last_name;
	string address;
	int phone;
	string email;
	char sex;
	int age;
	string class1;

	
	cout <<setw(52)<<"*************************" <<endl;
	cout <<setw(52)<<"*       Header          *" << endl;
	cout <<setw(52)<<"*  Personal Information *" <<endl;
	cout <<setw(52)<<"*************************" <<endl;
	cout <<setiosflags (ios::left)<<setw(20)<< "First Name:"<<setiosflags (ios::right)<<setw(60)<<"Last Name:\n"; 
    cin >>first_name;
	cout<< "\t \t \t \t \t \t \t \t     "; /I tried using tab spaces here to move where the information is input the last name, it moves, but it skips a line, which is not what I want
	cin >>last_name;
	
	cout<<setiosflags(ios::left)<< "Address:"<<setiosflags (ios::right)<<setw(68)<<"Phone:\n";

	cout<<setiosflags(ios::left)<<"Email:"<<setiosflags (ios::right)<<setw(68)<<"Sex:\n";

	cout <<setiosflags(ios::left)<<"Age:"<<setiosflags (ios::right)<<setw(77) <<"Department:\n";

	system ("PAUSE");
	return 0;
}

It's not very much
Last edited on
I don't need to be able to select the fields, just be able to enter information below them. For example after the fields have displayed, I enter the first name, hit enter, enter the last name and so on.
Is that possible to do in the command prompt?
like this ?

cout << "enter first name";
cin >> firstname;
etc...


and you are missing a "/" in line 26 to start your comment
Last edited on
think i get your problem now. after you enter the first name you start in the next line, but you want to write the last name in the same line too. right?
I get it to output the first name and the last name on the same line, but I can't get it to output all the fields and then allow me to input the data below each field.
@SidneyT123

Maybe this will help. It uses a gotoXY() function to place the input cursor at the location you want.

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
// Data Form.cpp : main project file.
#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;


HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For gotoXY() function
COORD CursorPosition;

void gotoXY(int x, int y);

int main ()
{
	string first_name;
	string last_name;
	string address;
	string email;
	string class1;
	int phone;
	int age;
	char sex;
		
	gotoXY(30,2);
	cout << "*************************";
	gotoXY(30,3);
	cout << "*       Header          *";
	gotoXY(30,4);
	cout << "*  Personal Information *";
	gotoXY(30,5);
	cout << "*************************";

	gotoXY(10,10);
	cout << "First Name:";
	gotoXY(40,10);
	cout << "Last Name:";
	gotoXY(10,13);
	cout << "Address:";
	gotoXY(40,13);
	cout << "Email:";
	gotoXY(10,16);
	cout << "Phone:";
	gotoXY(40,16);
	cout << "Sex: [M] [F]";
	gotoXY(10,19);
	cout << "Age:";
	gotoXY(40,19);
	cout << "Department:";

    gotoXY(10,11);
	cout << "_\b";
	cin >>first_name;

	gotoXY(40,11);
	cout << "_\b";
	cin >>last_name;
	cin.ignore();

	gotoXY(10,14);
	cout << "_\b";
	getline(cin, address); // So a space between address number and street name, stays as one input

	gotoXY(40,14);
	cout << "_\b";
	cin >> email;
	cin.ignore();

	gotoXY(10,17);
	cout << "_\b";
	cin >> phone;

	gotoXY(40,17);
	cout << "_\b";
	cin >> sex;

	gotoXY(10,20);
	cout << "_\b";
	cin >> age;

	gotoXY(40,20);
	cout << "_\b";
	getline(cin, class1);
	cin.ignore();

	system ("PAUSE");
	return 0;
}

void gotoXY(int x, int y) 
{ 

	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition);
}
Thank you SO much!
I saw the gotoXY function but I could not understand how to get it to work.
Why did you have to use getline commands for some and not others?
Also, is the cin.ignore() for ignoring whitespace?
I'm a beginner at programming obviously, sorry if the answer to any of these was obvious.
Thanks again to everyone that tried to help!
@SidneyT123

Why did you have to use getline commands for some and not others?


I put it on lines that would most likely have an input that would contain a space. Otherwise, each time a space is encountered, the next input request would use it as data. Example, for address, you would type the house number, a space, then street name. Even the street name may contain a space, like 'Main Street'. So, without the 'getline', address would contain the house number, then email would contain street name, and if the street name has a space in it, then phone would try to contain the rest of the street name, except phone is an int, and the street name, a string. Either the program crashes, or you'll get some really weird data.

Also, is the cin.ignore() for ignoring whitespace?


No, I was using the cin.ignore() to prevent the next input from accepting the return value from the previous input. May not have been really needed, but it couldn't hurt.

I'm a beginner at programming obviously, ...


Don't worry about it. We all started somewhere. I've only really been using C++ for about 2 years. It won't take too long, and a lot of the ins and outs of the language becomes a lot clearer for you. And then pretty soon, it'll be us asking you how to get our program over obstacles.

Thanks again


You're very welcome..



Thanks for the new idea for some practice. This is how I went about doing it. Honestly there is so many ways you can write this up so choose which one you feel is best or take a little bit of everyones.

Note: I didn't add the code to write to text files yet I will update it tomorrow with the addition for all of those that want to see.


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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
/* This data structure called struct add acts like a database and stores the personal information
** This information will then be save to a binary or text file to be opened later***/
	struct add {
		string fname;
		string lname;
		string address;
		string phone;
		string email;
		string sex;
		int age;
		string department;
	}account;

	/*A void function that displays the information to the viewer. 
	It is a void function because it will not return back a value 
	to later be used or displayed. Also all information display 
	is taken from the data structure*/
	void printAccount (){
		
		cout<<"****************\n";
		cout<<"*    Header    *\n";
		cout<<"* Personal Data*\n";
		cout<<"****************\n";

		cout<<"First name:\t"<<"Last name:\n";
		cout<<account.fname<<"\t"<<account.lname<<"\n";
		cout<<"Address:\t\t\t\t\t"<<"Phone\n";
		cout<<account.address<<"\t"<<account.phone<<"\n";
		cout<<"Email:\t\t\t\t"<<"Sex:\n";
		cout<<account.email<<"\t"<<account.sex<<"\n";
		cout<<"Age:\t"<<"Department:\n";
		cout<<account.age<<"\t"<<account.department<<"\n";

	}

	// This function takes the personal information and sends it off to the data structure
	void AccountInfo(){

		cout<<" Please type your first and last name.";
		cin>>account.fname;
		cin>>account.lname;
		cout<<" Please type in your address.";
		getline(cin,account.address);
		cout<<"Please type in your Phone Number.";
		getline(cin,account.phone);
		cout<<"please type in your email.";
		getline(cin,account.email);
		cout<<" Please type in your gender.";
		cin>>account.sex;
		cout<<" Please type in your age.";
		cin>>account.age;
		cout<<" Please type in your Department";
		cin>>account.department;
	}


int main () {

	string mystr;
	int UpAcc;
	// Introduction screen
	Repeat:
	cout<<"Please choose one of the following choices by typing\n in the number infront of the choices:\n\n";
	cout<<"1.Create a new account.\n";
	cout<<"2.Update Account.\n";
	cout<<"3.Delete exist account\n";
	cout<<"4.Display Current information\n";
	cout<<"5.Leave Account\n";
	cin>>UpAcc;
	
	// These if statements allow the user to select an option
	if(UpAcc==1){
		int compl;
		cout<<" You have decided to create a new account";
		AccountInfo();
		cout<<"Would you like to see the completed form?\n";
		cout<<"1. Yes";
		cout<<"2. No";
		cin>>compl;
		if(compl==1){
			printAccount();
		}
		else {
			cout<<"Thank you for completing the form";
			cin.ignore();
			cin.get();
			exit(0);
		}
	}
	else if (UpAcc==2) {
	}
	else if (UpAcc==3){
	}
	else if (UpAcc==4){
	}
	else if (UpAcc==5){
		string leave;
		cout<<"Are you sure you wish to Leave?\n If you do wish to leave please type\"yes\" or if not please type \"no\"";
		cin>>leave;
		if(leave=="yes") {
			exit (0);
		}
		else {
			goto Repeat;
		}
	}
	else {

		cout<<" I am sorry but you have type an incorrect number. You must choose between 1-5 please try again.\n\n";
		goto Repeat;
	}

	cin.ignore();
	cin.get();
	return 0;
}


There is only 1 problem with this code and that is something really simple to fix just that I don't have to time for it. After you are asked for your first and last name. the program skips address for some silly reason.

I hope you have fun getting it to work.
@whitenite1
Those cin.ignore() commands were necessary indeed, the program kept moving the "_" to the next input location without them!

I added a section to the program for it to display all entered information in a list like so:

first name: *firstname*
last name: *lastname*
etc.

While doing this I discovered that when displaying the department information, two words would not show regardless of what I did. Using the getline command nothing is output for department. Using a cin command only the characters before the space are displayed.

If it matters here is the code I have for displaying the data:
1
2
3
4
5
6
7
8
        cout <<"First Name:"<<first_name<<"\n\n";
	cout <<"Last Name:" <<last_name<<"\n\n";
	cout <<"Address:"<<address<<"\n\n";
	cout <<"Email:"<<email<<"\n\n";
	cout <<"Phone:"<<phone<<"\n\n";
	cout <<"Sex:"<<sex<<"\n\n";
	cout <<"Age:"<<age<<"\n\n";
	cout <<"Class:"<<class1<<"\n\n";


@Stormhawk
Thank you for that alternate approach to doing it, I'll be sure to have a look at it.
@SidneyT123

Add cin.sync(); after the cin >> age; and you should then be able to input the class1 string, and have it printed to screen, afterwards. I tried it, and it worked for me. Hopefully, it'll do the same for you..
Topic archived. No new replies allowed.