Help with code

Pages: 12
I need help, my code is just not working and I've tried everything to make it work. I've been at this for a week and well, I haven't been able to get it to display the content in the txt.file It'll display everything in "" but not the file, I'm not sure where I went wrong.


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
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
#define Max_Number 100   
void loadArrays(string console[], int price[], int& numb);
void showArrays(string console[], int price[], int numb);
void sortPrices(string cons[], int pric[], int numb);
void lookupPrice(string console[], int price[], int numb);
void loadArrays(string console[], int price[], int& numb)
{
	
    int i = 0;
    const int l = 5;
    ifstream txtpricesfile;
	txtpricesfile.open("prices.txt");
    i++;
    while (getline(txtpricesfile, console[i]))

    {
        
        string temp;
        getline(txtpricesfile, temp);
        price[i] = stoi(temp);
    }
}
void showArrays(string console[], int price[], int numb)
{
    system("cls");
    cout << setw(25) << "Console" << setw(25) << "Price" << "\n";
    for (int i = 0; i < numb; i++)
    {
        cout << setw(25) <<  console << setw(25) << price[i] << "\n";
    }
}
void lookupPrice(string console[], int price[], int numb)
{
    string Console, tmp;
    cout << "Enter the console name:";
    cin >> Console;
    cin >> tmp;
    Console == tmp;
    int i = 0;
 

		if (Console == (console[i]))
        {
            cout << "The current price for " << Console << " is " << price[i] << "\n";
        }
    if (i == numb)
        cout << "Incorrect choice. ";
}
void sortPrices(string cons[], int pric[], int numb)
{
    int i, j, min, tmp;
    string t;
    for (i = 0; i < numb - 1; i++)
    {
        min = i;
        for (j = i + 1; j < numb; j++)
            if (pric[j] < pric[min])
                min = j;
        tmp = pric[min];
        t = cons[min];
        pric[min] = pric[i];
        cons[min] = cons[i];
        pric[i] = tmp;
        cons[i] = t;
    }
    cout << "Consoles sorted!. ";
}
void HighestPrice(string cons[], int pric[], int numb)
{
    int max = pric[0], i = 0;
    string Console = cons[0];
    for (i = 1; i < numb; i++)
    {
        if (pric[i] > max)
        {
            max = pric[i];
            Console = cons[i];
        }
    }
    cout << "The console that has the highest price is : " << Console << "\n with " << pric;
}

int main()
{
    string cons[Max_Number];
    int pric[Max_Number];
    int numb = 0;
    loadArrays(cons, pric, numb);
    int toggle = 1;
    do
    {
        cout << "\nConsole Pricing - Main Menu \n1. Display all console prices \n2. Look up the price of a particular console \n3. Sort prices in descending orders \n4. Display the console with the highest price. \n5. Exit the program \nEnter Your choice";
        int choice;
        cin >> choice;
        switch (choice)
        {
            system("CLS");
        case 1: showArrays(cons, pric, numb); 
            toggle = 1;
            break;
            system("CLS");
        case 2: lookupPrice(cons, pric, numb); 
            toggle = 1;
            break;
            system("CLS");
        case 3: sortPrices(cons, pric, numb); 
            toggle = 1;
            break;
            system("CLS");
        case 4: HighestPrice(cons, pric, numb);
            toggle = 1;
            break;

        case 5: break;
        default: cout << "Incorrect choice. ";break;
        }
       
    } while (toggle = 1);
    return 0;
}
Given that console is an array it isn't going to like (on line 34):
cout << setw(25) << console << setw(25) << price[i] << "\n";

Try console[i], not console.

Also, make sure that your file opened (i.e. was found where you expected to find it.)
Last edited on
Hello Notfire,

As a start consider this:
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<fstream>
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

#define Max_Number 100  

void loadArrays(string console[], int price[], int& numb);
void showArrays(string console[], int price[], int numb);
void sortPrices(string cons[], int pric[], int numb);
void lookupPrice(string console[], int price[], int numb);

void loadArrays(string console[], int price[], int& numb)
{
    int i = 0;
    const int l = 5;

    ifstream txtpricesfile;

    txtpricesfile.open("prices.txt");
    
    // <--- How do you know it is open??
    
    i++;

    while (getline(txtpricesfile, console[i]))
    {
        string temp;
        getline(txtpricesfile, temp);
        price[i] = stoi(temp);
    }
}

Adding some blank lines to the code makes it easier to read and follow along with helping the problems to be seen quicker.

As the comment says how do you know the file is open and usable? For all I know the file did not open and the while loop is bypassed when the "getline()" can not read anything.

Also you did not provide any input file to use with the program, so if I was to run the program this function would not work.

Andy
Hello Notfire,

While I am waiting, your program reworked a bit:
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

#define UINT unsigned int

constexpr UINT Max_Number{ 100 };

int loadArrays(string console[], int price[], int& arrayUsed);
void showArrays(string console[], int price[], int arrayUsed);
void sortPrices(string cons[], int pric[], int arrayUsed);
void lookupPrice(string console[], int price[], int arrayUsed);

int loadArrays(string console[], int price[], int& arrayUsed)
{
    int idx = 0;
    const int l = 5;  // <--- Why are you setting the number 1 = to 5 and what is is used for?

    ifstream txtpricesfile("prices.txt");

    if (!txtpricesfile)
    {
	    std::cout << "\n File " << std::quoted("prices.txt") << " did not open" << std::endl;

        return 1;
    }

    while (getline(txtpricesfile, console[idx]))
    {
        string temp;

        getline(txtpricesfile, temp);

        price[idx] = stoi(temp);
        
        idx++;  // <--- Moved.
    }

    return 0;
}

void showArrays(string console[], int price[], int arrayUsed)
{
    system("cls");

    cout << setw(25) << "Console" << setw(25) << "Price" << "\n";

    for (int i = 0; i < arrayUsed; i++)
    {
        cout << setw(25) << console << setw(25) << price[i] << "\n";
    }
}

void lookupPrice(string console[], int price[], int arrayUsed)
{
    string Console, tmp;

    cout << "Enter the console name:";

    cin >> Console;

    cin >> tmp;

    Console == tmp;
    int i = 0;


    if (Console == (console[i]))
    {
        cout << "The current price for " << Console << " is " << price[i] << "\n";
    }

    if (i == arrayUsed)
        cout << "Incorrect choice. ";
}

void sortPrices(string cons[], int pric[], int arrayUsed)
{
    int i, j, min, tmp;

    string t;

    for (i = 0; i < arrayUsed - 1; i++)
    {
        min = i;

        for (j = i + 1; j < arrayUsed; j++)
            if (pric[j] < pric[min])
                min = j;

        tmp = pric[min];
        t = cons[min];
        pric[min] = pric[i];
        cons[min] = cons[i];
        pric[i] = tmp;
        cons[i] = t;
    }
    cout << "Consoles sorted!. ";
}

void HighestPrice(string cons[], int pric[], int arrayUsed)
{
    int max = pric[0], i = 0;

    string Console = cons[0];

    for (i = 1; i < arrayUsed; i++)
    {
        if (pric[i] > max)
        {
            max = pric[i];
            Console = cons[i];
        }
    }
    cout << "The console that has the highest price is : " << Console << "\n with " << pric;
}

int main()
{
    string cons[Max_Number];
    int pric[Max_Number]{};
    int arrayUsed{};

    //if (loadArrays(cons, pric, arrayUsed))
    //    return 1;

    int toggle{ 1 };

    do
    {
        cout <<
            "\n" << std::string(10, ' ') << "Console Pricing - Main Menu\n" << std::string(47, '-') <<
            "\n 1. Display all console prices"
            "\n 2. Look up the price of a particular console"
            "\n 3. Sort prices in descending orders"
            "\n 4. Display the console with the highest price."
            "\n 5. Exit the program"
            "\n  Enter Your choice: ";

        int choice;

        cin >> choice;

        switch (choice)
        {
            //system("CLS");  // <--- Not used here Has no effect. Move to above switch. Or delete and move the next "CLS".
            case 1:
                showArrays(cons, pric, arrayUsed);
                toggle = 1;  // <--- NOt needed as "togle" is already 1.
                break;
                system("CLS");  // <--- Never reached because of the "breaK". Maybe move to first line of the case.
            case 2:
                lookupPrice(cons, pric, arrayUsed);
                toggle = 1;  // <--- NOt needed as "togle" is already 1.
                break;
                system("CLS");  // <--- Never reached because of the "breaK".
            case 3:
                sortPrices(cons, pric, arrayUsed);
                toggle = 1;  // <--- NOt needed as "togle" is already 1.
                break;
                system("CLS");  // <--- Never reached because of the "breaK".
            case 4:
                HighestPrice(cons, pric, arrayUsed);
                toggle = 1;  // <--- NOt needed as "togle" is already 1.
                break;

            case 5:
                toggle = 0;  // <--- Leaves the switch, but "toggle" needs to be set to 0.
                break;
            default:
                cout << "Incorrect choice. ";
                break;
        }

    } while (toggle = 1);

    return 0;
}

Just a suggestion.

The "cout" statement in the do/while loop in main produces the output of:

          Console Pricing - Main Menu
-----------------------------------------------
 1. Display all console prices
 2. Look up the price of a particular console
 3. Sort prices in descending orders
 4. Display the console with the highest price.
 5. Exit the program
  Enter Your choice:


I also had to add a line to case 5.

Andy
@OP, your first procedure, loadArrays, needs to give a value to variable numb, or you’ll never know how many items you read and main() will leave it with its initial value 0. You also need to move the i++ inside the while loop, or you’ll be continually overwriting console[1] and price [1].
Last edited on
@Handy Andy This is the information on the txt.file, had no idea how to upload the file itself.

Nintendo Entertainment System
57

Sega Genesis
34

Microsoft Xbox 360
58

Sony PlayStation 4
261

Atari 2600
26

Nintendo Game Cube
52

I geninuinely have some problems with explaining things, so I am sorry.
Last edited on
I need help, my code is just not working and I've tried everything to make it work. I've been at this for a week and well, I haven't been able to get it to display the content in the txt.file It'll display everything in "" but not the file, I'm not sure where I went wrong.


ROFL, another one - writes 125 lines of code (personally and guaranteed not dredged up from the web) and can't get the file to read.

C'mon @Notfire try it yourself instead of this con trick.
I haven't been able to get it to display the content in the txt.file


Here, this is how you read a text file.
https://www.cplusplus.com/doc/tutorial/files/
ROFL, another one - writes 125 lines of code (personally and guaranteed not dredged up from the web) and can't get the file to read.


I don't take kindly to your tone, I genuinely have issues with this type of stuff and I can't help it even if do study. I don't process things the same way you would.
Take kindly or not. I could care less.

If you were serious about what you are doing you would concentrate on the problem you have in reading the text file instead of submitting 125 lines of code which, let's be kind, hide the problem you so eminently wrote into your program while slaving away at it for a week.

The problem you have is addressed by adapting trying the tutorial example out and getting that to at least read in your file, and then, and only then, place it back into your program.

My method works in accordance with normal software development practice.

Pissing in my pocket and wasting your time about tone is slowing down your progress, if indeed you could call it that because you haven't changed since you started posting with your other rubbish.

If you were genuine you wouldn't be moaning.
Take kindly or not. I could care less.

If you were serious about what you are doing you would concentrate on the problem you have in reading the text file instead of submitting 125 lines of code which, let's be kind, hide the problem you so eminently wrote into your program while slaving away at it for a week.

The problem you have is addressed by adapting trying the tutorial example out and getting that to at least read in your file, and then, and only then, place it back into your program.

My method works in accordance with normal software development practice.

Pissing in my pocket and wasting your time about tone is slowing down your progress, if indeed you could call it that because you haven't changed since you started posting with your other rubbish.

If you were genuine you wouldn't be moaning.


So me asking for help with an issue I've been slaving over is wrong? Is that what you're saying? Cause I've tried everything and I've rewritten this blasted program to hell and back. I'm not the best coder I'll admit that but at least I fucking try before coming here and asking for godamn help.
Boo hoo. What a snowflake.

Why don't you spend your time on something useful trying out what I suggested?

Everybody else I know, including me, have done exactly the same instead of throwing a tantrum.

The world doesn't owe you a living simply because you can't help yourself once given a useful pointer to solving the problem.

By the way, you system cls and \n's littering your so uniquely-you program is peppered with, are the lowest of low priorities in developing a program, especially when you can't or won't even read in the data your program is supposed to process. It's also about priorities, and along with whining, you have them all ass about face.
Ah, @againtry is our resident idiot. You're fine, don't pay him any mind.
Last edited on
You sure @mbozzi, he's reminding me of several people that I already had a dislike for. But I'll take your word for it.
Most of our trolls get bored after a few posts, but this dude knows enough C++ to answer some basic questions. This means he/she can actually engage with the topic enough to stick around. He probably has a few accounts.

Personally, I can't usually see what he's posting thanks to a tampermonkey script another regular cooked up.
Last edited on
Ah, well he's basically telling me I'm a snowflake. Also, neat. Hey, do you have discord of sometype @mbozzi?
Last edited on
I've got an account, I think, but I'm about done for the night.
@mbozzi would you mind private messaging me? I'd like to talk, perhaps see if I could get some help or something. Fuck, its been a long week.
Here we go,

Carol the Argentinian Nektra creep, and her groomer software has caught another groomed donkey to chime in.

Notfire can't even fire up with sound advice, scrapes up somebody else's program and expects namby pamby to mother the way through - no effort, no commitment and what's even sadder a denier who'll moan and whine throughout his life knowing nothing, contributing nothing - just a self entitled POS full of back answers that only dig his hole of ineptitude and cheating deeper and deeper.

I've seen school kids with more nouse than smoke and noFire shows. They move forward with all sorts of suggestions from all sorts of people here while this one just splutters to a standstill.
There ya go! I self reported for tone reasons - my heart bleeds - a nice heartfelt tonal response! ROFL.

But still no advance on the thing that counts though. Like reading a text file.

Even I with my mediocre, trolling, retard, cuntface , and go fuck yourself, shitbag, palooka, brainless, stamp your foot, capitalize flaming, and less than barebones basic C++ knowledge knows how to do it. Ha ha ha.
@Notfire. I'm sorry you're been subject to 'abuse' by againtry. I assure you we've not all like that and most do try to be helpful.

Consider:

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
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

using UINT = unsigned int;

constexpr UINT Max_Number {100};

int loadArrays(string [], int [], UINT&);
void showArrays(string [], int [], UINT);
void sortPrices(string [], int [], UINT);
void lookupPrice(string [], int [], UINT);

int loadArrays(string console[], int price[], UINT& used)
{
	used = 0;

	ifstream prices("prices.txt");

	if (!prices)
		return (std::cout << "\n File " << std::quoted("prices.txt") << " did not open" << std::endl), 1;

	for (string pr, temp; used < Max_Number && getline(prices, console[used]) && getline(prices, pr) && getline(prices, temp); ++used)
		price[used] = stoi(pr);

	prices.close();

	return (std::cout << "Data loaded OK\n"), 0;
}

void showArrays(string console[], int price[], UINT arrayUsed)
{
	cout << '\n' << setw(35) << "Console" << setw(25) << "Price" << "\n";

	for (UINT i = 0; i < arrayUsed; ++i)
		cout << setw(35) << console[i] << setw(25) << price[i] << "\n";
}

void lookupPrice(string console[], int price[], UINT used)
{
	string Console;

	while ((cout << "\nEnter the console name: ") && (!getline(cin, Console) || Console.empty())) {
		cout << "Invalid name\n";
		cin.clear();
	}

	UINT i = 0;

	while ((i < used) && (Console != console[i]))
		++i;

	if (i < used)
		cout << "The current price for " << Console << " is " << price[i] << "\n";
	else
		cout << "Console not found.\n";
}

void sortPrices(string cons[], int price[], UINT arrayUsed)
{
	for (UINT i = 0, min = 0; i < arrayUsed - 1; ++i)
	{
		min = i;

		for (UINT j = i + 1; j < arrayUsed; ++j)
			if (price[j] > price[min])
				min = j;

		int tmp = price[min];
		string t = cons[min];

		price[min] = price[i];
		cons[min] = cons[i];
		price[i] = tmp;
		cons[i] = t;
	}

	cout << "Consoles sorted!\n";
}

void HighestPrice(string cons[], int price[], UINT arrayUsed)
{
	int max = price[0];
	string Console = cons[0];

	for (UINT i = 1; i < arrayUsed; ++i)
	{
		if (price[i] > max)
		{
			max = price[i];
			Console = cons[i];
		}
	}

	cout << "\nThe console that has the highest price is\n" << Console << " at " << max << endl;
}

int main()
{
	string cons[Max_Number];
	int price[Max_Number] {};
	UINT arrayUsed {};

	if (loadArrays(cons, price, arrayUsed))
		return 1;

	bool cont {true};

	do {
		cout <<
			"\n" << std::string(10, ' ') << "Console Pricing - Main Menu\n" << std::string(47, '-') <<
			"\n 1. Display all console prices"
			"\n 2. Look up the price of a particular console"
			"\n 3. Sort prices in descending orders"
			"\n 4. Display the console with the highest price."
			"\n 5. Exit the program"
			"\n\n  Enter Your choice: ";

		UINT choice;

		while (!(cin >> choice)) {
			cin.clear();
			cin.ignore(1000, '\n');
			cout << "Invalid input \nPlease input again: ";
		}

		cin.ignore(1000, '\n');
		switch (choice)
		{
			case 1:
				showArrays(cons, price, arrayUsed);
				break;

			case 2:
				lookupPrice(cons, price, arrayUsed);
				break;

			case 3:
				sortPrices(cons, price, arrayUsed);
				break;

			case 4:
				HighestPrice(cons, price, arrayUsed);
				break;

			case 5:
				cont = false;
				break;

			default:
				cout << "Incorrect choice.\n";
				break;
		}

	} while (cont);
}

Last edited on
Pages: 12