Why isn't my code displaying the correct output?

I am reading from a text file. Asking the user to input an inventory number and finding that inventory number using the linear search function. Then I am displaying the results found associated with that inventory number. Overall, I think I have the program right, however, I am getting the incorrect output display.

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

using namespace std;

//Prototype Statements
void getData(string, string [], string [], int [], double [], int &);
void processUserSelections(string [], string [], string , double [], int [], int &);
int linearSearch(const string [], int &, string);
void displayResults(string [], double [], int [], int );

const int AMT = 40;

int main()
{	
	
	string number;
	int position;
	string filename = "inventory.txt";
	int num_inventory = 0;
	string inv_num[AMT],  description[AMT];
	int mileage[AMT];
	double price[AMT];
	
	
	//Call the functions.
	getData(filename, inv_num, description, mileage, price, num_inventory);
	processUserSelections( inv_num,  description,  number,  price, mileage, num_inventory);
	
}
void getData(string filename, string inv_num[], string description[], int mileage[], double price[], int &num_inventory)
{
	ifstream infile;
	infile.open(filename.c_str());
   
    string line;
	int c = 0;
	while (getline(infile, inv_num[c]))
    {
        getline(infile, description[c]);
		infile >> mileage[c];
		infile >> price[c];
		infile.ignore();
		getline(infile, line); 
		c++;
    }

    infile.close();  

    num_inventory = c;  
}
void processUserSelections(string inv_num[], string description[], string number, double price[], int mileage[], int &num_inventory)
{
	
	int position = 0;
	cout << "Welcome to the Car Lot Inventory program!\n" << endl;
	
	while(!(number == "q" || number == "Q"))
	{
		cout << "Enter the cars inventory number <or q to quit>: ";
		cin >> number;
		cout << "\n";
		
		if(number == "q" || number == "Q")
		{
			cout << "\nPress ENTER to continue.....\n\n";
			cin.ignore();
			cin.get();
			return;
		}
		
		
		position = linearSearch(inv_num, num_inventory, number);
		displayResults(description, price, mileage, position);
		
	}
	return;
	
	
}
int linearSearch(const string inv_num[], int &num_inventory, string number)
{
	for (int c = 0; c < num_inventory; c++)
	{
		if (inv_num[c] == number)
			return c;
	}
	return -1;
	
}
void displayResults(string description[], double price[], int mileage[], int position)
{
	
	
	cout << "Description: " << description[position];"\n";
	cout << "Mileage:     " << mileage[position] << "\n";
	cout << "Price:		  " << price[position] << "\n";
	
	
	
}

Reading from this following list in a text file.
1005
2010 Honda Accord Ex-L
36800
10500

1018
2015 Jeep Cherokee Latitude
26500
20300

1158
2011 GMC Yukon Hybrid
91700
27600

1198
2008 Nissan Titan SE
114000
16000

1245
2004 Toyota Celica
103600
7400

1353
2007 Nissan Pathfinder SE
130000
7900

1408
1996 Ford Bronco XLT 4WD
151000
8900

1489
2012 Nissan Altima
65800
13900

1965
2013 Ford F-150 STX
42600
26100

1983
2010 Toyota 4Runner SR5 4WD
68000
23000

2266
2012 Ford Explorer XLT
40300
27800

2277
1014 Toyota Camry
24500
18000

2388
2011 Mazda RX-8
38000
18500

2468
2009 Kia Optima
102300
6900

2510
2006 Dodge Caravan
157000
3300

2647
1996 Chevrolet Astro Cargo Van
226000
3250

2897
2014 Hyundai Genesis
20400
28000

2978
2011 Fiat 500L
17500
11900

3248
2015 Jeep Wrangler
6100
29900

3589
2005 Chevrolet Silverado 1500
185000
10000

3608
2011 Nissan Frontier
65000
17500

3907
2012 Honda Civic
78000
10500

4588
2014 Dodge Avenger
50000
9900

4968
2015 Dodge Grand Caravan
25000
17500

5148
2015 Toyota Highlander
18000
34000

5855
1996 Nissan 300ZX
68000
13200

6450
2016 Honda Odyssey
14500
32700

6877
2003 GMC Safari
220000
4000

6900
2013 Ford Expedition
87000
22900

7234
2002 Mazda Millenia
109000
5900

7580
2001 Chevrolet Lumina
154000
3500

7966
2016 Toyota Tacoma
4000
21900

8125
2001 Chevrolet Corvette
47000
18800

8435
2007 Jeep Grand Cherokee
121000
9800

8677
2012 GMC Acadia SLE
108900
14900

8958
2016 Fiat 500X Trekking
3690
22990

9105
2009 Kia Sportage LX
40500
16900

9238
2014 Chevrolet Corvette Stringray
2400
56800

9488
2001 Nissan Frontier
138000
9400

9899
1994 Toyota Land Cruiser
261000
8800
Last edited on
Hello lg77756,

I have tried and tried, but I do not believe I am getting the same incorrect output that you are.

Given what input what incorrect output are you getting?

Difficult to duplicate your error when no one has any idea what you did.

You need to explain your question and problem better.

One thing I did notice:
 
while (!(number == "q" || number == "Q"))

"number" is defined as a "std::string" and can hole any number of characters. So if a user decides to enter "quit" into number it will never match because the strings are different lengths.

Consider:
 
while (!(number[0] == 'q' || number[0] == 'Q'))

Notice the single quote around the (q)s.

And I did this with:
1
2
3
4
5
6
7
void displayResults(string description[], double price[], int mileage[], int position)
{
    cout <<
        "\nDescription: " << description[position] <<
        "\nMileage:     " << mileage[position] <<
        "\nPrice:       " << price[position] << "\n";
}

This makes it easier to work with.

Andy
When I run the program my output does not display the description, mileage, and price correctly. They are just weird numbers.
When you write things like
string number;
I can't help feeling you are asking for trouble.

What happens when your linear search returns -1 and you then try to displayResults() with that array index?



When I finally clocked what I was supposed to input (how about putting that in your question?) then I got this:
Welcome to the Car Lot Inventory program!

Enter the cars inventory number <or q to quit>: 1018
Description: 2015 Jeep Cherokee LatitudeMileage:     26500
Price:           20300
Enter the cars inventory number <or q to quit>: q

Press ENTER to continue.....


The whimsical layout comes from your line
cout << "Description: " << description[position];"\n";
Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

//Prototype Statements
void getData(const string&, string[], string[], int[], double[], int&);
void processUserSelections(string[], string[], int[], double[], int);
int linearSearch(const string[], int, const string&);
void displayResults(const string[], const int[], const double[], int);

const int AMT {40};

int main()
{
	const string filename {"cars.txt"};

	int num_inventory {};
	string inv_num[AMT], description[AMT];
	int mileage[AMT] {};
	double price[AMT] {};

	//Call the functions.
	getData(filename, inv_num, description, mileage, price, num_inventory);
	processUserSelections(inv_num, description, mileage, price, num_inventory);
}

void getData(const string& filename, string inv_num[], string description[], int mileage[], double price[], int& num_inventory)
{
	num_inventory = 0;

	ifstream infile(filename.c_str());

	if (!infile) {
		cout << "Cannot open the file\n";
		return;
	}

	for (; num_inventory < AMT && getline(infile, inv_num[num_inventory]) &&
		getline(infile, description[num_inventory]) &&
		infile >> mileage[num_inventory] &&
		infile >> price[num_inventory] >> ws ; ++num_inventory);
}

void processUserSelections(string inv_num[], string description[], int mileage[], double price[], int num_inventory)
{
	cout << "Welcome to the Car Lot Inventory program!\n" << endl;

	while (true) {
		string number;

		cout << "Enter the cars inventory number <or q to quit>: ";
		cin >> number;
		cout << "\n";

		if (number[0] == 'q' || number[0] == 'Q') {
			cout << "\nPress ENTER to continue.....\n\n";
			cin.ignore();
			cin.get();
			return;
		}

		const int position {linearSearch(inv_num, num_inventory, number)};

		if (position != -1)
			displayResults(description, mileage, price, position);
		else
			cout << "Not found!\n";
	}
}

int linearSearch(const string inv_num[], int num_inventory, const string& number)
{
	for (int c = 0; c < num_inventory; c++)
		if (inv_num[c] == number)
			return c;

	return -1;
}

void displayResults(const string description[], const int mileage[], const double price[], int position)
{
	cout << "Description: " << description[position] << '\n';
	cout << "Mileage:     " << mileage[position] << '\n';
	cout << "Price:       " << price[position] << '\n';
}


which reads the file OK if exists, tests if the entered number exists, orders the function params in the same order etc.


Welcome to the Car Lot Inventory program!

Enter the cars inventory number <or q to quit>: 6877

Description: 2003 GMC Safari
Mileage:     220000
Price:       4000
Enter the cars inventory number <or q to quit>: 5667

Not found!
Enter the cars inventory number <or q to quit>: 6900

Description: 2013 Ford Expedition
Mileage:     87000
Price:       22900
Enter the cars inventory number <or q to quit>: q


Press ENTER to continue.....

Hello lg77756,

I wish I could have finished this last night, but I could not.

Over time I have learned to trust lastchance's advice. So when he says "number" should not be a string consider this: as you read the file the inventory numbers are "1000", "1001" and so on all 4 digit numbers, but "number as a "std::string" a user could enter " 1000" then the 2 strings would not match.

I did not see this last night: cout << "Description: " << description[position];"\n";. The "\n" between the ;s has no affect on the "cout" statement that ends with the first (;).

As lastchance's shows when the first line, the description, is displayed the "\n" is ignored or the program does not know what to do with it, so it is just bypassed and the second "cout" starts where the last one ended.

To properly write what you have it should look like this:
cout << "Description: " << description[position] << "\n";. Now the "\n" at the end is part of the "cout" statement.

Starting in "processUserSelections" you have the line:
position = linearSearch(inv_num, num_inventory, number);. That part is fine and works. The function call to "linearSearch" is called and returns a value that you do capture except that you do not use it properly. You just call the next function "displayResults" with no idea what the value of "position" is.

Trying to access an array with (-1) means that you are trying to read a memory location before the array starts.

When I figured out what was haping I changed "linearSearch" to return (0) zero if no match was found.

In the function "processUserSelections" I added an if statement:
1
2
3
4
5
6
7
8
if (position)
{
    //call display functio.
}
else
{
    // display an error message.
}

This way if "position" is a positive number you enter the if block. If "position" is (0) zero you enter the else block.

After that it was a matter of adjusting the "\n"s to get this to display on the screen:

 Welcome to the Car Lot Inventory program!

 Enter the cars inventory number <or q to quit>: 1198

Description: 2008 Nissan Titan SE
    Mileage: 114,000
      Price: $16,000.00

 Enter the cars inventory number <or q to quit>: 1000

     Inventory number not found!
     Try again.

 Enter the cars inventory number <or q to quit>:


And even that could maybe use some adjustment. Feel free to change the error message to anything that you like.

Andy
Topic archived. No new replies allowed.