Display currencies in a table

Hey!
I need some help understanding whats wrong with my code.
The program is supposed to read from a textfile, sort the currencies in alpabethic order and write a table. Then it should ask user for a new currency from the table and write a new table. Here is the problem. It writes the table and the new currencies but then continue to write out more numbers "rubbish" after. How do i get it to stop?
Greatful for som help, heres my 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <fstream>
#include <string>
#include "Currency.h"
using namespace std;

/***************************************
        Function declarations
***************************************/
//Sort alphabetically array V with n currencies
//currency designation is used for sorting
void sort(Currency V[], int n);

//Read a text file f of currencies and
//Load the currencies into array V
int read_from_file(ifstream& f, Currency V[], int n);

//Display all currencies in V to the screen
void display_DB(Currency V[], int n);

//Read user input and validate
int request(Currency V[], int n, string user_input);

void ask_user(Currency V[], int n);

//Print new table and change currency
void change_currency (Currency V[], int n, int j);


int main()
{
    ifstream in_file("currencies.txt"); //file to read
    const int SIZE = 100;
    Currency DB[SIZE];
    string user_input;

    //Test if opening the file is succeeded
    if ( !in_file.is_open())
    {
        cout << "Error in file opening" << endl;
        return 0; // end the program
    }

    //Load a file of movies into array DB
    int howMany = read_from_file(in_file, DB, SIZE);

    if (!howMany)
    {
        cout << "File is empty!!" << endl;
        return 0;
    }

    //Sort array DB by movies name
    sort(DB, howMany);

    //Display the sorted movies in array DB to the screen
    display_DB(DB, howMany);

    //Read and validate user input
    ask_user(DB, SIZE);

    //Display new table and change currency
   change_currency(DB, SIZE, howMany);

    //close the files
    in_file.close();

    return 0;
}

/***************************************
        Function definitions
***************************************/
//Sort alphabetically array V with n currencies
//currencies designation is used for sorting
void sort(Currency V[], int n)
{
    //Bubble sorting
    for ( int pass = 1; pass <= n - 1; pass++ ) // passes

        for (int i = 0; i <  n - 1; i++ )      // one pass
        {
            //use operator>
            if ( V[ i ] > V[ i + 1 ] )
            {
                //swap
                Currency c = V[ i ];
                V[ i ] = V[ i + 1 ];
                V[ i + 1 ] = c;
            }
        }
}

//Read a text file f of currencies and
//Load the currencies into array V
int read_from_file(ifstream& f, Currency V[], int n)
{
    Currency c;
    int counter = 0;

    get(f, c); //read first currency

    while ( f ) //test if stream is in good state
    {
        V[counter] = c; //store currency in V
        counter++;      //increment the counter

        //all n slots of array V are occupied
        if (counter == n) break;

        get(f, c);   //read next currency
    }

    return counter;
    return 0;
}

//Display all currencies in V to the screen
void display_DB(Currency V[], int n)
{
    cout << "Currency " << setw(10) << "Value in Euro " << setw(10) << "Name " << endl;
    cout << "====================================" << endl;
    for(int i = 0; i < n; i++)
    {
        //Display currencies
        put(V[i]);
    }
}
int request(Currency V[], int n, string user_input)
{
    for (int i = 0; i < n; i++)
    {
        if (V[i].designation == user_input)
            return i;
    }
    return n;
}

void ask_user(Currency V[], int n)
{
    string user_input;
    for (int i = 0; i < n; i++)
    {
        cout << "Enter another currency: ";
        cin >> user_input;

        if(user_input == "quit")
            break;

        int v = request(V, n, user_input);
        if (v == n)
            cout << "Non-existing currency! " << endl;
        else change_currency(V, n, v);
    }
}

void change_currency(Currency V[], int n, int j)
{
    cout << "Currency " << setw(10) << "Value in " << V[j].designation << setw(10) << "Name " << endl;
    cout << "==============================================" << endl;
    cout << fixed << setprecision(3);
    for ( int i = 0; i < n; i++)
    {
        cout << setw(5) << V[i].designation << setw(10) << V[i].value/V[j].value << right << V[i].name << endl;
    }
    cout << endl;
} 
Last edited on
Topic archived. No new replies allowed.