| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Cin & endless loop
Hello,
Im writing a little program to better my knowledge of c++ and im having a little bit of difficulty. Code:
//_____________________________________________
// |Program: pie |
// |Date: 12/25/2004 |
// |File: main.cpp |
// |Info: Explore different ways of finding pie |
// |____________________________________________|
#include<iostream>
using namespace std;
// setting variables
int input;
main() {
// while user input != 3 loop program
while(input!=3){
// prints menu
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
cout << "+ How Would you like your pie +" << endl;
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
cout << "+ 1) By the diameter +" << endl;
cout << "+ 2) By the circumference +" << endl;
cout << "+ 3) I dont want pie +" << endl;
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
// reset input
input = 0;
// gets user input
cin >> input;
cin.clear();
// checks input
if (input == 1){
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
cout << "+Enter the diameter +" << endl;
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
}
else if (input == 2) {
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
cout << "+Enter the circumference +" << endl;
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
}
else if (input == 3) {
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
cout << "+Good Bye! :-) +" << endl;
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
}
else {
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
cout << "+We only accept numbers 1-3 +" << endl;
cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
}
}
// making os happy
return 0;
}
For some reason when ever you input something different than a number it just keeps looping(without asking for input) and i have to control c to get out. Anyone know why? \ thanks, jordan |
|
#2
|
|||
|
|||
|
yeah, this happens when you input something other than an int when you are trying to get an int using cin.
|
|
#3
|
|||
|
|||
|
Quote:
The user is taking another number . He is giving an integer as input. Hence i do no agree with your explaination. |
|
#4
|
|||
|
|||
|
Quote:
The explanation, in my opinion, was excelent. I don't know if true....but accurate! |
|
#5
|
|||
|
|||
|
hey jordan,
just add the following code after cin.. if(!input) break; now it works.. baba.. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Cin & endless loop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|