the use of getline

1
2
3
4
5
string name[30];
cout << "Please enter a  name?  << endl;
	
cin.getline(name[i], 30);
 


i have a problem with this piece of code. i want to make sure that when the inputs their name that it captures the space as well. i am not sure what to do
Last edited on
Line 1 creates an array of 30 strings, not a string of size 30. Standard strings are not limited in size (except for available memory, of course), so you don't need to make sure that getline() produces an overflow.
Use this, instead:
1
2
std::string s;
std::getline(std::cin,s);
I don't see how the original code would compile. Aside from the missing end quote, that version of getline shouldn't compile in that way. Looks like helios fixed that.

With regards to getline, it will capture the spaces. That's why the function is named getLine. The default delimiter is the '\n' character. It will read everything into the string up to the carriage return. It is designed to get the entire line and not just the text up to the first whitespace encountered.
can u give me and example. my brian is moving so slow right now.
the problem i get is when i input: star wars. i just get the first letter which is s.
how do i change that
Post your code.
the bold is where my probelm is.
i want to capture the user input if the user inputs star wars instead of starwars it accepts it as well.


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

#include "stdafx.h"
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::cin;
using std::getline;

int main(int arg[])
{
	
	int n = 0;
	string size;
	int capa[100];
                char name[30];	
               char userchoice = 'y';
	int sold[100];
	int i=0;
    int index;
	while(userchoice == 'y'){
	cout << "Hello, Welcome to the top entertainment movie purchase services:" << endl;
	cout << "Please enter a Movie name? Please indicate when you want to stop typing with a %" << endl;
	
	cin.getline(name[i], 29);
	

	//cin.ignore();
	cout << "The Movie that you entered was: " << name[i] << endl;
	cout << "What is the ticket capacity for this movie?" << endl;
	cin >> capa[i];

	cout << "The movie capacity for this movie is: " << capa[i] << endl;

	cout << "The number of tickets that are sold for this movie are :" << endl;
	cin >> sold[i];

		cout << " Would you like to add additonal Information? [Y/N]" << endl;
        cin >> userchoice;
         i= i++;

	}    
        
		for( int j = 0; j < i; j++ ) {
       
		cout << "Movie Name:  " << name[j] << endl;
		cout << "Movie Capacity: " << capa[j] << endl; 
		cout << "Movie Tickets: " << sold[j] << endl;
		}
	   

     cout << "Thank you using the top entertainment movie Purchases Program!!!!!" << endl;
	 cout << "Have a nice day" << endl;
	 cout << "Bubye" << endl;
	 cout << ":-p" << endl;
   system("PAUSE");
	return 0;
}

Change line 26 to cin.getline(name, 29);

You can't store a string in one character.

When you are using an array of characters for input you use it like a normal variable, not an array.
Last edited on
but when i do that and if i input star wars i get s as my output.
The reason is that you are only outputting 1 character. Change line 30 to the following:

cout << "The Movie that you entered was: " << name << endl;


If you specify name[i] then you are only going to output one character.

In this code, it is not necessary to tell the user to use a % sign. Getline gets until the user presses enter and then discards the carriage return. The user can just type and press enter when finished.

cout << "Please enter a Movie name? Please indicate when you want to stop typing with a %" << endl;

It is better if you follow helios' advice and use a std::string. Any user entry greater than 29 characters will overflow the character array and cause major problems.
Topic archived. No new replies allowed.