Parallel Arrays

Hello...questions are below :)

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

int main()
{
   // Declare variables.
   string addIn;     // Add-in ordered
   string XXX;
   const int NUM_ITEMS = 5; // Named constant
   // Initialized array of add-ins
   string addIns[5] = {"Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"}; 
   // Initialized array of add-in prices
   double addInPrices[5] = {.89, .25, .59, 1.50, 1.75};
   bool foundIt = false;     // Flag variable
   int x;   		     // Loop control variable
   double orderTotal = 2.00; // All orders start with a 2.00 charge

   // Get user input
   cout << "Enter coffee add-in or XXX to quit: ";
   cin >> addIn;
		
   // Write the rest of the program here. 
  for(x = 0; x < NUM_ITEMS; x++)
  {
      cout << addIns[x] << " cost " << addInPrices[x] << ", "<< "Order Total is " << addInPrices[x] + orderTotal + 1.75 << endl; 
      cout << "Additional Charge: 1.75, " << endl;
  }
            
     if (addIn == addIns[x])
       {
           foundIt = true;
       }
    else
    {
        cout << "Sorry, we do not carry that.";
        
    }        
    
    if (foundIt == true)
       {
           cout << "valid flavors" << endl;
       }
    else
    {
        cout << "invalid flavors" << endl;
    } 

   return 0;
} // End of main() 


My output:
Enter coffee add-in or XXX to quit: Cream
Cream cost 0.89, Order Total is 4.64
Additional Charge: 1.75,
Cinnamon cost 0.25, Order Total is 4
Additional Charge: 1.75,
Chocolate cost 0.59, Order Total is 4.34
Additional Charge: 1.75,
Amaretto cost 1.5, Order Total is 5.25
Additional Charge: 1.75,
Whiskey cost 1.75, Order Total is 5.5
Additional Charge: 1.75,
Sorry, we do not carry that. invalid flavors ===> for this statement...I want this to only show when the user enters other than the valid array data. how?

Also, is it possible just to have this kind output whenever the users enter a valid addin?
like....
output:
Enter coffee add-in or XXX to quit: Cream
Cream cost 0.89, Order Total is 4.64
Additional Charge: 1.75

or

output:
Enter coffee add-in or XXX to quit: XXX
"Thanks for using our services."

I'm kind of having trouble when comes to creating loops. Please help me learn and overcome this obstacle :D
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

int main(){

    cout << "Welcome, press 1 to order and 0 to quit" << endl;

    int choice; 
    cin >> choice;

    while(choice == 1){
       // your code for ordering here

       cout << "Would you like to order again (1 or 0)? << endl;
       cin >> choice; 
    }
    cout << "Have a good day!" << endl; 
Last edited on
For the other question you could also do a loop inside the while loop that checks to see if the user inputs an invalid answer:

1
2
3
4
5
6
7
8

cout << "Enter a valid choice" << endl;

// add valid choices until you have all the valid choices
while ((addIn == /*valid choice*/) || (addIn == /*valid choice*/)){
        // put your code for adding prices of orders here
}


If you want that to loop then add another choice option like: int order that asks for more choices but add something after this that totals everything together. Using different loops might make this code shorter (i.e. switch case loop). Hope this helps a little.
Last edited on
Hello kuushie118,

First tip: A few blank lines and your program would look like this. It is much easier to read and follow:

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

using namespace std;  // <--- Best not to use.

int main()
{
	const int NUM_ITEMS = 5; // Named constant
	
	// Declare variables.

	int x;   	 // Loop control variable  <---Better to do this in the for loop unless you need it outside the loop.
	// Initialized array of add-in prices
	double addInPrices[5] = { .89, .25, .59, 1.50, 1.75 };
	double orderTotal = 2.00; // All orders start with a 2.00 charge

	string addIn;     // Add-in ordered
	string XXX;  // <--- You have defined it, but never yse it. What is it for?

	// Initialized array of add-ins
	string addIns[5] = { "Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey" };

	 // Get user input
	cout << "Enter coffee add-in or XXX to quit: ";
	cin >> addIn;

	// Write the rest of the program here. 
        // <--- A for loop is not what you want here.
	for (int x = 0; x < NUM_ITEMS; x++)  // <--- Changed.
	{
		cout << addIns[x] << " cost " << addInPrices[x] << ", " << "Order Total is " << addInPrices[x] + orderTotal + 1.75 << endl;
		cout << "Additional Charge: 1.75, " << endl;
	}

	if (addIn == addIns[x])
	{
		foundIt = true;
	}
	else
	{
		cout << "Sorry, we do not carry that.";

	}

	if (foundIt == true)
	{
		cout << "valid flavors" << endl;
	}
	else
	{
		cout << "invalid flavors" << endl;
	}

	return 0;
} // End of main()  

I also moved some lines around like the "const" I moved to the first line so that it is easier to find and change if needed. BTW "constexpr" is the newer version of "const". Used from C++11 on.

To answer your question you can have your output look like:

Enter coffee add-in or XXX to quit: Cream
Cream cost 0.89, Order Total is 4.64
Additional Charge: 1.75

or

output:
Enter coffee add-in or XXX to quit: XXX
"Thanks for using our services."


Do not take this wrong, but just tell the program to do it that way.

The first for loop, in your code line 24, should not be there. At that point you should only print out based on the users input not the whole array.

Do you know about "enum"s, "switch"s, "do/while loops and "functions? Together they would make life easier just print what the user ordered. With the do/while loop allowing for more than one choice. An after thought include the header file "cctype" and you can use "std::toupper(...)" or "std::"tolower(...)" to change the case of a letter or in a "switch" the "case" statements can deal with both cases.

If you are not up to a "switch' yet you could use if/else" or "if/else if" statements.

I am not you or there, so you will have to let me know what you do know and what you can do so I can better help you in the right direction.

Do notice my comments in the program. They are the ones that start with "// <--- ".

I will work on your code a bit and see what I come up with.

Hope that helps,

Andy
Hello kuushie118,

I worked on the program for awhile and achieved these results. I believe this is what you are looking for:



 Enter coffee add-in or Q to quit
 Your choices are (Cream, Cinnamon, Chocolate, Amaretto, Whiskey): xxx

 Sorry, we do not carry that flavor. Your choices are (Cream, Cinnamon, Chocolate, Amaretto, Whiskey).

 Enter coffee add-in or Q to quit
 Your choices are (Cream, Cinnamon, Chocolate, Amaretto, Whiskey): cream

 Cream cost 0.89, Order Total is 4.64
 Additional Charge: 1.75

 Enter coffee add-in or Q to quit
 Your choices are (Cream, Cinnamon, Chocolate, Amaretto, Whiskey): cinnamon

 Cinnamon cost 0.25, Order Total is 4
 Additional Charge: 1.75

 Enter coffee add-in or Q to quit
 Your choices are (Cream, Cinnamon, Chocolate, Amaretto, Whiskey): q

 Thanks for using our services.



 Press Enter to continue: _


I tried the "enum"and "switch, but found thet "addIn" being defined as a string I could not use it in a switch, so I went with "if/else if" statements for now.

After working with a running program I have these thoughts:

Since I used a "do/while" loop to enter the choices I am thinking that a menu with choices would work better than having the used, who can not read or spell type in a whole word, it would be easier to choose a number from a menu.

When you get to printing the output What happens if the user chooses more than one item for their coffee. As of now it prints out prices and total for each item which could make a coffee over $8.00 and that is not what you want. You need a way to keep track of each item ordered and add it all together into one total then print out what was ordered. After that is done some of the variables would need to be zeroed out before the next purchase.

Hope that helps,

Andy
Everyone, thanks for your help :D :D
I managed to get it to work and submit the work with 100% score
Thank you so much T^T
Last edited on
Hello kuushie118,

You are welcome. I am glad that you learned something from all this.

Andy
No problem, happy coding!

Talemache~
Topic archived. No new replies allowed.