Why does it not allow input for name

The code does not allow input for the name? Why exactly?
I am missing something and keep going back and forth.
(its not done yet, I ran into this issue)
The code is blah blahed because of search engines to make it generic.
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()

{
string captain;
int shipid,pods,proton,jetpack,oxygen;

//Intro
cout<<"  Intro to prog"<<endl;
//Allows u to enter this then skips captains name and goes straight to pDDD sales. 
Did I forget to close something? It has been a long day!!!!


cout<<"  Please Enter blahblah ID or -999 to Terminate  ";
cin>>shipid;
// Captain Name is John Smith

cout<<"  Please Enter the Captain's Name  " <<setw(15) << " ";
getline(cin,captain);

//Still playing w setw for aligning texts
	
cout<<"  Please enter Fuel pDDD Sales  " <<setw(15) << " ";
cin>>pods;

cout<<"  Please Enter PP blahblah Sales  ";
cin>>proton;
cout<<"  Please Enter JJJ  Sales  "; 
cin>> jetpack;
cout<<"  Please Enter OXXX Sales  ";
cin>> oxygen;
cout << endl;

cout<<"  Exit Statement blah blah"<<endl;
cout<<"  blahblah"<<setw(6)<<shipid<<endl;
cout<<"  Captain's Name:"<<setw(4)<<captain<<endl;
	
cout<<"  *********************************"<<endl;

system ("PAUSE");

return 0;
}
Last edited on
It does input name. What do you, as user, type after the last digit of the shipid number? Do you hit 'Enter'?

I bet you do. That enter creates a newline. That newline is not consumed by the line 19 and thus lurks there for the unsuspecting getline on line 23. "Up to next newline" is the task description of getline and that is exactly what it does. A very short line.

You have to decide what to do if a newline follows the shipid.
I type anything from 1-999 ( and hit enter) for shipid

(when I run it with visual C++ it doesn't allow you to enter the Captains Name unless I use cin.ignore(); for line 24. Then name part works but you can not input anything else besides shipid and captains name
Last edited on
The line 24 could be:
1
2
3
4
5
6
if ( captain.empty() ) {
  getline(cin,captain);
}
else {
  cout << captain << '\n';
}
That worked, thanks.
Topic archived. No new replies allowed.