Input Unknow Variable

I need to be able to input any of 118 pre-programed variables or any number into a program, and I don't know how many inputs there are going to be. I need umbers multiplied by the prior input, and symbols (after multiplication by the numbers) to be added to gether. is there a way I can do that?
Here's the program
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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

float h = 1.0079;
float he = 4.002;
float li = 6.941;
float be = 9.012;
float b = 10.811;
float c = 12.0107;
float n = 14.0067;
float o = 15.9994;
float f = 18.998;
float ne = 20.1797;
float na = 22.989;
float mg = 24.3050;
float al = 26.981;
float si = 28.0855;
float p = 30.973;
float s = 32.065;
float cl = 35.453;
float ar = 39.948;
float k = 39.948;
float ca = 40.078;

char number[101];
    char cmpnd[101];
    cout << "Please input the first symbol in your chemical compound (all lower case):"
    cin >>//this is where it starts.
  
}

I have only programmed in the first 20 variables.
Thank you in advance!
The simplest way is via a sentinal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	int count = 0;
	int input;
	int collection[128]; // or any sized number that is more than ever used

	cout << "Enter numbers, negative to stop.\n"; 
	do
	{
		cout << "Input #" << count + 1 << ": ";
		cin >> input;
		if (input >= 0)
		{
			collection[count] = input;
			count++;
		}
	} while (count < 128 && input >= 0 );

	cout << "\nYou entered:\n";
	for (int i = 0; i < count; i++)
		cout << "Input #" << i + 1 << " = " << collection[i] << endl;


If you want your program to accept something like CH3CO2H, then I would suggest extracting the entire line as a character array and then parsing as needed.
Last edited on
Topic archived. No new replies allowed.