Problem with sentinels

Hello all @ c++.com, I am a newb to C++ programming and I'm working on a currency conversion program which converts dollars to yen and yen to dollars for my programming class. It is also to read and do the dollar values and convert them to Yen and the reverse later in the program. The issue I am having is getting my sentinel to work notating the end of the dollar inputs. I am not asking for anybody to do my homework for me just asking for some suggestions, etc. Any help would be outstanding. When I debug my program, I was able to input the exchange rates now after adding the sentinel, the program crashes.

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
//This program asks for current exchange rates, 
//then converts the US dollar to Japanese yen.

#include <iostream> 
#include <string>
#include <cmath>

using namespace std;


int main()
{

char choice;
//Declare floating-point variables
//Displays messages to user

string input;
const string sentinels = 0;
int exchange_rates;
int currency;
float dollarsyen = 90.9280;
float yendollars = 0.0110;


cout <<"\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
cout << "Enter todays U.S. Dollars to Yen exchange rates: " "\n";
cin >> exchange_rates;

cout <<"1.Dollars to Yen\n";
cout <<"2.Yen to Dollars\n";

cout <<"Enter the number of the currency you would like to exchange: \n";
cin >> currency;


if (currency ==1)
{
	cout <<"Enter the amount of Dollars you would like to exchange to Yen: \n";
	cin >> dollarsyen;

	cout <<"\nYou have entered " << dollarsyen << " Dollars which is equal to " << dollarsyen * 90.9280 << " Japanese Yen. \n";
    cin >> dollarsyen;
}

if (currency ==2)
{
	cout <<"Enter the amount of Yen you would like to exchange to Dollars: \n";
    cin >> yendollars;
	cout <<"\nYou have entered " << yendollars << " Yen which is equal to " << yendollars * 0.0110 << " Dollars. \n";
}


while (input != sentinels) {

	if (input == "no_currency")  {input++;}
	    
	    cin >> input;
 }

	    		
	if (cin.fail())
	{
		if (cin.eof())
		{
		    // End of file has been resched
		}
		else
		{
			  // Bad input
		}
	}
  
  system("PAUSE");

  return 0;

}
	
First of all, all currency-related values should be stored in real number types (float or double), so that it is not rounded down after conversion.
1
2
int exchange_rates; // NOOOooooo!
                    // The machine ate my 50 cents! 

Secondly:
{input++;} - What is that supposed to do? Remember, that it is string type.

And finally:
1
2
3
4
5
string input;               // It is empty string.
const string sentinels = 0; // Assigning numeric value 0 to a string !!!
//(...)
while (input != sentinels)  // at this point input is unitialized, 
                            // so by default it is empty. The loop will never execute 
Last edited on
on line 19: you must not initialize a string with 0. It's likely misinterpreted as a pointer to a c string and hence it crashes. What's the purpose of such a sentinel anyway?

Since input isn't set to any value before the loop starts it won't do anything. use a do { } while(...); loop

And as JockX stated: what is line 56 supposed to do?
The sentinel is supposed to be 0 is to denote the end of the dollar inputs and the end of the input file is supposed to terminate the second sequence.

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
//This program asks for current exchange rates, 
//then converts the US dollar to Japanese yen.

#include <iostream> 
#include <string>
#include <cmath>
#include <cfloat>

using namespace std;


int main()
{

char choice;

//Declare floating-point variables
//Displays messages to user
int currency;        // Used to initialize currency variable
int exchange_rates; // Used to initialize exchange_rates variable
int a;
int b;
float dollarsyen = 90.76;
float yendollars = 0.0110;


cout << "Enter todays U.S. Dollars to Yen exchange rates: " "\n";
cin >> exchange_rates;

cout <<"1. Dollars to Yen \n";
cout <<"2. Yen to Dollars \n";

cout <<"Please enter the number of the currency you would like to convert: ";
cin >> currency;


if (currency == 1) 
{
   cout <<"Please enter the amount of United States Dolars you would like to convert to Japanese Yen: ";
   cin >> currency; 
   cout << "You have entered " << dollarsyen << "Dollars which is equal to " << dollarsyen * 90.76 << " Yen " << endl;
   cin >> dollarsyen;
} 
else if (currency == 2)
{	 
   cout <<"Please enter the amount of Japanese Yen you would like to convert to United States Dollars: ";
   cin >> currency;
   cout <<"You have entered " << yendollars << " Yen which is equal to " << yendollars * 0.011 << " Dollars " << endl;
   cin >> yendollars;
}
  
 system("PAUSE");

  return 0;

}
Last edited on
I have had to re-code this program and I get errors still and that may be due to MSVS2011 attempting to overwrite the old code not sure. This is what I have done thus far and I am still lost as how to code my sentinel. Have tried several different methods and !!!!!!!!!!!!!!!! Thanks in advance to all those who have helped me thus far in this grueling adventure.

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
//This program asks for current exchange rates, 
//converts the US dollar to Japanese yen then 
//converts Japanese yen to US dollars.

#include <iostream> 
#include <string>
#include <cmath>

using namespace std;

char choice;

void knownexchangerate()    // Choose if current exchange rates known
{
double solve1;             // dollars * current exchange rate = Yen
double dollars;          // amounts in United States Dollars
double exchangerate1;   // current exchange rate

cout <<"Please enter the current United States Dollars to Japanese Yen exchange rate: \n";
cin >> exchangerate1;

cout <<"Please enter the amount in United States Dollars you would like to exchange to Japanese Yen: \n";
cin >> dollars;

solve1 = dollars * exchangerate1;
cout << solve1 <<" Yen. " << "\n";
}
void knownexchangerates()
{
double solve2;            // yen * current exchange rate
double yen;             // amounts in Japanee Yen
double exchangerate2;   // current exchange rate 

cout <<"Please enter the current Japanese Yen to United States Dollar exchange rate: \n";
cin >> exchangerate2;

cout <<"Please enter the amount of Japanese Yen you would like to exchange to United States Dollars: \n";
cin >> yen;

solve2 = yen * exchangerate2;
cout << solve2 <<" Dollars. " << "\n";
}

void unknownexchangerate()
{
double solve1;         // dollars * current exchange rate = Yen
double dollars;       // amounts in United States Dollars
double solve2;       // yen * current exchange rate = Dollars
double yen;         // amounts in Japanese Yen

cout <<"Enter the amount of United States Dollars you would like to exchange to Japanese Yen:\n";
cin >> dollars;
solve1 = dollars * 90.760;   // amounts in United States Dollars * 90.760 exchange rate as of 20130128
cout << solve1 <<" Yen. " << "\n";

cout <<"Enter the amount of Japanese Yen that you would like to exchange to United States Dollars:\n";
cin >> yen;
solve2 = yen * 0.0110;      // amounts in Japanese Yen * 0.110 exchange rate as of 20130128
cout << solve2 <<" Dollars. " << "\n";
}

int main()

{
// Declare variables
int answer;
int x;
bool more = true;

while (more)
{
  cin >> x;
  if (cin.fail())
	  more = false;
  else
  {
	x = 0
  }

  


cout <<"Do you know the current United States Dollar to Japanese Yen currence exchange rates (y/n)? ";
cout <<"1 = Yes or 2 = No \n";
cin >> answer;

if (answer == '1')
   {
   knownexchangerate();
   }
else if (answer == '2')
   {
   unknownexchangerate();
   }
else cout <<"Not a valid answer, Please enter one of the following: 1 or 2: \n";

system("PAUSE");

return 0;

}
I re-coded yet again and I got it working with eof as well I believe. How is end-of-file called anyway? Here is my program:

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

//This program asks for current exchange rates, 
//converts the US dollar to Japanese yen then 
//converts Japanese yen to US dollars.

#include <iostream> 
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

char choice;

int main()
{

do
{
double exchangerate1;
double solve1;
double dollarsyen;

cout <<"Please enter the current United States Dollars to Japanese Yen exchange rate: \n";
cin >> exchangerate1;

cout <<"Please enter the amount in United States Dollars you would like to exchange to Japanese Yen: \n";
cin >> dollarsyen;

solve1 = dollarsyen * 90.760;
cout << solve1 <<" Yen. " << "\n";

cin >> choice;

} while(choice != '0');


double exchangerate2;             
double solve2;
double yendollars; 

cout <<"Please enter the current Japanese Yen to United States Dollar exchange rate: \n";
cin >> exchangerate2;

cout <<"Please enter the amount of Japanese Yen you would like to exchange to United States Dollars: \n";
cin >> yendollars;

solve2 = yendollars * 0.0110;
cout << solve2 <<" Dollars. " << "\n";

// Displays message to user.
cout <<" Good job with this conversion! \n";

//Give user action to close window in order to capture screenshot
//of window without having to run the program in cmd prompt.
cout <<"Press enter to close window!\n";

'fin';

system("PAUSE");

return 0;


}
in this grueling adventure.
Not that bad. You have the right ideas, but you seems to have problems with the order of the thing. I put it all together for you:

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
//This program asks for current exchange rates,
//converts the US dollar to Japanese yen then
//converts Japanese yen to US dollars.

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
double yen_to_dollar_exchangerate = 0.0110;

int answer;
bool more = true;

while (more)
{
cout <<"Do you know the current United States Dollar to Japanese Yen currence exchange rates (y/n)? ";
cout <<"1 = Yes or 2 = No \n";
cin >> answer;

if (answer == 1)
   {
more = false;
cout <<"Please enter the current Japanese Yen to United States Dollar exchange rate: \n";
cin >> yen_to_dollar_exchangerate;
   }
else if(answer != 2)
{
  cout <<"Not a valid answer, Please enter one of the following: 1 or 2: \n";
}
else
more = false;
}

int currency;
do
{
cout <<"0. Exit\n";
cout <<"1. Dollars to Yen \n";
cout <<"2. Yen to Dollars \n";

cout <<"Please enter the number of the currency you would like to convert: ";
cin >> currency;


if (currency == 1)
{
   double dollars;

   cout <<"Please enter the amount of United States Dolars you would like to convert to Japanese Yen: ";
   cin >> dollars;
   cout << "You have entered " << dollars << "Dollars which is equal to " << dollars / yen_to_dollar_exchangerate << " Yen " << endl;
}
else if (currency == 2)
{
   double yen;

   cout <<"Please enter the amount of Japanese Yen you would like to convert to United States Dollars: ";
   cin >> yen;
   cout <<"You have entered " << yen << " Yen which is equal to " << yen * yen_to_dollar_exchangerate << " Dollars " << endl;
}
else if(currency != 0)
{
  cout <<"Not a valid answer, Please enter one of the following: 0, 1 or 2: \n";
}
} while(currency != 0);


// Displays message to user.
cout <<" Good job with this conversion! \n";

//Give user action to close window in order to capture screenshot
//of window without having to run the program in cmd prompt.
cout <<"Press enter to close window!\n";

'fin';

//system("PAUSE");

return 0;


}
Thanks much coder777 for your assistance. I believe I figured out the entire program prior to reading your reply. Thanks again for everybody's help. Here is a copy of the corrected program:

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



//This program asks for current exchange rates, 
//converts the US dollar to Japanese yen then 
//converts Japanese yen to US dollars.

#include <iostream> 
#include <fstream>
#include <stdio.h>
#include <string>
#include <cmath>

using namespace std;

char choice;

int main()
{


FILE * pFile;
long n = 0;
pFile = fopen ("currency.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{

			
	do
	{
		double exchangerate1;
		double solve1;
		double dollarsyen;

		cout <<"Please enter the current United States Dollars to Japanese Yen exchange rate: \n";
		cin >> exchangerate1;

		cout <<"Please enter the amount in United States Dollars you would like to exchange to Japanese Yen: \n";
		cin >> dollarsyen;

		solve1 = dollarsyen * 90.760;
		cout << solve1 <<" Yen. " << "\n";

		cin >> choice;

	} while(choice != '0');

	
	{
		double exchangerate2;             
		double solve2;
		double yendollars; 

		cout <<"Please enter the current Japanese Yen to United States Dollar exchange rate: \n";
		cin >> exchangerate2;

		cout <<"Please enter the amount of Japanese Yen you would like to exchange to United States Dollars: \n";
		cin >> yendollars;

		solve2 = yendollars * 0.0110;
		cout << solve2 <<" Dollars. " << "\n";

		// Displays message to user.
		cout <<" Good job with this conversion! \n";
	 

	//Give user action to close window in order to capture screenshot
	//of window without having to run the program in cmd prompt.
	cout <<"Press enter to close window!\n";

	while (!feof (pFile)) {
		fgetc (pFile);
		n++;
	}
	fclose (pFile);
}
	return 0;
	

}

Last edited on
Topic archived. No new replies allowed.