string

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
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

// Prototypes
void students();
int exit();

// Students Function
void students()
{
	int students, x = 0, i = 0;
	string name;
	vector<string>Students_Names;

	cout << "\n\nEnter the number of students: ";
	cin >> students;

	// Enter Names
	for (x = 0 ; x < students ; x++)
	{
		cout << "Enter names: ";
		getline(cin , name);
		Students_Names.push_back(name);
		sort (Students_Names.begin(), Students_Names.end());
	}
	cout << "\n\n";

	for ( i = 0 ; i < students ; i++)
	{
		cout << Students_Names[i];
		cout << endl;
	}
	_getch();
}
// Exit Function
int exit()
{
	cout << "\n\nThis program is exiting.";

	return (0);
}
// Main Function
int main()
{
	int choice;

	cout << "This will display student names in alphabetical order by first name.\n\n";

	do
	{
		cout << "--------------------" << endl;
		cout << "|                  |" << endl;
		cout << "|                  |" << endl;
		cout << "| 1. Students      |" << endl;
		cout << "| 2. Exit          |" << endl;
		cout << "|                  |" << endl;
		cout << "|                  |" << endl;
		cout << "--------------------" << endl;

		cout << "\n\nEnter Choice: ";
		cin >> choice;

		while ( choice < 1 || choice > 2)
		{
			cout << "\n\nPlease re-enter choice, it must be 1 or 2.\n\n" << endl;

			cout << "--------------------" << endl;
			cout << "|                  |" << endl;
			cout << "|                  |" << endl;
			cout << "| 1. Students      |" << endl;
			cout << "| 2. Exit          |" << endl;
			cout << "|                  |" << endl;
			cout << "|                  |" << endl;
			cout << "--------------------" << endl;

			cout << "\n\nEnter Choice: ";
			cin >> choice;
		}
		switch ( choice )
		{
			case 1: students(); break;
			case 2: exit(); break;
		}
	} while ( choice !=2 );
	_getch();

	return 0;
}


Is there a way, when using cin with a string instead of a char, am i able to make it so a first and last name can be entered. Like using cin.ignore.

Right now i can only enter a first name but because of using the space key i cant enter a last name.

If i am able to use cin.ignore, can you tell me what the space key number is.

Any ideas/suggestions?
try using getline(cin, string)
The integer value of a space is ' '. A char (as denoted by the single quotes) is an integer value.

The ASCII value of ' ' happens to be 32, but that's harder to remember and recognize in the code. Also, you might be working in hex values, in which case the value is 0x20. And someday you might work on a system that doesn't use ASCII, and then the space character might not be 32. If you want to use the value of the space, then use the space character itself.
Topic archived. No new replies allowed.