Problem with input

Let says my defined an input as an integer but i key in an character. How can i convert it into integer?
define the input as a character then convert it to an integer.
What do you mean?

When your program asks for an integer and the user enters "N", then the user has not obeyed the instruction given him -- and the input should be considered invalid.

If you mean: how do you convert a string like "123" to the integer value 123, then that's a different thing. C++ streams are designed to do that for you.
1
2
3
4
5
istringstream oss( "123" );
int n;
oss >> n;
if (!oss) fooey();
else      yeah();


[edit] bug fix
Last edited on
HOw ? Use wat to convert it to int ?
use atoi function.
I think that is in stdlib.h
1
2
3
4
char c_input[64]; //input with max of 64 characters.
int i_input;
cin >> c_input;
i_input = atoi(c_input);
Yea. Lets say the user does not obey the instructions . He key in the a character as he should key in an integer. BUt my program jammed ad after he key in the character . HOw to resolve it ?
but i am using it for a switch function. can it be used for it?
i guess it could, but why?

use this to check if the input is numeric.
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
I not really understand about it. For example, if i want the user key in 1 to 4 only. but if the user key in a character. how can i avoid it make=ing my program jammed ?
I've already given you the answer. Here it is again:
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  int n;

  cout << "Please enter a number in 1 through 4> " << flush;
  while (true)
    {
    // Read the user's input
    string s;
    getline( cin, s );

    // Get rid of any trailing whitespace
    s.erase( s.find_last_not_of( " \t\v\n\r\f" ) + 1 );

    // Convert the user's input into a number, if possible
    istringstream ss( s );
    ss >> n;

    // If conversion worked, we're done
    if (ss.eof() && (n >= 1) && (n <= 4)) break;

    // Else conversion failed. Complain to user.
    cout << "Please, enter 1, 2, 3, or 4> " << flush;
    }

  cout << "Good job, you entered " << n << ".\n";
  return 0;
  }

Seriously, I answer this very question about once a week.

Hope this helps.

[edit] Fixed to handle the additional constraint given in the last post
Last edited on
oke

to saferty input
i using
1
2
3
4
5
6
7
int i_input;
while( cin >> i_input)
{
       cout << "\n please input a integer number! " << endl;
       cin.clear();
       cin.ignore();
}
Hey, tridung9x, did you actually compile and try that? (I know you didn't.)
Thank you very much Duoas. It works.
Duoas. Another problem comes out. How about i put it in a for loop ? It does let to key in again ? How to do it ?
Why i cant go into the while loop again when it is under a for loop. Means when the for loop loops second time. It cant go into while loop. Why like this ? Anything need to adjust ?
There is no reason why you can't.
Post your 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
void main()
{
	passenger Seating[240],plane[12];
	passenger S1;
    int i,z,a,k=300,row,choice2;
	char column,choiceone;
	char character[100];
	string depart;
	string arrive;
	time_t start;	//declaring start as a time function
	time (&start);

	for(a=1;a<=12;a++)
	{
		plane[a].seatplan();			//Set the seat plane for each plane
	}

	for (i=1;i<=100000;i++)
	{
		
		S1.menu(); //Display the main manu
		cout<<"Which process you want to proceed <1-4>?"<<endl;		// choosing which process would like to be proceed
		while (true)
		{
		  // Read the user's input
		    string s;
		    getline( cin, s );

		   // Get rid of any trailing whitespace
			s.erase( s.find_last_not_of( " \t\v\n\r\f" ) + 1 );

			// Convert the user's input into a number, if possible
			istringstream ss( s );
			ss >> choice2;

		  // If conversion worked, we're done
			if (ss.eof() && (choice2 >= 1) && (choice2 <= 4)) break;
	
		  // Else conversion failed. Complain to user.
			cout << "Please, enter 1, 2, 3, or 4> " << flush;
			 }



		switch (choice2)
		{
		case 1:		for (z=13;z>=12;z--)
					{
					Seating[i].Destination_list();
					a=Seating[i].decision();
					z=a;
					}
					cout<<"Please wait a while......"<<endl;
					S1.loading(3);			//hold the program for 3 seconds
					system ("cls");			// clear the screen
					cin.ignore(1000,'\n');
					cout<<"Please enter your details."<<endl;
					cout<<"Name                       : ";
					Seating[i].Setname();		//customers enter their names
					cout<<"Nationality                : ";
					Seating[i].Setnation();		//nationality
					cout<<"Identity Card/Passport NO. : ";
					Seating[i].Seticno();								//IC no./Passport no.
					cout<<"Address                    : ";
					Seating[i].Setaddress();						//address
					cout<<endl;
					Seating[i].select_seat();					//customer select their desired seats
					row=Seating[i].Getrow();					//Get the row
					column=Seating[i].Getcolumn();				//and column chosen
					plane[a].show_seat(row,column);				//pass the row and column into function by reference using pointer
					cout<<"Please check the details below are correct or not."<<endl;
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;		//prints
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;	//out
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;		//the info
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;	//entered
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"If correct, press <Y>. If wrong, press <N>. "<<endl; //confirm the info entered is correct or not
					cin>>choiceone;
					choiceone = static_cast<char>(toupper(choiceone));	//column is converted to uppercase and a temporary copy of type char is made
					if (choiceone == 'Y')
					{
						cout<<"Seat reserved at "<<ctime (&start);	//Set the reservation time
						Seating[i].Settime(ctime (&start));			//for identical customer
						cout<<"Your Reservation Reference No. : "<<i<<endl;
						cout<<"Please settle your payment within 1 month. "<<endl;
						cout<<"If not, your reservation will be cancelled automatically."<<endl<<endl;
						cout<<"Press any character to continue. ";
						cin>>character;

					}
					else
					{
						i--;
					}break;
		case 2:		cout<<"Enter your reference no."<<endl;				//customers check their reservation 
					cin>>i;												//using the provided reference after the
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;	//reservatiion made
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"Seat reserved time         : "<<Seating[i].Gettime()<<endl<<endl;
					cout<<"Press any character to continue. ";
					cin>>character;
					break;
		case 3:		S1.Destination_list();			//Allow the customers to check the seats availability
					a=S1.decision();				//for their desired flight
					plane[a].show_seat(k++,k++);
					cout<<"Press any character to continue. ";
					cin>>character;
					break;
		case 4:		i+=100050;			// exits the system
					break;
		default:	i--;
					cout<<"Wrong Input. Please Select again. ";
					break;
		}
		S1.loading(2);			//hold the system for 2 seconds before proceed to next line
		system ("cls");			//clear the system screen
	}
}

This is my main().
I writing this using classes.
It becomes like this during the second loop.
              WELCOME TO UNIMAS AIRLINES RESERVATION SYSTEM

           @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
           @    1. Reserve a seat.                           @
           @    2. Check reserved seat.                      @
           @    3. Check seat availablity.                   @
           @    4. Exit.                                     @
           @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Which process you want to proceed <1-4>?
Select your departure location.
1. Kuala Lumpur (LCCT)
2. Johor Bharu (JHB)
3. Kuching (KCH)
4. Kota Kinabalu (BKI)


There is no input for me to keep to choose the process.
Topic archived. No new replies allowed.