terminating loop

I am trying to write a simple money exchange program. The following should happen: User enters maximum five currency-exchange rate pairs. If they would like to stop entering currencies they may enter x. It calculates the equivalent value. If the user enter -1 it shod stop calculating.

The progream doesn't go for the next loop and when we press character x nothing happens. Can I have some help about it please?

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
#include <iostream>
#include <string>
using namespace std;

int main(){
  string currency;
  double rate{0};
  double pound{0};
  double resoult{0};
  int counter{1};
  cout << "Enter max.5 currency/ exchange rate for one Pound" << endl;
  while(counter <=5){
  getline (std::cin, currency);
  cin >> rate;
  cout << "\nHow many Pounds do you want to exchange?";
  cin >> pound;
  if(pound == -1){
      break;
  }
  resoult = pound*rate;
  cout << "\nExchanged currency: " << currency
   << " In pounds: " << resoult;
  if(currency == "x"){
      break;
  }
  counter++;
  }
  
}
      
      
If i understand correctly, your testing if currency equals x, but your if statement is way at the bottom, move it under your getline and it should work. the way you have it now, If the user presses x, it will still execute the rest of your cin inputs before reaching the if statement that tests for x being input.
Last edited on
I made some changes, but the problem is from the second loop it doesn`t let the user enter anything, it just makes some calculations and the result is 0. Can I have some help please?

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
#include <iostream>
#include <string>
using namespace std;

int main(){
  string currency;
  double rate{0};
  double pound{0};
  double resoult{0};
  int counter{1};
  
  cout << "Enter max.5 currency/ exchange rate for Pounds" << endl;
  getline (cin, currency);
  cin >> rate;
  
  
  while(currency != "-1" && counter <= 4){ 
  
  cout << "\nHow many Pounds do you want to exchange?";
  cin >> pound;
  resoult = pound*rate;
  
  cout << "\nExchanged currency: " << currency
   << " In pounds: " << resoult;
  
  cout << "\n\nEnter max.5 currency/ exchange rate for Pounds" << endl;
  getline (cin, currency);
  cin >> rate;
  counter++;
  
  }
  
}
      
Registered users can post here. Sign in or register to post.