In need of some C++ Help!

The program I'm trying to code needs to consist of the following:

Letting a user input the name and prices for 10 stocks and store the names and values of these stocks into two separate arrays. From there, you will ask the user (using a menu) to make a selection:

1. Convert from dollars to pounds.
2. Convert from dollars to euros.
3. Average the prices of the 10 stocks.
4. Count up how many stocks are above $25.
5. Display the stocks sorted.

Write "IF statements that will handle the above menu and do the selected choice." Use the following guidelines:

1. $1 US dollar is 0.97 pounds.
2. $1 in US is 0.45 euros.
3. Do the average of all 10 stocks.
4. You will use an "IF Statement" to see if the price is above 25 for each value in the array.
5. Use only the "Bubble Sort algorithm" to display the stocks sorted.

I need to have the output for whichever selection the user selects, and from there I will then need to look through the program.

**NOTE** MUST use a "Switch Statement" when looking through to see what selection was made.


Here is the code I have written. Please go easy on me for I am very new to the any kind of coding and programming language. I just don't understand 90% of any of this. Please try to help me code and explain in the simplest terms possible.
I have honestly tried and tried to get to this work after many hours. I not having much luck at all with this. Here is the 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
#include <iostream>
using namespace std;
#include <math.h>
#include <string>

void main(){
	int selection;
	double prices[10];
	string names[10];
	int i;
	char wait='a';

	do{

	cout << "Please make your selection: " << endl;
	cout << "What would you like to do: " << endl;
	cout << "--------------------------" << endl;
	cout << "1. Convert from Dollars to Pounds.\n2. Convert from Dollars to Euros.";
	cout << endl;
	cout << "3. Average the prices of the 10 stocks. ";
	cout << endl;
	cout << "4. Count up how many stocks are above $25. ";
	cout << endl;
	cout << "5. Display the stocks sorted. ";
	cout << endl << endl;
	cin >> selection ;
	cout << endl << endl;

	


		for(int i=1; i<=9; i++){
			cout << "\nEnter a stock name: ";
			cin >> selection;
			cout << "\nEnter a stock price: ";
			cin >> selection;
		}


		switch (selection){
		case 1:
			prices[10] =(prices[10]*0.97);
			cout<<"\n1. Convert from Dollars to Pounds.\n2. Convert from Dollars to Euros. " << prices;
			break;
		case 2:
			prices[10] =(prices[10]*0.45);
			cout<<"\n2. Convert from Dollars to Euros." << prices;
			break;
		case 3:
			prices[10] = (prices[10] + prices[10]) / 10.0;
			cout<<"\n3. Average prices of 10 stocks. " << prices;
			break;
		case 4:
			prices[10] = prices[10] < 25.00;
			cout << "\n4. Count stocks that are above $25. " << prices;
			break;
		

		}
		

		cout << "Enter 1 to continue, any other key to exit:" ;
		cin >> wait;

	}while(wait != 'x' && wait != 'X');
	return;
}


Can you use Functions?
Also you use alot of cout's in the beginning. May I suggest this alternative?
1
2
3
4
5
6
7
8
9
10
cout << "Please make your selection:\n"
        "What would you like to do:\n"
        "----------------------------\n"
        "1. Convert from Dollars to Pounds.\n"
        "2. Convert from Dollars to Euros.\n"
        "3. Average the prices of the 10 stocks.\n"
        "4. Count up how many stocks are above $25.\n"
        "5. Display the stocks sorted.\n\n";
// This is legal. C++ will merge all these lines into a single const character array which cout will then
// print. 
closed account (EvoTURfi)
On lines 34 and 36, you are taking the stock name and price into the selection variable. This will overwrite the user's menu choice!

cin >> selection;

Why aren't you just using the prices and names variable you declared?

1
2
double prices[10];
string names[10];


Also, in the switch statement, you are only converting the 10th value in the prices array:

prices[10] =(prices[10]*0.97);

The assignment says that you need to convert all of them. I would recommend using a loop to go through all the values and convert each one like this:

Declare a int variable a and then:

1
2
3
4
5
while(prices[a]!='\0')
{
prices[a]=prices[a]*0.97;
a++;
}


You can do something similar for the rest of the converting. As for the average of the prices, you can use a loop and go through the prices array values like what I showed you above and then you can add the values to a counter. Then you can divide the counter by 10. For the sorting, I can't help as I am not familiar with the bubble sort algorithm.
Last edited on
Topic archived. No new replies allowed.