Using user inputed date later in code

So I Have a code
1
2
3
4
5
6
7
int nName
int nYear
cout << "Please enter your last name: ";
cin >> nName

cout << "Please enter a year: "
cin >> nYear


Then what i want to be able to do is use the above like that :
cout << "Hello Major" << (nName here) << "It is Year" << (nYear here)

Now i tryied :
cout << "Hello Major " << (nName) << "It is Year " << (nYear) <<
and
cout << "Hello Major " << nName << "It is Year " << nYear <<

But none worked also when i run the above code:
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

//This Part of The Code will ask user his data for later use

int main (int nNumberofArgs, char* pszArgs[])
{

int nYear;
int nName;
cout << "Please Enter your last name: ";
cin >> nName;
cout << "Please Enter a number between 2300 - 3000: ";
cin >> nYear;

//This Part of the code explains the storyline to the user

cout << "Hello Major " << nName << "It is Year " << nYear <<
cout << "Lt.Kowalski : Sir what the ship is talking damage from the morgs what should we do?";

system ("PAUSE");
return 0;

}



And get this

http://img96.imageshack.us/i/adek.jpg/

also the program asks for the name and then displays the result without askign for the year , if someone could help me it would be apprecieated



Your trying to enter a string into an int data type. Use a string instead. http://www.cplusplus.com/reference/string/
Last edited on
sorry... friend..., im beginer too, but this is what can i do to solve your froblem
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
#include <iostream>
#include <cstdlib>
using namespace std;

//This Part of The Code will ask user his data for later use

int main (int nNumberofArgs, char* pszArgs[])
{	
	char nName[20];
	int nYear;
	cout << "Please Enter your last name: ";
	cin >> nName;
	cout << "Please Enter a number between 2300 - 3000: ";
	cin >> nYear; //but this will cause an error if user enter alphabet number
	
	//This Part of the code explains the storyline to the user
	system("cls");
	
	cout << "Hello Major " << nName <<endl<<"It is Year " << nYear<<endl;
	cout << "Lt.Kowalski : Sir what the ship is talking damage from the morgs";
	cout <<endl<< "what should we do?";
	
	cout<<endl<<endl;
	system ("PAUSE");
	return 0;
}

but cin<<nYear will goes wrong if user hit alphabet number soo you need to check input from user, i use this to check
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
#include <iostream>
#include <cstdlib>
#include <sstream>
using namespace std;

bool check_digit(char string[])
{
	if(strlen(string)>0)
                  {     
                  cout<<endl<<"----------start checking---------------"<<endl;            
                  for(int i=0;i<strlen(string);i++)            
                           {                 
                            if(!isdigit(string[i]))                 
                                    {        
									 cout<<endl<<"dont use Alphabet on it"<<endl;          
                                     return false;               
                                     }            
                           }        
                  }        
                   else       
                  { 
				  cout<<endl<<"dont use Alphabet on it";
                  return false;        
                   }    
          return true;    
}

//This Part of The Code will ask user his data for later use

int main (int nNumberofArgs, char* pszArgs[])
{	
	char nName[20],charyear[6];
	int nYear;
	cout << "Please Enter your last name: ";
	cin >> nName;
	do
	{
		cout << "Please Enter a number between 2300 - 3000: ";
		cin >> charyear;	
	}while(!check_digit(charyear));
	stringstream(charyear)>>nYear; //convert from char to int;
	
	//This Part of the code explains the storyline to the user
	system("cls");
	
	cout << "Hello Major " << nName <<endl<<"It is Year " << nYear<<endl;
	cout << "Lt.Kowalski : Sir what the ship is talking damage from the morgs";
	cout <<endl<< "what should we do?";
	
	cout<<endl<<endl;
	system ("PAUSE");
	return 0;
}

wish you luck
Topic archived. No new replies allowed.