+ 3

Basic C++ cin compiler problem

Hi lovely community <3. I trying to use a function and i got a litle problem, the compiler in code blocks only ask me the first CIN, if i answer both CIN in the first one the programm work fine, but i want that compiler make " Question --> answer , Question--> Answer " and NO "Question--> Answer, Answer, Question " sorry my english, doing my best. #include <iostream> using namespace std; char compra_libro(double precio_libro,double dinero_bolsillo); int main() { int precio_libro, dinero_bolsillo; char compra; cout<<"introduzca el precio del libro:\n"; //Here I enter the price of the book cin>>precio_libro; cout<<"introduzca su presupuesto:\n"; //Enter the money i got BUT the compiler jump the second cin cin>>dinero_bolsillo; cout<<endl<<endl; compra= compra_libro(precio_libro,dinero_bolsillo); if (compra=='A') { cout<<"la compra es posible de realizar";} else {cout <<"la compra no se puede realizar";} return 0; } char compra_libro(double precio_libro,double dinero_bolsillo) { while (dinero_bolsillo >= precio_libro) return 'A'; }

10th Feb 2017, 7:31 PM
Yuriy az
Yuriy az - avatar
7 Respostas
+ 6
I would like to direct more of everyone's attention towards this thread. AFAIK, @Yuriy is trying to say that the program is skipping the second cin, but if two input is provided for the first cin, the program works. Based on the code provided, I can't seem to identify why the program does this. One thing I can think of is user input error.
11th Feb 2017, 5:06 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
@Kenyatta I understand about clearing the stream, but why do some programs run OK with similar input counts whilst we need to clear the input stream in this case? What are the differences?
11th Feb 2017, 6:53 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Thx Ace and Hatsy. All this is still new and confusing for me. I used cin.clear(); //after first cin and all worked fine.
11th Feb 2017, 3:58 PM
Yuriy az
Yuriy az - avatar