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
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:
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
#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;
}
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.